开发者

Handling up- and down-arrow keys in a ListBox in VB6

Public Sub subkeydown(txt As TextBox, lst As ListBox, KeyCode As Integer)

  On Error Resume Next

  lstfstrec = True

  If txt.Text = "" Then lst.Visible = False: Exit Sub
  If KeyCode = 40 Then
    lst.Selected(lst.ListIndex + 1) = True ': Exit Sub
    'MsgBox lstMedicine.ListIndex
  End If
  If KeyCode = 38 Then lst.Selected(lst.ListIndex - 1) = True ': Exit Sub

End Sub

I have a function nam开发者_StackOverflowed subkeydown() in my project (see above), which is called when the user presses the up-arrow or down-arrow keys. When the function is called, the ListBox's click event is fired. The ListBox contains medicine product name and is bound to a database, so I want to call the Click event when the user clicks on the ListBox but not automatically.


You can set a flag "isUpDownClicked" to true when clicking the up/down button, and in your Sub List_Click you exit sub when flag is true, like:

Option Explicit
Dim lstfstrec As Boolean
Dim isUpDownClicked As Boolean

Public Sub subkeydown(txt As TextBox, lst As ListBox, KeyCode As Integer)

    On Error Resume Next

    lstfstrec = True

    If txt.Text = "" Then lst.Visible = False: Exit Sub
    If KeyCode = 40 Then
      lst.Selected(lst.ListIndex + 1) = True ': Exit Sub
      'MsgBox lstMedicine.ListIndex
    End If
    If KeyCode = 38 Then lst.Selected(lst.ListIndex - 1) = True 
End Sub

Private Sub CommandUp_Click()
    isUpDownClicked = True
    subkeydown Text1, lstMedicine, 38
End Sub
Private Sub CommandDown_Click()
    isUpDownClicked = True
    subkeydown Text1, lstMedicine, 40
End Sub

Private Sub Form_Load()
    lstMedicine.AddItem "1"
    lstMedicine.AddItem "2"
    lstMedicine.AddItem "3"
End Sub

Private Sub lstMedicine_Click()
    If isUpDownClicked Then
        isUpDownClicked = False
        Label1.Caption = "no"
        Exit Sub
    End If

    Label1.Caption = "lst_Click"
End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜