2011年11月13日 星期日

#VB TableLayoutPanel 為表單做排版


楼主用TableLayout+Lable来做表格是一种不明智的做法。
你可以选择ListView或是DataGridView 来做,操作更方便,效果更好。



或是類似datagridview的雙緩衝


[WinForm] 如何使 控制項 不閃爍 /How to use double buffer for controls




tableLayoutPanel.DoubleBuffered=true
不過DoubleBuffered是Protected 屬性,所以需要要使用反射來處理,請參考[C#.NET][VB.NET] 如何 列舉 類別中的成員 / Type.GetMembers


PropertyInfo info = this.GetType().GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
info.SetValue(tableLayoutPanel1, true, null);



'建立程式碼控制項時,將雙緩衝屬性寫進去
    Private Sub TableLayoutPanel5_HandleCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles TableLayoutPanel5.HandleCreated

        '設定雙緩衝
        Dim PropertyInfo As System.Reflection.PropertyInfo = GetType(TableLayoutPanel).GetProperty("DoubleBuffered", Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
        PropertyInfo.SetValue(TableLayoutPanel5, True, Nothing)
    End Sub



==========================================================================



I found a combination of the setting DoubleBuffer = true helped on the form resizing flickering.

(C++/CLR code)
ref class MyTableLayoutPanel : public TableLayoutPanel
{
public:
MyTableLayoutPanel()
{
DoubleBuffered = true;
}
};

Also, to prevent flickering during a redraw operation - like deleting a row in the table, I do a SuspendLayout on the both the layout panel and the form before doing changes to the items.

myTableLayout->Parent->SuspendLayout();
myTableLayout->SuspendLayout();

// Do your stuff here
myTableLayout->Parent->ResumeLayout();
myTableLayout->ResumeLayout();


==============================================================

TableLayoutPanel 控制項概觀


該如何動態加Control和可以設定誇行跨列


TableLayoutPanel.SetColumnSpan 方法



有點效能上的問題

可能需要改 GDI 去繪製框線以及內容

使用TableLayoutPanel控件寫的任務列表控件,如何改善(WINFORM)
http://tc.0728.org/News/229515.html

用GDI+沒錯,我做過類似EXECL的表格,能放大縮小,能編輯,非常快,看不出延時。
 private void Form1_Paint(object sender, PaintEventArgs e) {
   Graphics g = e.Graphics;
 // 第一步,畫表格線
   System.Drawing.Pen myPen; myPen = new 
   System.Drawing.Pen(System.Drawing.Color.Gray);
   g.DrawLine(myPen, 10, 10, 200, 10);
   g.DrawLine(myPen, 10, 30, 200, 30);
   g.DrawLine(myPen, 10, 50, 200, 50);
   myPen.Dispose();


 // 第二步,填文字
   System.Drawing.Font drawFont = new System.Drawing.Font("宋體", 10);
   System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
   string drawString = "張三";
   g.DrawString(drawString, drawFont, drawBrush, 14, 16);
   drawString = "李四";
   g.DrawString(drawString, drawFont, drawBrush, 14, 36);
   drawFont.Dispose();
   drawBrush.Dispose();

沒有留言:

張貼留言