简体   繁体   English

c# 如何将按钮添加到数组并设置所有按钮的文本

[英]c# how to add Button to array and set text of all buttons

Hi my problem is i want to add buttons to array size: 65 and there is a problem here's my code:嗨,我的问题是我想将按钮添加到数组大小:65,这是我的代码的问题:

Button[] buttonsa = new Button[65];
for (int d = 0; d <= buttonsa.Length; d++)
                {
                    try
                    {
                        buttonsa[d].Text = "Runned!";
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

My all code for creating button to scene:我创建按钮到场景的所有代码:

private void CreateButton(int xh, int yh, int width, int height)
        {
            Button newButton = new Button();
            if (width < 100)
            {
                newButton.Width = 100;
            }
            else
            {
                newButton.Width = width;
            }
            if (height < 30)
            {
                newButton.Height = 30;
            }
            else
            {
                newButton.Height = height;
            }
            
            
            newButton.Text = "button" + btnIndex;
            newButton.ForeColor = Color.FromArgb(35,35,35,0);
            newButton.Name = "btn" + btnIndex.ToString();
            newButton.AccessibleName = btnIndex.ToString() + "|FlatStyle=" + FlatStyle.System.ToString() + "|Pussy=sussy";
            newButton.ContextMenuStrip = btnContext;
            newButton.Tag = "button" + btnIndex;
            newButton.FlatStyle = FlatStyle.Flat;
            newButton.BackColor = Color.White;
            newButton.Location = new Point(xh, yh);
            newButton.Click += newButton_Click2;
            buttonsa[btnIndex] = newButton;
            btnIndex++;


            int x = rand.Next(10, pic.ClientSize.Width - newButton.Width);
            int y = rand.Next(10, pic.ClientSize.Height - newButton.Height);
            newButton.Location = new Point(xh, yh);
            pic.Controls.Add(newButton);
            buttonsInScene.Add(newButton);
            this.CenterToScreen();
            g.Clear(Color.FromArgb(35, 35, 35, 0));
            pic.Refresh();
        }

Im working on big project if anyone can help me Thanks alot!如果有人能帮助我,我正在做一个大项目非常感谢! <3 <3

I tried a few times to fix it i needed to get all my buttons in form and set text to "Runned!"我尝试了几次来修复它,我需要将所有按钮都设置为表单并将文本设置为“已运行!”

You can use the recursive method for getting all buttons on the form.您可以使用递归方法获取表单上的所有按钮。 You don't need to add all buttons to the array type, anytime you can use your function and gets all buttons on the form and on the child controls.您不需要将所有按钮添加到数组类型,您可以随时使用 function 并获取表单和子控件上的所有按钮。 But, if you want, you can write easy code inside the function, for adding buttons to an array or to the List.但是,如果需要,您可以在 function 中编写简单的代码,用于将按钮添加到数组或列表中。

Example:例子:

 private void ButtonList (Control cont)
    {
        foreach (Control c in cont.Controls)
        {
            if (c is Button) 
            {
                (c as Button).Text = "Hello";
                (c as Button).Visible = true;
                // and etc... 
                /*************** example adding button object to list *************
                   declare listButton property of type List<Button> on your code 
                   listButton.Add((c as Button));
                *******************************************************************/
            }
            if (c.Controls.Count > 0)
            {
                ButtonList(c);
            }
        }
    }

And you can use this function calling that你可以使用这个 function 调用它

ButtonList (MainForm);

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

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