Member Login
| Total: | 16,480 |
| Latest: | nang |
| Onlines: | 0 |
| บุคคลทั่วไป: | 58 |

Category |
Set/Get html data ด้วย VB6 ต่อ...
All Articles >> VB >> - Set/Get html data ด้วย VB6 ต่อ...
Set/Get html data ด้วย VB6 ต่อ...
มีบทความที่รอคิวจะลงเยอะเลยครับ เรื่องไหนใหม่ผมก็จะแทรกลงไปครับ บทความวันนี้เป็นส่วนเพิ่มเติมจากเรื่อง Set/Get html data ด้วย VB6 ภาคแรกครับก็มีน้องคนนึงเมล์มาถามเรื่องการส่ง username กับ password เข้าไปยังหน้า log in ของเว็บเพจ อันที่จริงแล้วสามารถใช้ตัวอย่างของภาคแรกได้เลยครับ ซะงั้นเพื่อตัดตอนคำถามที่จะตามมา ผมทำตัวอย่างการ sign in ของ hotmail มาให้ดูครับ แต่เพิ่ม txtURL ขึ้นมาให้เหมือน webbrowser เป็นความรู้เสริม
1. ก่อนอื่นต้องเอาตัวอย่างภาคแรกมาแก้ไขครับเพิ่มคอนโทรลและจัดหน้าตาดังรูป

2. เพิ่มเติมโค้ดนี้เข้าไป
Private Sub cmdnavigate_Click()
WebBrowser.Navigate txtURL.Text, navNoReadFromCache
End Sub
Private Sub cmdlogin_Click()
Do
DoEvents
Loop Until Not WebBrowser.Busy
Set docCurrentHTML = WebBrowser.Document.documentElement.All
For Each Item In docCurrentHTML
If Item.tagName = "INPUT" Then
If Item.Name = "login" Then
Item.Value = txtusername.Text
Exit For
End If
End If
Next Item
For Each Item In docCurrentHTML
If Item.tagName = "INPUT" Then
If Item.Name = "passwd" Then
Item.Value = txtpassword.Text
Exit For
End If
End If
Next Item
WebBrowser.Document.Forms(0).submit
End Sub
Private Sub cmdnavigate_Click()
WebBrowser.Navigate txtURL.Text, navNoReadFromCache
End Sub
Private Sub cmdlogin_Click()
Do
DoEvents
Loop Until Not WebBrowser.Busy
Set docCurrentHTML = WebBrowser.Document.documentElement.All
For Each Item In docCurrentHTML
If Item.tagName = "INPUT" Then
If Item.Name = "login" Then
Item.Value = txtusername.Text
Exit For
End If
End If
Next Item
For Each Item In docCurrentHTML
If Item.tagName = "INPUT" Then
If Item.Name = "passwd" Then
Item.Value = txtpassword.Text
Exit For
End If
End If
Next Item
WebBrowser.Document.Forms(0).submit
End Sub
3. พิมพ์ "www.hotmail.com" ลงใน txtURL แล้วกดปุ่ม navigate

4. ใส่ username และ password ของ hotmail และกดปุ่ม login hotmail สังเกตว่า username กับ password ใน text box จะถูกป้อนไปใน text field ของ hotmail

5. ถ้า username และ password ถูกต้องก็จะเข้าไปยังเมล์บ๊อกซ์ดังรูป

6. นอกจากนี้เรายัง set data จาก cmbKeyword ไปยัง text field serach ได้อีกด้วย ลองคลิกที่ปุ่ม set value

เห็นไหมครับโค้ดง่าย ๆ แต่ก็ประยุกต์ไปใช้ได้ประโยชน์จริง ๆ อีกนิดนึงครับผมเห็นบางเว็บเอาเนื้อหาไปเผยแพร่หรือดัดแปลง ผมยินดีนะครับ แต่ผมอยากให้เพิ่มเครดิตคนเขียนหรือเว็บวราด้วยก็ดีครับ
บทความ: แมงเม่า