简体   繁体   English

如何给字典添加标签 Windows Forms App

[英]How to add Labels to a Dictionary Windows Forms App

counter=1;
while (counter < 100)
{
    label = "label" + counter;
    buttoncontrol.Add(counter, label);
    buttoncontrol[counter].BackColor=Color.Brown;
    counter++;
}

I am trying to add labels to a dictionary so I can call them up later in the program.我正在尝试将标签添加到字典中,以便稍后在程序中调用它们。 The code shown results in the error Cannot convert from string to systems.windows.forms.label.显示的代码导致错误无法从字符串转换为系统。windows.forms.label。 I am hoping to save each label with there respected number eg: 1, label1 2, label2 3, label 3我希望用尊重的数字保存每个 label,例如:1,label1 2,label2 3,label 3

Your question is how to add Labels to a Dictionary in a WinForms app.您的问题是如何在 WinForms 应用程序中将标签添加到字典

Here's an example of one way to add 100 labels to a dictionary, that at the same time will place them in a FlowLayoutPanel to provide a basis for a simple test.下面是将 100 个标签添加到字典的一种方法示例,同时将它们放置在FlowLayoutPanel中,为简单测试提供基础。

private void addLabelsToDictionary()
{
    for (int i = 1; i <= 100; i++)
    {
        var label = new Label
        {
            Name = $"label{i}",
            Text = $"{i}",
            TextAlign = ContentAlignment.MiddleCenter,
            BackColor = Color.Brown,
            ForeColor= Color.White,
            Size = new Size(45, 45),
            Margin = new Padding(5),
        };
        buttonControl[i] = label;
    }
}
private readonly Dictionary<int, Label> buttonControl = new Dictionary<int, Label>();

This method can be called in the main form constructor.可以在主窗体构造函数中调用此方法。

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
        flowLayoutPanel.MaximumSize = new Size(1100, int.MaxValue);
        flowLayoutPanel.AutoSize= true;
        addLabelsToDictionary();
        // Populate flow layout panel
        foreach (var label in buttonControl.Values)
        {
            flowLayoutPanel.Controls.Add(label);
        }
        textBoxSearch.TextChanged += onSearch;
    }

所有 100


It sounds like you want labels to "come and go" during the applications's runtime.听起来您希望标签在应用程序运行时“来来去去”。 A basic test for this functionality uses a text box for comma-delimited values corresponding to the dictionary keys.此功能的基本测试使用文本框来存放与字典键对应的逗号分隔值。

    private void onSearch(object? sender, EventArgs e)
    {
        // Changing 'Visible' works but is SLOW for ~100 labels.
        // Do this instead:
        flowLayoutPanel.Controls.Clear();
        if (string.IsNullOrEmpty(textBoxSearch.Text))
        {
            foreach (var label in buttonControl.Values)
            {
                flowLayoutPanel.Controls.Add(label);
            }
        }
        else
        {
            var parse = textBoxSearch.Text.Split(',');
            foreach (var sVal in parse)
            {
                if (int.TryParse(sVal, out int value) &&
                    buttonControl.TryGetValue(value, out Label label))
                {
                    flowLayoutPanel.Controls.Add(buttonControl[value]);
                }
            }
        }
    }
}

过滤

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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