tags:

views:

51

answers:

5

hi

i have 5 button's and 3 textbox in my form (WinForm)

i need to run from button1 to button5 and change the text to "aa" for example

how to do it ? (in C# code)

thank's in advance

A: 

Put your buttons into a List collection during initialisation, then run through the list in your event, changing the Text property

Unless I've misunderstood your question, that should work

Jroc
+6  A: 
foreach(var button in this.Controls.OfType<Button>())
{
    button.Text = "aa";
}

(assuming there's no other buttons on the form)

A.
and how to do it if there are more button (i have button6 and button7)
Gold
can't you for example put your 1-5 buttons on some kind of panel and then iterate through `panel.Controls`?
A.
A: 

Set the TabOrder in such a way that button5 is after button1. Or using the following code: button5.Focus()

Peter van Kekem
I thought that was what the OP meant also at first, but I think that A. answer is actually what he is after :)
Øyvind Bråthen
A: 

How about this:

foreach(var button in this.Controls.OfType<Button>()
                                   .Where(btn => btn.Name.StartsWith("groupedButtons"))
{
    button.Text = "aa";
}

To make this working you should rename your buttons in some way, that you can determine the grouping by it's name. So that Button 1 - 5 are named "buttonGroupUserFirstName", "buttonGroupUserLastName", "buttonGroupUserPhone", "buttonGroupMachineCpu", "buttonGroupMachineOs", etc. With this approach you can then use the "buttonGroupUser" as prefix to find all buttons that belongs to this group.

Last but not least i must say that such an approach always sounds to be a bad approach. It would be much better to create for each group at least an own Panel or even an own UserControl. The usage of UserControls to group a specifc part of the GUI (and the logic within this part) helps a lot to modularize your code and prevents you from creating one single form, containing dozens of directly hosted elements and a code file of several hundreds or thousand lines.

Oliver
@downvoter: Any comment why to downvote it?
Oliver
+1  A: 

Hi,

I saw that an answer was already accepted but I have to agree with Oliver who himself said that the approach is not really appropriate (and I’m to noobish to be able to comment so I submit another answer).

I would suggest either to go for a UserContol or Panel approach as this would make for a nicer overall design. Otherwise you can opt for a list where you add all your controls that should be edited.

List<Control> _controls = new List<Control>();

Then just add the correct controls (e.g. in the onLoad event or constructor of the form).

public Form1()
    {
        InitializeComponent();

        _controls.Add(button1);
        _controls.Add(button2);
        _controls.Add(button3);
        _controls.Add(textBox1);
        _controls.Add(textBox2);

    }

And then just iterate through and change when needed.

    private void ChangeTextOnControls(string text)
    {
        foreach (Control c in _controls)
            c.Text = text;
    }

E.g.

private void button1_Click(object sender, EventArgs e)
    {
        ChangeTextOnControls("Blah");
    }
Mikael