0

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]);
    }
}
3
  • 2
    @HarithaGayashan - Please don't put your code in the comments. Edit your question instead. Commented Jul 8, 2017 at 6:27
  • @HarithaGayashan - And what's wrong with your code? Does it compile? What happens when you run your code? Does it cause an error? Commented Jul 8, 2017 at 6:28
  • 1
    You should read How to Ask. Commented Jul 8, 2017 at 6:29

2 Answers 2

2

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Add to your answer that if User for example change the text in that textbox twice there will be created Buttons with the same Tag and Text
Thanks for your advice. But as a new member I think the answers that we share with the people are considered as simple samples which help them to find the best idea. I think we are not going to consider or solve all possible challenges that they may confront with. Don't you think so? Please let me know. Thank [email protected]
Yes, of course, but to warn of a possible problem is never superfluous :)
0

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().

8 Comments

Please explain how this answers the question? I can see that this relates to creating buttons, but the OP doesn't appear to be having a problem with naming of the buttons.
@Enigmativity as we see from his added code he can create Buttons, he does not how to work with them after that. how can I get id's of that buttons? Buttons have not ID, they have Name for such kind of things and I have explained how to work with Names
No, the OP doesn't know how to create buttons. His code doesn't create buttons.
@Enigmativity my answer is providing part of dynamically creation of Button too
@Enigmativity OP's question -> 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? , Your comment -> No, the OP doesn't know how to create buttons. His code doesn't create buttons.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.