-1

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.

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

How can I do that?

baltermia
  • 1,151
  • 1
  • 11
  • 26
  • [Determine the Cell of a Table Layout Panel Controls are Contained in](https://stackoverflow.com/a/50414129/7444103) (VB.Net code, exactly the same in C# of course). See also [this](https://stackoverflow.com/a/59666527/7444103) (implemented, in C# this time) – Jimi Jun 18 '20 at 12:01
  • Or [this](https://stackoverflow.com/a/54565075/7444103), a little more complex, but you can find a couple of methods that may come in handy (how to remove a Row from a TLP without generating layout *discrepancies*). Also described more in depth here: [Remove Row inside TableLayoutPanel makes a layout problem](https://stackoverflow.com/a/55225457/7444103). – Jimi Jun 18 '20 at 12:06

1 Answers1

1

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;
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40