如何实现按钮拖放到TableLayoutPanel
											请教:如何实现按钮或其他控件拖放到TableLayoutPanel中的任何格中?谢谢
	
		
			 Drop.zip
				(72.05 KB)
Drop.zip
				(72.05 KB)
				
				
			 程序代码:
程序代码:
    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
										
					
	
 程序代码:
程序代码:
'环境: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										
					
	