Reputation: 19
I want to add a set of items to a table in runtime with C#.
One of those items would be a TextBox
and I would like to make it show something like "Input Number1", "Input Number2", and so on.
private void Add_Input_Click(object sender, EventArgs e)
{
TabelaInputs.Controls.Add( new TextBox() {Text = "Input number {0}",InputCount });
Doesn't seems to be the right way...
Upvotes: 0
Views: 459
Reputation: 1
int counter = 0;
private void Add_Input_Click(object sender, EventArgs e)
{
new TextBox() { Name = "TextBox" + counter.ToString(), Text = "Input number " + counter.ToString() };
counter++;
}
This will create a new text box every time you click the button.
Upvotes: 0
Reputation: 1487
All you are missing is a call to string.Format();
:
C# 5:
private void Add_Input_Click(object sender, EventArgs e)
{
TabelaInputs.Controls.Add(new TextBox() { Text = string.Format("Input number {0}", InputCount) });
}
C# 6:
private void Add_Input_Click(object sender, EventArgs e)
{
TabelaInputs.Controls.Add(new TextBox() { Text = $"Input number {InputCount}" });
}
Upvotes: 1