I would create a user control that contains a shortcut and a text field in it and simply creates instances of this user control "n". If you want to know the best way to do this and use properties to access Label and Text Box values from a user control, let me know.
An easy way to do this would be:
int n = 4;
The above code assumes that you have a btnDisplay button and it has an onClick event assigned to the btnDisplay_Click event btnDisplay_Click . You also need to know the value of n and you need to figure out where to place all the controls. Controls must also have a width and a height.
To do this with User Control, just do it.
Well, first of all, go in and create a new user control and put a text box and a label in it.
Let's say they are called txtSomeTextBox and lblSomeLabel . In the code behind, add this code:
public string GetTextBoxValue() { return this.txtSomeTextBox.Text; } public string GetLabelValue() { return this.lblSomeLabel.Text; } public void SetTextBoxValue(string newText) { this.txtSomeTextBox.Text = newText; } public void SetLabelValue(string newText) { this.lblSomeLabel.Text = newText; }
Now the code for creating a user control will look like this (MyUserControl is the name that you specified on your user control):
private void btnDisplay_Click(object sender, EventArgs e) { MyUserControl[] controls = new MyUserControl[n]; for (int i = 0; i < n; i++) { controls[i] = new MyUserControl(); controls[i].setTextBoxValue("some value to display in text"); controls[i].setLabelValue("some value to display in label");
Of course, you can create more methods in usercontrol to access and set properties. Or simply, if you need to access a lot, just enter these two variables and you can directly access the text box and label:
public TextBox myTextBox; public Label myLabel;
In the user control designer, do the following:
myTextBox = this.txtSomeTextBox; myLabel = this.lblSomeLabel;
Then in your program, if you want to change the text value or just do it.
control[i].myTextBox.Text = "some random text"; // Same applies to myLabel
Hope this helps :)
PS. Here is a link to a sample project if.
Create Dynamic Management