
I have a usercontrol containing a panel , it contains two set of Textboxes one for name and another for comments, I create the Textboxes dynamically then I hide the textbox used for commenting.
Here I am not able to show it on click event , I tried using a function called Textbox() , but it adds the comment box just for the first row.
This is the code for creating textbox in usercontrol
public void Textbox()
{
TextBox[] tb1 = new TextBox[10];
for (int i = 0; i < 7; i++)
{
tb1[i] = new TextBox();
tb1[i].Multiline = true;
tb1[i].Height = 10;
tb1[i].Name = i.ToString();
tb1[i].Location = new Point(250 + i * 90, 82);
tb1[i].Size = new System.Drawing.Size(80, 40);
rowpanel.Controls.Add(tb1[i]);
}
}
next,I do this
private void comment_btn_Click(object sender, EventArgs e)
{
add.Textbox();
}
When I click comment_btn the comment textbox gets added to first row.
add is my usercontrol
As seen in the figure, when Add is Clicked ,Multiline textbox is added only to first row, I want it to be added to every row that is displayed on the panel, I just cant figure out how I go about doing it,
You mention that it only generates a textbox for the first row, indicating you’re expecting the dynamically generated comments textboxes to display on multiple rows?The code you’ve got there will generate the text boxes on the same row in multiple columns. The likelihood is that all 7 of your textboxes are generating correctly but you can only see the first one as the 2nd onwards are outside the bounds of your panel.
Changing the location line to
will generate the textboxes on multiple rows rather than multiple columns and hopefully display what you are after. – Addition of screenshot indicates the above is not the problem.
EDIT: If each of those rows of controls is an instance of your user control, then currently you’re only calling the
Textbox()method on one of them which is why only one row is being added. You need to add a loop in yourcomment_btn_Clickmethod which calls theTextbox()method on every control you’ve created.e.g. Assuming your collection of user controls is on a panel called show_pnl and assuming your user controls are of type TimeRecordingControl:
Also, consider naming your methods in a way that indicates what they do, i.e.
CreateTextboxesForCommentsrather than justTextbox🙂