Dynamically create buttons in c# by user at runtime - c#

How to create buttons dynamically after user input in C# (Visual Studio).
There is a text-box to enter how many buttons user wants?
Then my target is to create buttons below the input field as the user wants
then how can I get id's of that buttons?
private void button1_Click(object sender, EventArgs e)
{
List<Button> buttons = new List<Button>();
for (int i = 0; i < n; i++)
{
this.Controls.Add(buttons[i]);
}
}

Here I first added an event handler to the textbox, which is called whenever the text value is changed. The value is converted to the int value and then is used in a for loop statement. You can set your button's potion to the desired value using location property. Using tag or name property you can assign a unique value to your buttons. I hope the code helps.
Look at the code below :
private void Form1_Load(object sender, EventArgs e)
{
textBox1.TextChanged += textBox1_TextChanged;
}
void textBox1_TextChanged(object sender, EventArgs e)
{
var txtBox = sender as TextBox;
if (txtBox == null) return;
var count = Convert.ToInt16(txtBox.Text);
//
var xPosition = 0;
for (var i = 1; i <= count; i++)
{
var button = new Button
{
Tag = string.Format("Btn{0}", i),
Text = string.Format("Button{0}",i),
Location = new Point(xPosition, 0)
};
xPosition = xPosition + 100;
Controls.Add(button);
}

When you are creating Control(in your case Buttons) you can give them Name property. It will be very good if that name will be unique.
var btn = new Button();
btn.Name = "MyBtn";
btn.Text = "Our Button";
this.Controls.Add(btn);
For creation of N buttons you just need to put this in a Loop with N iterations and set btn.Name to something like "Name"+SomeNumber.
To set the Position of the Buttons to below the input you should set btn.Left and btn.Top to the corresponding coordinates.
Then when you need to work with generated Control/Button you can do search by that Name in the following way:
var btn = (Button)this.Controls.Find("MyBtn", true).First();
and do whatever you want with that Control/Button.
But in this case there is some danger as I am not checking if there was found any control with that name. If you write incorrect Name this will throw exception on .First().

Related

How can I retrieve the input values of dynamically created textfields?

Here is my code so far where I have a numericupdown item named numericUpDown and button. Once the user selects a number when they press the button it dynamically created the fields.
private void createPerson_Click(object sender, EventArgs e)
{
Label[] person_Name = new Label[(int)this.numericUpDown.Value];
TextBox[] person_txtinput = new TextBox[(int)this.numericUpDown.Value];
for (int i = 0; i < this.numericUpDown.Value; i++)
{
//create person name label
Person_Name[i] = new Label();
Person_Name[i].Location = new System.Drawing.Point(20, 114 + i * 25);
Person_Name[i].Size = new System.Drawing.Size(120, 15);
Person_Name[i].Text = (i + 1).ToString() + #")" + "Person Name:";
this.Controls.Add(Person_Name[i]);
//create person name textbox
PersonNameTxtInput[i] = new TextBox();
PersonNameTxtInput[i].Location = new System.Drawing.Point(140, 114 + i * 25);
PersonNameTxtInput[i].Size = new System.Drawing.Size(125, 20);
this.Controls.Add(PersonNameTxtInput[i]);
}
}
private void save_Click(object sender, EventArgs e)
{
for (int i = 0; j < this.numericUpDown.Value; i++)
{
MessageBox.Show("" + PersonNameTxtInput[i].Text);
}
}
My question is, how can I get all the values from the textboxes depending on how many fields are created by the user when the save button is pressed?
I have tried using the code within the save button listener however how can i make Label[] person_Name = new Label[(int)this.numericUpDown.Value]; a global variable so i can access it within the save button for loop.
Well, I don't know exactly why you are doing this in this particular way and I must admit it doesn't seem very effective, but you could just do what Ryan_L suggested and iterate through this.Controls like this
for(int i = 0; i < this.Controls.Count; i++)
{
if(this.Controls[i] is TextBox) //skip buttons and labels
{
MessageBox.Show("" + this.Controls[i].Text);
}
}
Now, regarding your question how to define a global variable so you can access it within the save button for loop...just define the two arrays outside of the createPerson_Click event like this:
Label[] person_Name;
TextBox[] person_txtinput;
private void button1_Click(object sender, EventArgs e)
{
person_Name = new Label[(int)this.numericUpDown.Value];
person_txtinput = new TextBox[(int)this.numericUpDown.Value];
//the rest of the code
}
Hope this helps. However, you might want to reconsider your entire approach.

c# - how can I handle the click event of controls created dynamically within a loop

I'm looking for some advice on how to add click event handlers to labels that have been created created dynamically within a loop.
I've searched for click event handlers on dynamically created controls but this always comes back with single controls that aren't within an array.
example of code:
//create an array of 16 labels
Label[] label = new Label[16];
//loop through the array of labels
for (int i = 0; i < label.Length; i++)
{
label[i] = new Label(); //create new label
label[i].Name = "lbl" + i.ToString(); //give the label a name
label[i].Text = "label " + i.ToString(); //give the label text
}
Any help and advice on this would be great, thanks!
Add a handler:
label[i].Click += HandleLabelClick;
void HandleLabelClick(object sender, EventArgs e)
{
// ...
}
Note that you can determine which label was clicked by using the sender argument:
void HandleLabelClick(object sender, EventArgs e)
{
var label = (Label) sender;
if (label.Text == "this or that") { /* ... */ }
}

How to access or modify dynamically created button

I've created 8*8 arrays of button to create a grid for Minesweeper Game. After creating the grid I need to access them(buttons). How can I access them like change the name or disable click-ability or change the colors.
private void gridDesign()
{
/***********************************Uniform grid*********************/
int firstLoop, secondLoop;
for (firstLoop = 0; firstLoop < 8; firstLoop++)
{
for (secondLoop = 0; secondLoop < 8; secondLoop++)
{
Button lbl = new Button()
{
Name = "_" + firstLoop.ToString() +"_"+ secondLoop.ToString(),
Content = firstLoop.ToString() + " " + secondLoop.ToString(),
};
lbl.Click += button_Click;
uniformgridMinesweeper.Children.Add(lbl);
}
}
/********************************************************************/
}
private void button_Click(object sender, RoutedEventArgs e)
{
Button bbb = e.Source as Button;
bbb.Background = Brushes.Indigo;
//how can i access a button named "_5_5"
}
It is straightforward to access a child of a Grid with a specific property (Name):
var button = uniformgridMinesweeper.Children.OfType<Button>()
.FirstOrDefault(button => button.Name == "_5_5");
That said, you might want to take thumbmunkey's advice and put the buttons in your own Dictionary or lookup for quick and easy access. Or better yet, use data binding with a view model ...

Click event for dynamically created array of buttons

In my application i have array of buttons created dynamically.I am trying to raise an onclick event for those buttons and change the text of the button which i click.I tried the below code for this but its not working.How can i do this?.Any suggesions?
Code:
for (int i = 0; i < 5; i++)
{
lbl = new Button[5];
lbl[i] = new Button();
lbl[i].Text = "hi";
lbl[i].Width = 30;
lbl[i].Click += new EventHandler(lbl_click);
//lbl[i].CssClass = "label";
div1.Controls.Add(lbl[i]);
}
Click Event:
protected void lbl_click(object sender, EventArgs e)
{
Button[] lbl = sender as button[];
lbl[i].Text = "clicked";
}
You are recreating the array of buttons in your event handler, but this array is not populated with the buttons created before. It is empty and will give you a null reference exception if you try to use an element of this array (null.Text, it will never work).
The sender object instead, represent the button that the user has clicked.
protected void lbl_click(object sender, EventArgs e)
{
Button lbl = sender as Button;
lbl.Text = "clicked";
}
Also, if you need to know which specific button has been clicked then I suggest you to add something to differentiate between them at creation time:
For example use the name property:
Button[] lbl = new Button[5];
for(int i = 0; i< 5; i++)
{
....
lbl[i].Name = "Button_" + i.ToString();
....
}
Notice that I have moved the array declaration and initialization outside the loop that create every single element of the array (the actual button).

Populate form with controls based on int value

I was wondering how to go about doing something such as this:
I need to create a Form with a specific number of buttons based on an integer value representing the number of buttons needed, then give them their own specific names so that each can have their own unique event handlers.
A real example I can think of doing this would be the Windows log-in screen, where the number of controls created is based on the number of users and whether there is a Guest account or not. How do you think that they programmed that?
Thank you.
for (int i = 0; i < 5; i++)
{
Button newButton = new Button();
newButton.Name = "button" + i.ToString();
newButton.Text = "Button #" + i.ToString();
newButton.Location = new Point(32, i * 32);
newButton.Click += new EventHandler(button1_Click);
this.Controls.Add(newButton);
}
private void button1_Click(object sender, EventArgs e)
{
if (((Button)sender).Name == "button0")
MessageBox.Show("Button 0");
else if (((Button)sender).Name == "button1")
MessageBox.Show("Button 1");
}
Somehow you have to define the names of all the buttons. I would suggest you to create a new string array and write the button names inside, and then use them in buttons creation loop:
//do the same length as the for loop below:
string[] buttonNames = { "button1", "button2", "button3", "button4", "button5" };
for (int i = 0; i < buttonNames.Lenght; i++)
{
Button newButton = new Button();
newButton.Name = "button" + i.ToString();
newButton.Text = buttonNames[i]; //each button will now get its own name from array
newButton.Location = new Point(32, i * 32);
newbutton.Size = new Size(25,100); //maybe you can set different sizes too (especially for X axes)
newButton.Click += new EventHandler(buttons_Click);
this.Controls.Add(newButton);
}
private void buttons_Click(object sender, EventArgs e)
{
Button btn = sender as Button
MessageBox.Show("You clicked button: " + btn.Text + ".");
}

Categories

Resources