【问题标题】:Handling up- and down-arrow keys in a ListBox in VB6在 VB6 中处理 ListBox 中的向上和向下箭头键
【发布时间】:2010-11-26 21:36:19
【问题描述】:
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 的单击事件。 ListBox 包含药品名称并绑定到数据库,因此我想在用户单击 ListBox 时调用 Click 事件但不是自动的。

【问题讨论】:

  • 您不必自己选择列表项 - 向上箭头和向下箭头将更改列表框的选择,并为您处理溢出/下溢,只要它具有焦点。这实际上是一个焦点问题吗?

标签: vb6 listbox keyboard


【解决方案1】:

您可以在单击向上/向下按钮时将标志“isUpDownClicked”设置为 true,并在您的 Sub List_Click 中当标志为 true 时退出 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

【讨论】:

    猜你喜欢
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多