【问题标题】:How to access each field in a Winform TableLayoutPanel in C#如何在 C# 中访问 Winform TableLayoutPanel 中的每个字段
【发布时间】:2020-06-18 11:52:08
【问题描述】:

我在Winforms 中有一个TableLayoutPanel,每个字段只包含一个标签。我现在需要获取行/列以及该字段中的内容。

f.E:我必须检查第一行中的标签是否都具有相同的文本。

我该怎么做?

【问题讨论】:

标签: c# winforms tablelayoutpanel


【解决方案1】:

我必须检查第一行中的标签是否都具有相同的文本。

在循环中使用TableLayoutPanel.GetControlFromPosition...类似:

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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多