【问题标题】:vb.net add values from listbox to textbox via drag&drop AND via double clickvb.net 通过拖放和双击将列表框中的值添加到文本框
【发布时间】:2016-09-14 16:22:58
【问题描述】:

我想在我的 Windows 窗体 上使用一些功能。 (仅供参考,这是针对 AutoDesk Inventor 插件的。)

这是我的表单布局。

当前工作流程

前 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

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

【问题讨论】:

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


    【解决方案1】:

    Mousedown 触发 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
    

    【讨论】:

    • 嗨!感谢您的快速回复!这个答案有效,但我想知道,还有其他可能性吗?我发现了这个,但是使用msdn.microsoft.com/en-us/library/ms171543%28v=vs.110%29.aspx 更复杂
    • 它的作用完全一样。它使用“标志”来评估它是第一次还是第二次点击 (isFirstClick) 以及间隔 (SystemInformation.DoubleClickTime)。这两种方法也都在上面的 MouseEventArgs 中。如果太麻烦:考虑使用“向下箭头”键作为拖放替代方案?用户选择项目,按向下箭头,然后它出现在下方。
    • 更正:向下箭头表示双击选项。我同意它不像双击那样用户友好,但人们(至少是那些使用我的程序的人)通常不会抱怨这些变通方法。
    • 感谢您的额外输入,双击是来自最终用户的请求,所以我的目标是它。 Down key,是个不错的选择!祝你有美好的一天!
    猜你喜欢
    • 1970-01-01
    • 2011-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多