简体   繁体   中英

How to access each field in a Winform TableLayoutPanel in C#

I have a TableLayoutPanel in Winforms , each field contains exactly one label. I now need to get the row/colum and also what is in that field.

fE: I have to check if the lables in the first row all have the same text.

How can I do that?

I have to check if the lables in the first row all have the same text.

Use TableLayoutPanel.GetControlFromPosition in a loop...something like:

private void button1_Click(object sender, EventArgs e)
{
    bool matching = RowMatches(0);
    Console.WriteLine(matching);
}

private bool RowMatches(int row)
{
    string value = null;
    for(int col=0; col<tableLayoutPanel1.ColumnCount; col++)
    {
        Label lbl = (Label)tableLayoutPanel1.GetControlFromPosition(col, row);
        if (value == null)
        {
            value = lbl.Text;
        }
        else if (lbl.Text != value)
        {
            return false;
        }
    }
    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM