2

我编写了一些代码来在“拖动模式”下拖动表单上的任何文本框并调整其大小这是发生了什么的 gif,而不是文本框正确拖动,

图像

代码:

#Region "Texbox Dragging"
    Private txt As TextBox
    Private txtptX, txtptY As Integer
    Private txtdrag As Boolean
    Private txtresize As Boolean
    Private Sub txt_MouseLeave(sender As Object, e As EventArgs)
  Me.Cursor = Cursors.Arrow
    End Sub

    Private Sub txt_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
  If DragMode = True Then
    If e.Button = MouseButtons.Left Then
    txtdrag = True
    txtresize = True
    txt = CType(sender, TextBox)
    txtptX = e.X : txtptY = e.Y
    End If
  End If
    End Sub

    Private Sub txt_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

  If txtdrag = True Then
    txt.Location = New Point(txt.Location.X + e.X - txtptX, txt.Location.Y + e.Y - txtptY)
    Me.Refresh()
    txtdrag = True
  End If
  If txtresize = True Then
    txtdrag = False
    If txt.Cursor = Cursors.Cross Then
    txt.Width = e.X
    txt.Height = e.Y
    Else
    If e.X >= txt.Width - 10 Then
    txt.Cursor = Cursors.Cross
    Else
    txt.Cursor = Cursors.IBeam
    End If

    If e.Y >= txt.Height - 10 Then
    txt.Cursor = Cursors.Cross
    Else
    txt.Cursor = Cursors.IBeam
    End If
    End If
  End If
    End Sub

    Private Sub txt_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
  txt = CType(sender, TextBox)
  If txt.Cursor = Cursors.Cross Then
    txt.Cursor = Cursors.IBeam
  End If

  txtdrag = False
  txtresize = False
    End Sub
#End Region

我为自己是一个凌乱的编码员而道歉,但这是关于第 12 次尝试,我正在尽一切努力让它发挥作用......

  • 它们都可以独立工作,但是我遇到了这个奇怪的错误,当它们在一起时我只能拖动 0.25 秒...
4

2 回答 2

2

我做的!在向我自己大声阅读代码并遵循流程之后,我意识到您的说法是正确的,我不能在 MouseMove 事件中同时包含拖动和调整大小。但是:-),我可以在 MouseDown 事件中同时拥有两者,而在 on 发生时,另一个不能被触发,它完美地工作。下面是实际代码的 .gif 文件以及代码!

.gif 没有像我应该的那样录制,你只需要相信我的话哈哈。

#Region "Texbox Dragging"
Private txt As TextBox
Private txtptX, txtptY As Integer
Private Sub txt_MouseLeave(sender As Object, e As EventArgs)
    Me.Cursor = Cursors.Arrow
End Sub
Dim MoveMode As Boolean
Private Sub txt_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    If DragMode = True Then
        MoveMode = True
        If e.Button = MouseButtons.Left Then
            txt = CType(sender, TextBox)
            txtptX = e.X : txtptY = e.Y
            If e.X >= txt.Width - 10 Then
                txt.Cursor = Cursors.Cross
            Else
                txt.Cursor = Cursors.IBeam
            End If

            If e.Y >= txt.Height - 10 Then
                txt.Cursor = Cursors.Cross
            Else
                txt.Cursor = Cursors.IBeam
            End If
        End If
    End If
End Sub

Private Sub txt_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    If MoveMode = True Then
        If txt.Cursor = Cursors.Cross Then
            txt.Width = e.X
            txt.Height = e.Y
        Else
    txt.Location = New Point(txt.Location.X + e.X - txtptX, txt.Location.Y + e.Y - txtptY)
            Me.Refresh()
        End If
    End If
End Sub

Private Sub txt_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    txt = CType(sender, TextBox)
    If txt.Cursor = Cursors.Cross Then
        txt.Cursor = Cursors.IBeam
    End If
    MoveMode = False
End Sub
#End Region
于 2013-08-06T02:18:42.343 回答
1

两个动作(调整大小和移动)不能同时执行(在MouseMove方法中):拖动部分(顶部的那个)与调整大小的部分重叠。另一方面,您不希望这两个动作同时发生(用户如何能够通过单击和移动来处理调整大小和重新定位?)。您必须设置进一步的条件以允许两个功能并行工作;例如:双击txt

全局标志:

Dim nowDragging As Boolean = True

双击事件为这些事件分配正确的值:

Private Sub txt_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles txt.MouseDoubleClick
    nowDragging = Not nowDragging
End Sub

更新MouseMove以说明它们:

If txtdrag = True And nowDragging Then 
'...
If txtresize = True And Not nowDragging Then
'..

此代码默认执行拖动;如果用户双击txt,此功能将转换为调整大小(等等)。这是一种简单的方法,只需了解基本思想:启用一种从一种功能转移到另一种功能的方法。

PS:显示您的确切问题的好方法。

于 2013-07-29T08:16:18.523 回答