【问题标题】:move form when panel is clicked vb.net单击面板时移动表单vb.net
【发布时间】:2020-05-25 15:49:14
【问题描述】:

我有一个没有边框样式的 form1,我想让这个表单在用户移动 panel1 时移动。 在进行简单的搜索后,我发现这个解决方案 C# 的情况相同

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
    this.Location = new Point(Cursor.Position.X + e.X , Cursor.Position.Y + e.Y);
}
}

对于我的情况,我使用 vb.net,所以我尝试制作类似的代码但我失败了:

Private Sub Panel1_MouseMove(sender As Object, e As MouseEventArgs) Handles GunaPanel1.MouseMove
    If e.Button = MouseButtons.Left Then
        MainDashboard.Location = New Point(Cursor.Position.X + e.X, Cursor.Position.Y + e.Y)
    End If
End Sub

【问题讨论】:

  • 你哪里失败了?使用 c# 到 vb 转换器
  • 当我将它转换为 vb.net 时它不起作用
  • 什么是 MainDashboard?是表格名称吗?
  • 是的,这是我的表单名称
  • 这是面板所在的当前表单名称?

标签: vb.net panel


【解决方案1】:

这里是解决方案: MouseIsDown 和 MouseIsDownLoc 是模块级变量。

    Private MouseIsDown As Boolean = False
    Private MouseIsDownLoc As Point = Nothing
    Private Sub Panel1_MouseMove(sender As Object, e As MouseEventArgs) Handles Panel1.MouseMove

        If e.Button = MouseButtons.Left Then
            If MouseIsDown = False Then
                MouseIsDown = True
                MouseIsDownLoc = New Point(e.X, e.Y)
            End If

            Me.Location = New Point(Me.Location.X + e.X - MouseIsDownLoc.X, Me.Location.Y + e.Y - MouseIsDownLoc.Y)
        End If
    End Sub
    Private Sub Panel1_MouseUp(sender As Object, e As MouseEventArgs) Handles Panel1.MouseUp
        MouseIsDown = False
    End Sub

【讨论】:

  • 是的,我在它不起作用之前尝试过,因为当您按住面板并移动表单时,表单会消失并出现多次
  • 您想移动该表单上的面板吗?如果是,请使用 Panel1 更改 Me。
  • 其实我想移动表单而不是面板
  • 你的意思是当用户按下面板上的左键并移动面板时,表单应该移动并且面板位置相对于表单将保持不变?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-23
相关资源
最近更新 更多