iterate control names using a counter - c#

I have over 100 buttons in a windows form an I want to access their name for command Button.PerformClick() based on the counter in a for loop. For example: Button + Convert.Tostring(Counter).PerformClick() How do I do this?

You can use OfType extension method to loop through all of your buttons:
foreach(var button in this.Controls.OfType<Button>())
{
// do something with button
button.PerformClick();
}
For loop version:
for(int i=0; i<counter; i++)
{
string name = "Button" + i;
if(this.Controls.ContainsKey(name))
{
var currentButton = this.Controls[name] as Button;
}
}
Note: This answer assumes that your buttons are direct child elements of your Form.If Buttons inside of a Panel then you should access ControlCollection of your panel with panelName.Controls instead of this.Controls.

Related

How to find index of controls in flowlayoutpanel in C#

I am new to C#.
I have Form1 and flowlayoutpanel. I dynamically add Buttons to flowlayoutpanel and the buttons details comes from a database table.
I want to know the name of the first button in the flowlayoutpanel.
for (i = 0; i < DataTable.Rows.Count; i++)
{
Button btn = new Button();
btn.Name = DataTable.Rows[i]["Name"].ToString();
btn.Text = DataTable.Rows[i]["PostCode"].ToString();
flowlayoutpanel.Controls.Add(btn);
}
String First_Button_Name = ...........
If you want to get the name of the first button to be added to the FlowLayoutPanel, regardless of how those buttons got there, use:
string firstButtonName = flowlayoutpanel.Controls.OfType<Button>().FirstOrDefault()?.Name;
This is the value you are searching for:
DataTable.Rows[0]["Name"].ToString()
but make sure you have at least an element before you try to get this value.

How to reference a ListBox name using a string in Csharp

for (int i=1; i<4; i++)
{
string buttonName = "button" + i;
if (Controls[buttonName].BackColor = Color.Red)
{
Controls[buttonName].Enabled = false;
}
}
This code works perfectly. The code checks 3 different buttons (button1, button2, button3) and if their color is red they become disabled. The button name is referenced using a string:
Controls[buttonName]
Is there a way to reference a ListBox using a string in the same way? What would "Controls" need to be changed to?
If you simply want to go over all ListBoxes, you could also use .OfType<T>()
foreach (ListBox lb in this.Controls.OfType<ListBox>())
{
lb.Enabled = false;
}
... and it would of course work the same for .OfType<Button>() without the need to name your controls in a way to enumerate them.

How do I loop all buttons in my Form to change their text in C#

I am new to C# and I use windows forms. I have Form1 with 20 buttons on it (button1 to button20).
How can I loop all those 20 buttons and change their text to for example "Hello" text?
Anyone knows how to achieve this? Thank you
Somewhere in your form's code behind:
foreach(var btnControl in this.Controls.OfType<Button>())
{
btnControl.Text = "Hello";
}
A simple loop will work find until you introduce containers into the page (groupboxes, tabs, etc). At that point you need a recursive function.
private void ChangeButtons(Control.ControlCollection controls)
{
for (int i = 0; i < controls.Count; i++)
{
// The control is a container so we need to look at this control's
// children to see if there are any buttons inside of it.
if (controls[i].HasChildren)
{
ChangeButtons(controls[i].Controls);
}
// Make sure the control is a button and if so disable it.
if (controls[i] is Button)
{
((Button)controls[i]).Text = "Hello";
}
}
}
You then call this function passing in the Form's control collection.
ChangeButtons(this.Controls);

Using labels like arrays

I am working on winform application in asp.net using c#. I have 10 labels on my winform created in the designer mode, called Label0 to Label9. Now I want to change the Text property of all the labels at once as per the data I acquire in the middle of execution of my program.
i want to do something like this :
for (int i = 0; i < 10; i++)
{
Label[i].Text = "Hello, this is label: "+ i.ToString();
}
Now, of course this won't work. But how can this be done? how can i call the label like its done in an array? If not possible, then what can be the best alternative solution for my problem?
If you are talking about WinForms, then you can do like this:
private void Form1_Load(object sender, EventArgs e)
{
// Form1_Load is just sample place for code executing
for (int i = 1; i < 10; i++)
{
var label = Find<Label>(this, "label" + i);
label.Text = "Hello, this is label: " + i.ToString();
}
}
private T Find<T>(Control container, string name)
where T : Control
{
foreach (Control control in container.Controls)
{
if (control is T && control.Name == name)
return (T)control;
}
return null;
}
This code will search label in form controls, and then return it based on control name and type T. But it will use just parent form. So if your label is in some panel, then you need to specify panel as container parameter. Otherwise Find method can be updated as recursive method, so it will search inside all form subcontrols, but if there will be two Label1 controls, then it will return just first one, that might be not correct.
If you can put all Label on a panel after the you can use below code to change the text
foreach (Control p in panal.Controls)
if (p.GetType == Label)
p.Text = "your text";

Changing button properties in a loop, for example

I would like to know how it's possible to change button properties by a code when we don't know a button name while writing it.
For example, I have a loop like this:
for (int i=0; i<5; ++i) {
int buttonName = "button_" + i;
buttonName.enabled = false;
}
Thanks in advance!
You can access the Controls collection of the parent containing the button like this:
if(parent.Controls.ContainsKey(buttonName))
{
Button myButton = (Button)parent.Controls[buttonName];
myButton.Enabled = false;
}
This will need a little extra work if your buttons are not contained within the same parent; ie. some buttons on a Form, some buttons on a Panel contained within that same form.

Categories