在VB6中处理ListBox中的上箭头键和下箭头键

时间:2010-11-26 13:18:34

标签: vb6 listbox keyboard

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

我的项目中有一个名为subkeydown()的函数(见上文),当用户按下向上箭头键或向下箭头键时调用该函数。调用该函数时,会触发ListBox的click事件。 ListBox包含医药产品名称并绑定到数据库,因此我想在用户单击ListBox但不自动时调用Click事件。

1 个答案:

答案 0 :(得分:0)

单击向上/向下按钮时,可以将标记“isUpDownClicked”设置为true,并在标记为true时在子List_Click中退出sub,如:

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