为tablelayoutpanel中的多个单元格设置背景色 [英] Set the backcolor for more than one cells in tablelayoutpanel

查看:1264
本文介绍了为tablelayoutpanel中的多个单元格设置背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此代码为tablelayoutpanel中的特定单元格设置了背景色,

I used this code to set a backcolor for a specific cell in a tablelayoutpanel,

private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
 
if (e.Column == GColumn && e.Row == GRow)//(GColumn=1 and Grow=1)
{
e.Graphics.FillRectangle(
Brushes.Pink , e.CellBounds);
}
}



它的工作,但我想选择多个单元格.因此,它应该显示多个单元的背景色.需要借助 ctrl按钮选择一个以上的单元格.

帮助我..



Its working but i want to select more than one cells. So that, it should show backcolor for more than one cells. Need to select more than one cells with the help of ctrl button.

help me..

推荐答案

您可以尝试以下方法:

You could try this:

public partial class Form1 : Form
{
    List<TableLayoutCellConfig> tlcc = new List<TableLayoutCellConfig>();
    
    public Form1()
    {
        InitializeComponent();
        tlcc.Add(new TableLayoutCellConfig(0, 0, Brushes.Green));
        tlcc.Add(new TableLayoutCellConfig(0, 1, Brushes.Red));
        tlcc.Add(new TableLayoutCellConfig(1, 0, Brushes.Lime));
        

    }
    
    private void TableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
    {
        foreach (TableLayoutCellConfig conf in tlcc)
        {
            if (e.Row == conf.Row && e.Column==conf.Column)
            {
                e.Graphics.FillRectangle(conf.Brush, e.CellBounds);
                continue;
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        tlcc.Add(new TableLayoutCellConfig(1, 1, Brushes.Pink));
        tableLayoutPanel1.Invalidate();
    }

    
}

class TableLayoutCellConfig
{
    public TableLayoutCellConfig(int X, int Y, Brush Background)
    {
        this.Column = X;
        this.Row = Y;
        this.Brush = Background;
    }
    
    private int row;
    public int Row
    {
        get { return row; }
        set { row = value; }
    }

    private int column;
    public int Column
    {
        get { return column; }
        set { column = value; }
    }

    private Brush brush;
    public Brush Brush
    {
        get { return brush; }
        set { brush = value; }
    }


}



使用



With

List<TableLayoutCellConfig>

,您可以在

TableLayoutPanel1_CellPaint


期间遍历您的配置
不要忘记更改配置后使之失效.



Don''t forget to invalidate after changing config.


这篇关于为tablelayoutpanel中的多个单元格设置背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆