注册 登录
编程论坛 VB.NET论坛

如何实现按钮拖放到TableLayoutPanel

yuk_yu 发布于 2017-08-03 10:27, 4866 次点击
请教:如何实现按钮或其他控件拖放到TableLayoutPanel中的任何格中?谢谢
只有本站会员才能查看附件,请 登录
9 回复
#2
yuk_yu2017-08-03 15:44
期待大家指点!!
#3
yuk_yu2017-08-04 16:11
请大家指点指点!!
#4
yuk_yu2017-08-08 11:22
兄弟姐妹们,我提的问题是不是不能实现啊?期待大家给一个答案!谢谢
#5
yuk_yu2017-08-29 11:18
有谁能指点指点吗?谢谢
#6
yuk_yu2017-09-11 16:08
真的无法实现我的要求吗?好期待大家给个答案,谢谢
#7
不说也罢2017-09-17 18:22
楼主的版本高,我的.net版本暂时打不开你的项目
能详细描述下么?在窗口中拖动一个按钮不是问题吧?下面的是在窗体上拖动一个按钮到任意位置的例子:

程序代码:

    Private nLocation As Point

    Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
        nLocation = e.Location
    End Sub

    Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove
        Dim pos_x, pos_y As Int32
        If e.Button = MouseButtons.Left Then
            pos_x = Button1.Location.X + (e.X - nLocation.X)
            pos_y = Button1.Location.Y + (e.Y - nLocation.Y)
            Button1.Location = New Point(pos_x, pos_y)
        End If
    End Sub

#8
yuk_yu2017-09-18 14:08
回复 7楼 不说也罢
版主,我想实现的就是能将一个控件拖动,并自动铺满
只有本站会员才能查看附件,请 登录
#9
不说也罢2017-09-23 20:27
这里粗略地写了一个例子,实在没有时间,你琢磨一下吧。基本上可以实现你要的功能。只是计算上有些误差。
程序代码:

'环境:VS2008
'
你测试时,需要在窗体上画一个button,其他什么也不用做。
'
当然,你制作时,可以参考下面的例子,考虑动态加载你的button

Public Class Form3
    Private nLocation As Point
    Private TableLayoutPanel() As TableLayoutPanel
    Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
        nLocation = e.Location
        With Button1
            If .Parent Is Me Then Exit Sub
            .Location = .Parent.Location
            .Width = .Parent.Width - 10
            .Height = .Parent.Height - 10
            .Parent = Me
            .Dock = DockStyle.None
            .BringToFront()
        End With
    End Sub

    Private Sub Button1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseMove
        Dim pos_x, pos_y As Int32
        If e.Button = MouseButtons.Left Then
            pos_x = Button1.Location.X + (e.X - nLocation.X)
            pos_y = Button1.Location.Y + (e.Y - nLocation.Y)
            Button1.Location = New Point(pos_x, pos_y)
        End If
    End Sub

    Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
        For i As Integer = TableLayoutPanel.Count - 1 To 0 Step -1
            If Button1.Left > TableLayoutPanel(i).Left And Button1.Top > TableLayoutPanel(i).Top Then
                Button1.Parent = TableLayoutPanel(i)
                Button1.Dock = DockStyle.Fill
                Exit For
            End If
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        Dim i, num As Integer
        Dim nTop As Long = 0, nLeft As Long = 0
        num = 100

        ReDim TableLayoutPanel(0 To num - 1) '根据输入的指定数值定义TableLayoutPanel对象

        nTop = 200 '从这里开始画TableLayoutPanel
        For i = 0 To num - 1
            Application.DoEvents() '设置程序友好响应
            TableLayoutPanel(i) = New TableLayoutPanel
            Controls.Add(TableLayoutPanel(i))
            With TableLayoutPanel(i)
                .SetBounds(nLeft, nTop, 50, 50) '将标签的高度设为50,宽度设为50
                .BorderStyle = BorderStyle.FixedSingle
                .Tag = i '这个属性用来识别每个TableLayoutPanel对象
                .Visible = True
                '为即将创建的下一个TableLayoutPanel设置坐标
                nLeft += .Width - 1   '将标签的左右间距设为10
                If nLeft + .Width >= Me.Width Then '如果下一个TableLayoutPanel将超出窗体的宽度,换行显示
                    nLeft = 0
                    nTop += .Height - 1 '标签的行间距
                End If
            End With
        Next i
    End Sub

End Class
#10
yuk_yu2017-09-27 08:54
回复 9楼 不说也罢
谢谢版主,正是我想要的效果!!再次感谢
1