0

I'm making a full programmed application using C# So instead of dropping controls I'm adding them dynamically using :


Button newButton = new Button();          
newButton.Name = "";
newButton.Text = "Created Button";
newButton.Location = new Point(250, 70);
newButton.Size = new Size(75, 23);

The first issue I encountered is that if I want to make multiple buttons that I cant use the same button variable multiple times, so I came with the idea of randomizing the name of the button variable like :


Random rnd = new Random();
int num = rnd.Next();
Button button+num.ToString() = new button();

But this don't work as you can only implement a simple name for the variable

Mxit _
  • 61
  • 4
  • 4
    When you do this: `Button newButton = new Button();` (then of course `[Some Container].Controls.Add(newButton)`), you create a new instance. The name of the variable is not relevant, `new Button()` is what counts. If, for some reason, you want to assign the `Name` Property, then do that. Probably not with the Random class. You'll find out sooner or later than it's not really required, You can handle collections of Controls added at run-time in very different ways, using different types of collections, depending on the use-case – Jimi Aug 22 '23 at 17:20

0 Answers0