如何在循环中处理按钮名称?

How to address button name in loop?

前提:- 我们有一个键值对列表。

此列表的项目数会有所不同。

我们有一个带有一堆默认按钮的表单。 (编辑 - 在设计器中较早构建,而不是在运行时构建。)

这些按钮被命名为 "button1, button2, ..."

我们的按钮比列表中的项目多。

在运行时,我们希望将信息从列表元素传输到按钮并隐藏未使用的按钮。

我的问题是如何通过循环处理这些按钮?

在 VBA 中使用 for 循环 我可以这样说:-

Me.Controls("TB_Item" & Format(i, "00")).Visible = False

在 C# 中,我以这个最小示例作为起点(表单有 10 个按钮):-

public UF_ButtonLoop()
    {
        InitializeComponent();

        List<KeyValuePair<string, string>> MyItems = new List<KeyValuePair<string, string>>
        {
            new KeyValuePair<string, string>("Apple", "Green Fruit"),
            new KeyValuePair<string, string>("Orange", "Orange Fruit"),
            new KeyValuePair<string, string>("Sprout", "Spawn of the Devil"),
            new KeyValuePair<string, string>("Hershey Bar", "A bit like chocolate"),
            new KeyValuePair<string, string>("Beefburger", "Man Food")
        };

        //Loop through the 10 buttons
        for (int i = 1; i < 11; i++)
        {
            if (i <= MyItems.Count )
            {
                //Transfer Data from list to button

                //Pseudo code
                Control("Button" + i).Text = (MyItems.ElementAt(i).Key);
                Control("Button" + i).Tag = (MyItems.ElementAt(i).Value);

            }
            else
            {
                //Hide the button as we've reached the end of the list so have no use for it.

                //Pseudo code
                Control("button" + 1).Hide();
            }

            // Note, VBA methos is:-
            // Me.Controls("TB_Item" & Format(i, "00")).Visible = False
        }

    }

Control("Button" + i) 语法不正确。

我可以在 C# 中执行此操作吗?如果可以,怎么做?

如果不是,正确的方法是什么?

此外,我是新来的,所以如果我以错误的方式提问,请不要害羞告诉我!

非常感谢, 欧文

我建议您在阅读列表时创建这些按钮。不要在设计器中静态创建按钮。您需要在主窗体中创建按钮列表,然后以编程方式添加按钮。

列表创建

List<Button> buttons = new List<Button>(); 

然后在你遍历数据列表的某个循环中

Button MyButton = new Button();
Mybutton.Location = new Point(YourX, YourY);
Mybutton.Text = "AnyText";
Mybutton.AutoSize = false;
MyButton.Size = new Size(width, height);
Mybutton.BackColor = Color.LightBlue;
Mybutton.Padding = new Padding(6);
buttons.add(MyButton);

广告 1。 最佳做法是创建自己的 "SpecialButton" class,它继承自原始按钮 class。然后您可以向按钮添加一些特殊属性,作为对其他一些元素的引用,这些元素应该由 SpecialButton 控制等等...

首先感谢您的帮助,非常感谢,尤其是通过其他方法,甚至不同的控件来推动它。

问题的答案可能分为两个选项:- (答案 1)- 通过在运行时创建您需要的内容来正确地做到这一点,而不是使用静态设计器然后隐藏过度构建的内容。

(答案 2)- 快速 "bodge" 让我最初的问题中的代码正常工作。 (我的 C# 经验可以用小时来衡量,所以我有很多东西要学,但也需要让代码在今天工作......)

因此,一种可行的方法是将按钮放在它们自己的列表中,然后引用它。像这样:-

public UF_ButtonLoop()
    {
        InitializeComponent();

        //TransferListToButtone_Rev1();
        List<KeyValuePair<string, string>> MyItems = new List<KeyValuePair<string, string>>
        {
            new KeyValuePair<string, string>("Apple", "Green Fruit"),
            new KeyValuePair<string, string>("Orange", "Orange Fruit"),
            new KeyValuePair<string, string>("Sprout", "Spawn of the Devil"),
            new KeyValuePair<string, string>("Hershey Bar", "A bit like chocolate"),
            new KeyValuePair<string, string>("Beefburger", "Man Food")
        };

        List<Control> ListOfButtons = new List<Control>
        {
            button1, button2, button3, button4, button5, button6, button7, button8, button9, button10
        };

        void A_Button_Click(object sender, System.EventArgs e)
        {      
            Console.WriteLine((sender as Button).Text + " = " + (sender as Button).Tag);
        }

        //Loop through the 10 buttons
        for (int i = 0; i < 10; i++)
        {
            if (i < MyItems.Count )
            {
                //Transfer Data from list to button
                    ListOfButtons.ElementAt(i).Text = (MyItems.ElementAt(i).Key);
                    ListOfButtons.ElementAt(i).Tag = (MyItems.ElementAt(i).Value);

                //Set Click Event
                    ListOfButtons.ElementAt(i).Click  += new EventHandler(A_Button_Click);
            }
            else
            {
                //Hide the button as we've reached the end of the list so have no use for it.
                    ListOfButtons.ElementAt(i).Hide();
            }

        }

    }

大家好, 欧文