Changing button properties in a loop, for example - c#

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.

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.

Is it possible to turn a button into a variable?

I have a code in C# with 26 button in it and I want them to all be disabled until the user does something, but I dont want to copy/paste button1.Enable = false; Button2.Enable = false...
So is there a way to do something like this :
for (int i = 1; i < 26; i++)
{
button+i.Enable = false;
}
Thanks for your help.
You probably want to iterate buttons directly.
foreach(var button in this.Controls.OfType<Button>())
{
button.Enable = false;
}
Using either of Controls.Find or Controls.OfType<Button>() only works at run-time. I prefer a statically checkable compile-time approach. Then if you delete a button on the form the code won't compile. Helps give you that type-safe feeling.
I do it this way. Start with a field to hold the buttons in an array:
private Button[] _buttons = null;
Set the array once in your initialization code:
_buttons = new[] { button1, button2, button26 };
Then when you need to do something with the buttons you just do this:
foreach (var button in _buttons)
{
button.Enable = false;
}
You can use Form.Controls.Find()
for (int i = 1; i < 26; i++)
{
this.Controls.Find("button" + i, searchAllChildren: false).Enable = false;
}
The code assumes your buttons are named button1 through button26.
It's not possible directly like you are, but that doesn't make sense anyway.

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);

Changing The Property Of An Object Using Variables? C#/Visual Studio

Ok so I have 100 buttons and I need to change there colors based on conditions in a while loop. They are named button1, button2, button3 ,etc. during the first time around the loop (iteration?) I need to edit button1, the next time button2 the third time button3, etc.
I thought I could just make a string that equaled "button", add the number of times around the loop to it and change the color like that.
String ButtonNumber = "button" + i; where i = number of times around loop
When I try to edit the color using ButtonNumber.BackColor = Color.Red; it won't let me because it's not treating ButtonNumber like a button, but like a string. How do I accomplish this? Thanks! (this is my first time programing pretty much)
Consider using Controls.Find to find a control by name, and then you can change it's properties:
for (int i = 1; i <= 100; i++)
{
var buttonName = string.Format("button{0}", i);
var foundControl = Controls.Find(buttonName, true).FirstOrDefault();
if (foundControl != null)
{
// You can now set any common control property using the found control
foundControl.BackColor = Color.Red;
// If you need to set button-specific properties (i.e. properties
// that are not common to all controls), then cast it to a button:
var buttonControl = foundControl as Button;
if (buttonControl != null)
{
buttonControl.AutoEllipsis = true;
}
}
}

iterate control names using a counter

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.

Categories