【问题标题】:How to search label tag property in c#?如何在 C# 中搜索标签标签属性?
【发布时间】:2021-08-15 07:49:43
【问题描述】:

表格上有 6 个标签。 TAG 属性设置为 1 到 6 之间的数字。如果我单击 1 个标签,它会生成 1 到 6 之间的随机数。如何找到生成数字的标签属性? firstlbl property = 1, secondlbl property = 2, etc ... 点击 firstlbl 会生成一个数字,例如 2。如何找到标签属性为 2 的标签? 代码如下:

    private void AnyukaLbl_Click(object sender, EventArgs e)
    {
        SikeresTalalat++;
        TalalatiSzamLbl.Text = SikeresTalalat.ToString();
        Random RandomSzam = new Random();
        int KoviSzam = RandomSzam.Next(1, 6);
        MessageBox.Show(KoviSzam.ToString());
    }

【问题讨论】:

  • 您已经有了如何找到标签的答案。附注:使用RandomSzam.Next(1, 6),您将获得从 1 到 5 的整数。

标签: c# properties tags label


【解决方案1】:

在一行中,您可以这样做:

var label = this.Controls.OfType<Label>().Single( x => x.Tag == tag );

但是,如果您要一遍又一遍地这样做,我建议您将它们存储在字典中以提高效率:

var labels = this.Controls.OfType<Label>().ToDictionary( x => x.Tag, x => x );

然后你可以在字典中找到带有标签的标签:

var label = labels[tag];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 2011-06-25
    相关资源
    最近更新 更多