vb.net 通过拖放和双击将列表框的值添加到文本框

vb.net add values from listbox to textbox via drag&drop AND via double click

我正在为一些我想在 Windows 表单 上使用的功能而苦苦挣扎。 (仅供参考,这是针对 AutoDesk Inventor 插件的。)

这是我的表单布局。

当前工作流程

前 4 个列表框填满了可用的参数名称。用户选择 he/she 想要使用的参数并将其拖放到驱动参数文本框之一(标有 <1> 标签)。

与拖放操作相关的代码

Private Sub lstTemp_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles lbModelParameters.MouseDown,
    lbUserParameters.MouseDown,
    lbReferenceParameters.MouseDown,
    lbLinkedParameters.MouseDown

    ' In order to access a specific item in a listbox.itemcollection, you must think of it
    ' as an array  of data or a collection and access it in the same manner by always
    ' letting VB know which item you intend to use by identifying it with its index location
    ' within the collection. And this is better than taking up basket weaving :-)
    lbModelParameters.DoDragDrop(sender.Items(sender.SelectedIndex()).ToString, DragDropEffects.Move)
End Sub

Private Sub txtTemp_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
    Handles tbParameter1.DragEnter,
    tbParameter2.DragEnter,
    tbParameter3.DragEnter,
    tbParameter4.DragEnter,
    tbParameter5.DragEnter

    'Check the format of the incoming data and accept it if the destination control is able to handle
    '  the data format

    'Data verification
    If e.Data().GetDataPresent(DataFormats.Text) Then
        e.Effect() = DragDropEffects.Move
    Else
        e.Effect() = DragDropEffects.None
    End If

End Sub

Private Sub txtTemp_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
    Handles tbParameter1.DragDrop,
    tbParameter2.DragDrop,
    tbParameter3.DragDrop,
    tbParameter4.DragDrop,
    tbParameter5.DragDrop
    'This procedure receives the dragged data once it passes the data verification handled by the DragEnter method

    'Drops the data onto the destination control
    sender.Text() = e.Data().GetData(DataFormats.Text).ToString()

End Sub

新功能

出于人体工程学原因和速度,现在我想减少用户鼠标的移动。但我也想保留拖放功能。因为它可以覆盖用户已经添加的值。

我希望能够 DoubleClick 列表框中的一个项目,该项目应该添加到第一个空文本框中。我用一个数字命名了我的文本框,这样很容易遍历它们以检查它是否为空。

我试着用这段代码来做,但是我的双击事件 never 被触发了。它总是拖放。你如何处理这个问题,即触发双击而不是拖放?

Private Sub ParameterAddDoubleClick(sender As Object, e As EventArgs) _
    Handles lbModelParameters.DoubleClick,
    lbUserParameters.DoubleClick,
    lbReferenceParameters.DoubleClick,
    lbLinkedParameters.DoubleClick

    Dim oControl As Windows.Forms.ListBox
    oControl = DirectCast(sender, Windows.Forms.ListBox)

    ' Add line in likedparameters listbox
    If oControl.SelectedIndex <> -1 Then

        ' Loop trough all the controls to see if one is empty
        ' if it's empty add parameter, else go to next
        ' if all textboxes are used do nothing.
        For i = 1 To 6

            Dim oTextbox As Windows.Forms.TextBox =
            CType(gbDrivingParameters.Controls("tbParameter" & i),
            Windows.Forms.TextBox)

            If oTextbox.TextLength = 0 Then
                ' Add the sender item into the linked listbox
                oTextbox.Text = oControl.Items.Item(oControl.SelectedIndex)
            End If
        Next
    End If
End Sub

我希望我的问题很清楚,并且准备充分。如果需要其他信息,请在评论中告诉我。

鼠标按下触发 DoDragDrop,从而停止触发双击事件。

要确定用户是双击还是要执行拖放操作,请考虑以下因素:

Private Sub ListBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseDown
    ' Determine whether we are performing a drag operation OR a double click
    If e.Clicks = 1 Then
        TextBox1.Text = "mousedown"
    Else
        TextBox1.Text = "dblclick"
    End If
End Sub