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

时间:2016-05-18 09:18:30

标签: vb.net winforms drag-and-drop double-click

我正在努力使用我想要在 Windows窗体上使用的某些功能。 (仅供参考,这适用于AutoDesk Inventor AddIn。)

这是我的表单布局。 form layout

当前工作流程

前4个列表框中填充了可用的参数名称。用户选择他/她想要使用的参数并将其拖放到其中一个驾驶参数文本框中(标有<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列表框中的项目,并且该项目应该添加到第一个空文本框中。我用一个数字命名我的文本框,所以很容易遍历它们以检查它是否为空。

我尝试使用此代码,但我的双击事件从不被触发。它总是去拖放。你如何处理这个问题,双击被激活而不是拖放?

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

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

1 个答案:

答案 0 :(得分:1)

Mousedown会触发DoDragDrop,从而停止双击事件。

要确定用户是否双击或想要执行拖放操作,请考虑以下事项:

{{1}}