简体   繁体   中英

Change "text" property of all buttons on form incrementally

I have created a soundboard in Visual C# where I click buttons and it plays an MP3 file. I currently have buttons named SBut_01 to SBut_x appropriately.

I store paths to the MP3 files that are being associated to these buttons in an xml file, which is fed in on startup and all "captions" for the corresponding buttons (which are extracted from the path for each key) are stored in an array named ButtonCapts[x] .

An example of something that could be in ButtonCapts[1] would be Foo stored as a string.

Originally when I created this soundboard, I manually specified each button's caption using a method I named AssignButtonCaptions . Which worked crudely like so:

public void AssignButtonCaptions{
SBut_01.Text = ButtonCapts[1];
SBut_02.Text = ButtonCapts[2];
...
}

However, as this soundboard expands in scope this is becoming a very large section of hardcode, so I would like to automate this as much as possible.

I've thought that I could probably use a foreach loop in the instance of ButtonCapts[] , but I don't at present know a way to loop through all of my SBut_xx buttons. Is it possible to detect all buttons on a form that are prefixed by SBut_ and iterate through them from 1 to x?

It would be great if something like below could be accomplished. Please note that I have assumed that each button has been put in an array named SButList as an example of what I'm trying to accomplish. I understand there is no way this code would work in its current form and am not even sure if objects such as buttons could be stored in such a way:

//Some kind of method to put all buttons prefixed with SBut_ in to an array named ButtonList here
    
//Then, iterate through all of the buttons and assign the captions

int i = 1;
foreach (button SBut in ButtonList)
{
   SBut.Text = ButtonCapts[i];
   i++;
}

Is this possible?

Thanks

Ok, so I've figured this out with some help from Anu6is. I'll document what I found so that it may help others.

Turns out that you can easily extract all controls in a form by using the following to accomplish this:

var myButtons in Controls.OfType<Buttons>().Where(button => button.Name.Contains("Something"))

The issue I was having was that I needed to iterate through each parent directory, as my buttons were inside of a Group Box, which was in turn inside of one of many Tab Pages within a Tab Control object. I just did a quick foreach loop to iterate through all of these tabs and group boxes in order to resolve this.

My new working code looks like this, which in my opinion is much better:

public void AssignButtonCaptions()
        {
            foreach (TabPage Page in tabControl1.TabPages)
            {
                foreach(GroupBox ButtonContainer in Page.Controls.OfType<GroupBox>())
                {
                    foreach (var SButton in ButtonContainer.Controls.OfType<Button>().Where(button => button.Name.Contains("SBut")))
                    {
                        int ButtonNum = Int32.Parse(SButton.Name.Split('_')[1]);
                        SButton.Text = ButtonCapts[ButtonNum];
                    }
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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