Winforms下使用TableLayoutPanel进行布局,并解决闪烁及平均列宽与平均行高问题

TableLayoutPanel控件,可以类似于在web下面进行表格布局,最近正好需要,使用了感觉,还是比较简单,但是有一个严重的问题。就是在更改布局时,闪烁严重。通过Google得知,可能修改DoubleBuffered属性,解决这个问题,但是DoubleBuffered是继承可见,这时只好用反射来处理了。

TableLayoutPanel1.GetType().GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic).SetValue(TableLayoutPanel1, True, Nothing)

  

Demo代码片断

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click


        Me.TableLayoutPanel1.RowCount = 1
        Me.TableLayoutPanel1.ColumnCount = 1
        Me.TableLayoutPanel1.Controls.Clear()

        Dim value As String = InputBox("输入控件数量:", "提示", "4")
        If String.IsNullOrWhiteSpace(value) Then
            Return
        End If

        Dim count As Int32 = CInt(value)


        '设置最大列数
        Dim maxColumnt As Int32 = 4

        If count > maxColumnt * 2 Then
            MessageBox.Show("不能超过" & maxColumnt * 2 & "个。")
            Return
        End If



        '判断是否需要重起一行
        If count > maxColumnt Then
            '重新起一行
            Me.TableLayoutPanel1.RowCount = 2
            Me.TableLayoutPanel1.ColumnCount = maxColumnt
        Else
            Me.TableLayoutPanel1.ColumnCount = count
        End If

        For c As Int32 = 0 To Me.TableLayoutPanel1.ColumnCount - 1
            Me.TableLayoutPanel1.ColumnStyles.Add(New ColumnStyle())
        Next

        '添加控件
        For r As Int32 = 0 To Me.TableLayoutPanel1.RowCount - 1

            Me.TableLayoutPanel1.RowStyles.Add(New RowStyle())


            For c As Int32 = 0 To Me.TableLayoutPanel1.ColumnCount - 1
                Dim g As New GroupBox
                g.Text = r.ToString & " " & c.ToString
                g.Dock = DockStyle.Fill
                g.Width = 10
                Me.TableLayoutPanel1.Controls.Add(g, c, r)
            Next

        Next

        TableLayouResize()

    End Sub

    '自动大小
    Private Sub TableLayouResize()


        '反射解决,闪烁问题。
        TableLayoutPanel1.GetType().GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic).SetValue(TableLayoutPanel1, True, Nothing)
        For r As Int32 = 0 To Me.TableLayoutPanel1.ColumnCount - 1
            Me.TableLayoutPanel1.ColumnStyles(r).SizeType = SizeType.Absolute
            Me.TableLayoutPanel1.ColumnStyles(r).Width = CInt(Me.TableLayoutPanel1.Width / Me.TableLayoutPanel1.ColumnCount)
        Next

        For r As Int32 = 0 To Me.TableLayoutPanel1.RowCount - 1
            Me.TableLayoutPanel1.ColumnStyles(r).SizeType = SizeType.Absolute
            Me.TableLayoutPanel1.RowStyles(r).Height = CInt(Me.TableLayoutPanel1.Height / Me.TableLayoutPanel1.RowCount)
        Next

    End Sub

    Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
        TableLayouResize()
    End Sub

  使用了一下效果,还行,当然,还有TableLayoutPanel合并之类的,还没有使用。

posted on 2011-11-28 10:40 zqonline 阅读(...) 评论(...) 编辑 收藏

导航