Private Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Any) As Long
Const LB_FINDSTRING = &H18F
Dim byKey As Boolean' is list1_click Event activated by a key press ?
Dim byCode As Boolean' is text1_change Event activated by code ?
Private Sub Form_Load()
' init some things ...
' first, hide list
List1.Visible = False
' set list position
List1.Top = Text1.Top + Text1.Height
List1.Left = Text1.Left
List1.Width = Text1.Width
' set list in front of all objects
List1.ZOrder
' then, let's populate the listbox with
' random strings
For i = 0 To 10000
a$ = ""
For j = 0 To 8
a$ = a$ & Chr$(Int(Rnd(1) * 26 + 65))
Next
List1.AddItem a$
Next
End Sub
Private Sub List1_Click()
' overrides any "list1.visible=false" ev
' ent-driven code ...
List1.Visible = True
' if the listindex changed because of a
' key press, we don't need to move the car
' et
If byKey = True Then
' we need to store the caret position be
' cause
' it'll be zero when we'll update the te
' xt
n = Text1.SelStart
Else
n = 0
End If
' change text box contents according to
' item selected
byCode = True' avoids calling text1_change Event
Text1.Text = List1.List(List1.ListIndex)
byCode = False
' let's change the selected text
Text1.SelStart = n
Text1.SelLength = Len(Text1.Text) - n
End Sub
Private Sub Text1_Change()
' if we come from list1_click event, exi
' t at once
If byCode = True Then Exit Sub
If Len(Text1.Text) <> 0 Then
' show the list
List1.Visible = True
' store caret position
n = Text1.SelStart
byKey = True
' find the listindex of the first occure
' nce of text1.text in listbox1
p = SendMessage(List1.hWnd, LB_FINDSTRING, -1, ByVal Left$(Text1.Text, n))
If p >= 0 Then
If p <> List1.ListIndex Then
List1.ListIndex = p
Else
List1_Click
End If
Else
' it wasn't found in listbox1 so we don'
' t need what's after the caret anymore
byCode = True
Text1.Text = Left$(Text1.Text, n)
Text1.SelStart = n
byCode = False
End If
byKey = False
Else
' hide the list if text1.text is empty
List1.Visible = False
End If
End Sub
Private Sub Text1_DblClick()
' hides/unhides list1 on double click
' this is a very nice trick (fast too) t
' o avoid using IF x=true THEN x=false ELS
' E x=true
' remember that TRUE=-1 and FALSE=0
' X = -X - 1 switches from 0 to -1 and -
' 1 to 0 nicely :-)
List1.Visible = -List1.Visible - 1
End Sub
Private Sub Text1_Keydown(KeyCode As Integer, Shift As Integer)
' LSTEP = how many items are scrolled do
' wn/up when pressing pgup/pgdn
' a constant here but this can certainly
' be computed though
Const LSTEP = 10
Select Case KeyCode
Case vbKeyUp' move up the list
List1.ListIndex = IIf(List1.ListIndex = 0, 0, List1.ListIndex - 1)
KeyCode = 0
Case vbKeyDown ' move down the list
List1.ListIndex = IIf(List1.ListCount - 1 = List1.ListIndex, List1.ListCount - 1, List1.ListIndex + 1)
KeyCode = 0
Case vbKeyPageUp' scroll up more items
n = List1.ListIndex - LSTEP
If n < 0 Then n = 0
List1.ListIndex = n
Case vbKeyPageDown ' scroll down more items
n = List1.ListIndex + LSTEP
If n > List1.ListCount - 1 Then n = List1.ListCount - 1
List1.ListIndex = n
Case vbKeyReturn
' when Enter is pressed, make it behave
' like Tab key
SendKeys "{TAB}", True
End Select
End Sub
Private Sub Text1_LostFocus()
' focus goes somewhere else, so hide lis
' tbox
List1.Visible = False
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim vDoc, vTag
Dim i As Integer
Set vDoc = WebBrowser1.Document
List1.Clear
For i = 0 To vDoc.All.length - 1
If UCase(vDoc.All(i).tagName) = "INPUT" Then
Set vTag = vDoc.All(i)
If vTag.Type = "text" Or vTag.Type = "password" Then
List1.AddItem vTag.Name
Select Case vTag.Name
Case "Name"
vTag.Value = "IMGod"
Case "NickName"
vTag.Value = "IMGod"
Case "Password"
vTag.Value = "IMGodpass"
Case "EMail"
vTag.Value = "IMGod@paradise.com"
End Select
ElseIf vTag.Type = "submit" Then
vTag.Click
End If
End If
Next i
End Sub
点击Command1就可以自动填表并提交了。