C# TableLayoutPanel控件的使用

由于 本人还是 小白一枚,有很多不懂,现在做项目是用到了TableLayoutPanel 这个控件,然后就去网上找学习资料,希望大佬们多多提意见给我:
好了废话不多说了,接下来说一下TableLayoutPanel这个控件吧


TableLayoutPanel是VS的原生控件,可以按下快捷键打开工具箱(ctre+alt+x)然后找到TableLayoutPanel拖到页面上

然后就可以看到是这样的,因为TableLayoutPanel多数用来动态添加删除控件,所以在winform项目中如果要用的动态的话,这个是最好了。
右上角有一个三角符号,你可以点击看看。
好了 废话不多说了 我们来说说 给它美化一下边框和单元格:

//在单元格需要重绘时发生,这个是吧单元格的线条画成红色的
   private void tableLayoutPanel_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
        {
            // 单元格重绘 
            Pen pp = new Pen(Color.Red);
            e.Graphics.DrawRectangle(pp, e.CellBounds.X, e.CellBounds.Y, e.CellBounds.X + e.CellBounds.Width - 1, e.CellBounds.Y + e.CellBounds.Height - 1);
        }


///当控件需要重绘时发生,就是把边框颜色画成红色
  private void tableLayoutPanel_Paint(object sender, PaintEventArgs e)
        {
            Pen pp = new Pen(Color.Red);
            e.Graphics.DrawRectangle(pp, e.ClipRectangle.X - 1, e.ClipRectangle.Y - 1, e.ClipRectangle.X + e.ClipRectangle.Width - 0, e.ClipRectangle.Y + e.ClipRectangle.Height - 0);
        }

当你放大或者动态添加删除数据时,控件会闪烁是吧,下面我们就来解决

这个必须放在窗体事件中 就是为了防止控件闪烁


  tableLayoutPanel1.GetType().GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).SetValue(tableLayoutPanel1, true, null);


或者你觉得麻烦了 那可以自己写一个控件:

 //TableLayoutPanel 绘制边框,防闪屏
    public class HSkinTableLayoutPanel : TableLayoutPanel
    {
        public HSkinTableLayoutPanel()
        {
            // 防止闪屏  
            this.GetType().GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).SetValue(this, true, null);
        }

        private Color borderColor = Color.Red;

        public Color BorderColor
        {
            get { return borderColor; }
            set { borderColor = value; }
        }


        protected override void OnCellPaint(TableLayoutCellPaintEventArgs e)
        {
            //绘制边框  
            base.OnCellPaint(e);
            Pen pp = new Pen(BorderColor);
            e.Graphics.DrawRectangle(pp, e.CellBounds.X, e.CellBounds.Y, e.CellBounds.X + this.Width - 1, e.CellBounds.Y + this.Height - 1);
        }  
    }

好了 就说这么多 希望大佬们多提提意见

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值