根据原代码写了一个Module
这对多个 tablelayoutpanel 使用字典和 4 个事件处理程序来重新调整其行和列的大小。
那里有一些代码可以捕捉屏幕上的移动物体,但不多。
阅读代码中的注释以获取更多信息
Module modResizeableTableLayoutPanel
Private otlpdic As Dictionary(Of String, Integer()) = Nothing
''' <summary>
''' Adds the needed handlers for resizing the rows and columns of multiple
''' tablelayoutpanel uses the tag property
''' also requires primary controls in the cells to have a margin of 6... this is
''' a hack to detect when the mouse
''' is no longer over a divider and not over a control in the cell
''' All rows and columns require absolute positioning
''' </summary>
''' <param name="otlp">tablelayoutpanel</param>
''' <remarks></remarks>
Public Sub InitializeResizeableTableLayOutPanel(ByRef otlp As TableLayoutPanel)
'create has if needed
Call CreateTableLayoutPanelHash()
'creat hash entry for TableLayoutPanel
Call CreateHashEntryForTableLayoutPanel(otlp)
'sets the border style to something that helps the mouse cursor detect.
otlp.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single
'hack to set margins on sub controls
For Each con In otlp.Controls
Select Case con.GetType
Case GetType(FlowLayoutPanel)
Dim obj As FlowLayoutPanel = DirectCast(con, FlowLayoutPanel)
obj.Margin = New Padding(6)
Case GetType(DataGridView)
Dim obj As DataGridView = DirectCast(con, DataGridView)
obj.Margin = New Padding(6)
Case GetType(Button)
Dim obj As Button = DirectCast(con, Button)
obj.Margin = New Padding(6)
Case GetType(GroupBox)
Dim obj As GroupBox = DirectCast(con, GroupBox)
obj.Margin = New Padding(6)
End Select
Next
'add event handlers
AddHandler otlp.MouseDown, AddressOf tlp_MouseDown
AddHandler otlp.MouseUp, AddressOf tlp_MouseUp
AddHandler otlp.MouseMove, AddressOf tlp_MouseMove
AddHandler otlp.MouseLeave, AddressOf tlp_MouseLeave
End Sub
''' <summary>
''' Clear the dictionary/hashtable that keeps track of needed variables to control the resizing
''' </summary>
''' <remarks></remarks>
Public Sub ResetResizeableTableLayOutPanelData()
otlpdic.Clear()
otlpdic = Nothing
Call CreateTableLayoutPanelHash()
End Sub
Private Sub CreateTableLayoutPanelHash()
If otlpdic Is Nothing Then
otlpdic = New Dictionary(Of String, Integer())
End If
End Sub
Private Sub CreateHashEntryForTableLayoutPanel(ByRef otlp As TableLayoutPanel)
If Not otlp.Tag Is Nothing AndAlso otlpdic.ContainsKey(otlp.Tag) Then
Else
Dim tmpguid As Guid = Guid.NewGuid
otlp.Tag = tmpguid.ToString
otlpdic.Add(otlp.Tag, {0, -1, -1})
End If
End Sub
Public Sub tlp_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs)
If e.Button = System.Windows.Forms.MouseButtons.Left Then
Dim otlp As TableLayoutPanel = DirectCast(sender, TableLayoutPanel)
Call CreateHashEntryForTableLayoutPanel(otlp)
Dim tmpdata As Integer() = otlpdic.Item(otlp.Tag)
tmpdata(0) = -1
End If
End Sub
Public Sub tlp_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs)
Dim otlp As TableLayoutPanel = DirectCast(sender, TableLayoutPanel)
Call CreateHashEntryForTableLayoutPanel(otlp)
Dim tmpdata As Integer() = otlpdic.Item(otlp.Tag)
'Dim rowStyles =
'columnStyles = TableLayoutPanel1.ColumnStyles
If Not tmpdata(0) Then
Dim width As Double = 0
Dim height As Double = 0
'for rows
For i As Integer = 0 To otlp.RowStyles.Count - 1
height += otlp.RowStyles(i).Height
If (e.Y > height - 2 And e.Y < height + 2) Then
tmpdata(1) = i
otlp.Parent.Cursor = Cursors.HSplit
Exit For
Else
tmpdata(1) = -1
otlp.Parent.Cursor = Cursors.Default
End If
Next
'for columns
For i As Integer = 0 To otlp.ColumnStyles.Count - 1
width += otlp.ColumnStyles(i).Width
If e.X > width - 2 And e.X < width + 2 Then
tmpdata(2) = i
If (tmpdata(1) > -1) Then
otlp.Parent.Cursor = Cursors.Cross
Else
otlp.Parent.Cursor = Cursors.VSplit
Exit For
End If
Else
tmpdata(2) = -1
If (tmpdata(1) = -1) Then
otlp.Parent.Cursor = Cursors.Default
End If
End If
Next
End If
If (tmpdata(0) And (tmpdata(2) > -1 Or tmpdata(1) > -1)) Then
Dim Width As Double = e.X
Dim Height As Double = e.Y
If e.X > otlp.Width - 100 Then
Exit Sub
End If
If e.Y > otlp.Height - 100 Then
Exit Sub
End If
If (tmpdata(2) > -1) Then
For i As Integer = 0 To tmpdata(2) - 1
Width -= otlp.ColumnStyles(i).Width
Next
otlp.ColumnStyles(tmpdata(2)).SizeType = SizeType.Absolute
If Width < 10 Then
Width = 10
End If
If Width > otlp.Width - 100 Then
Width = otlp.Width - 100
End If
otlp.ColumnStyles(tmpdata(2)).Width = Width
End If
If (tmpdata(1) > -1) Then
For i As Integer = 0 To tmpdata(1) - 1
Height -= otlp.RowStyles(i).Height
Next
otlp.RowStyles(tmpdata(1)).SizeType = SizeType.Absolute
If Height < 10 Then
Height = 10
End If
If Height > otlp.Height - 100 Then
Height = otlp.Height - 100
End If
otlp.RowStyles(tmpdata(1)).Height = Height
End If
End If
End Sub
Public Sub tlp_MouseLeave(sender As Object, e As System.EventArgs)
Dim otlp As TableLayoutPanel = DirectCast(sender, TableLayoutPanel)
otlp.Parent.Cursor = Cursors.Default
End Sub
Public Sub tlp_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs)
If e.Button = System.Windows.Forms.MouseButtons.Left Then
Dim otlp As TableLayoutPanel = DirectCast(sender, TableLayoutPanel)
Dim tmpdata As Integer() = otlpdic.Item(otlp.Tag)
tmpdata(0) = 0
otlp.Parent.Cursor = Cursors.Default
End If
End Sub
End Module