Dynamically Add Text Box After Label

chalupabatman

Member
Joined
Jun 3, 2014
Messages
16
Programming Experience
Beginner
A more experienced user will probably laugh at the predicament I have gotten myself into but here goes. I am dynamically creating labels on a windows form flow layout panel. Perfect, formatting properly, lining up properly hooray! Well....now I need to dynamically create a text box that will dispaly directly after the aforementioned dynamically created labels. But with my flow layout panel it is creating the labels - then creating the text boxes so it lines up like so
VB.NET:
label1
label2
label3
label4
label5
textbox1
textbox2
textbox3
textbox4
textbox5

My desired output is
VB.NET:
label1  textbox1
label2  textbox2
label3  textbox3
label4  textbox4
label5  textbox5
 
Perhaps you could use default flow direction and add them in pairs? Each time add one label and one textbox, making sure the width of the two takes up the space of one "row". In same thought, if these two controls are linked like that, then you can design a UserControl with the two controls and add UserControl instances to FlowLayoutPanel instead. There is also TableLayoutPanel container control with row/column table layout available.
If the label and textbox are not linked another approach could be to use two panels, adding labels to one and textboxes to the other.
 
From that tutorial - It doesn't seem that would be a feasible solution for dynamically creating multiple labels with text boxes at run time.

I'll have to think of a different approach of how to do my coding and syntax if adding a label then adding text box is my best option.
 
Add the label and textbox in a panel first, docked or anchored. Then add the panel in the FlowLayoutPanel. Or just create a user control with the Label and Textbox with Public modifiers. This way you can access each pair with MyUserControl1.Label.Text. Don't forget to give your controls names either.
 
Add the label and textbox in a panel first, docked or anchored. Then add the panel in the FlowLayoutPanel. Or just create a user control with the Label and Textbox with Public modifiers. This way you can access each pair with MyUserControl1.Label.Text. Don't forget to give your controls names either.

Something like this user control would be sufficient but my coding skill level is way low...

c# - How to create many labels and textboxes dynamically depending on the value of an integer variable? - Stack Overflow
 
Something like this user control would be sufficient but my coding skill level is way low...

Whether you intended it or not, that basically comes across as you asking us to do it for you. How about you have a go at it and then ask for help when you encounter an issue instead of not even trying? We're here to help if you need it but you haven't even established that you need it yet.

Designing a user control is almost identical to designing a form. Once you've designed the user control, using it is like using any other control. Those are two things that you can presumably already do so you basically already know how to design and use a user control. Give it a go and, if you encounter an issue, post back about that specific issue.
 
Whether you intended it or not, that basically comes across as you asking us to do it for you.
Apalogies, that is not what I was after. I was just meaning that following the tutorial that JohnH linked over to, I didn't feel like I had a full understanding of how to create a re-usable set of a label and text box. Which is why I added the stackoverflow link so that if I was speaking riddles on what I was after, that would help clear it up.

I wasn't asking for someone to write code for me from the ground up, I again apologize for coming across that way.

Designing a user control is almost identical to designing a form. Once you've designed the user control, using it is like using any other control. Those are two things that you can presumably already do so you basically already know how to design and use a user control. Give it a go and, if you encounter an issue, post back about that specific issue.

I created the user form that was explained from the tutorial link, let me pose the question a different way. Once I have a user control created, how could I dynamically insert it onto my Windows Form?
 
When you create a user control, you are basically creating a type. After you build your application once, that new control will be available from the toolbox at the top, and you can add it just like any other control, either from the designer or programmatically (e.g. MyForm.Controls.Add(New MyUserControl) ... ). There really is nothing to it. The hardest thing to figure out is how to make your user control properly resize. Use docking and anchoring for this.
 
One more question and I think I got this. How can I change the name on my user control of the label and text box at run time? I tried the below, but this does not change the names, and I verified this was the correct spelling & names
VB.NET:
UserControl1 uc1 = new UserControl1();
label1.Text = "I dynamically Changed The Label Text";
label1.Name = "I dynamically Changed The Label Name";
Form1.Cotrols.Add(uc1);
And if I tried to specify uc1 I would receive a compile error of label1 is inaccessible due to its protection level -
VB.NET:
uc1.label1.Text = "I changed the label text dynamically";
 
Typically all your labels and textboxes will have the same name. For example when you create your user control you will name the textbox MyTextBox and the label MyLabel. In the designer on the user control, find the property Modifier for the textbox and label and set them both to Public, so that you can access them from outside.

Then you instanciate your usercontrol, and you can access the label or textbox like this:

uc1.MyLabel.Text = "Some caption"
uc1.MyTextBox.Text = "Some text"

uc2.MyLabel.Text = "Some caption"
uc2.MyTextBox.Text = "Some text"
 
uc1.MyLabel.Text = "Some caption"
uc1.MyTextBox.Text = "Some text"

uc2.MyLabel.Text = "Some caption"
uc2.MyTextBox.Text = "Some text"

I think I am confused then as to how to re-use this user control. Would I need to create a uc1, uc2, uc3 each time I need to add an instance to my form?

Asking since the code example you provided shows
uc1.MyLabel.Text
uc2.MyLabel.Text
 
Last edited:
Well no you don't need to create uc1 uc2 etc..., but you DO need to create a new instance of the user control.

Dim uc = New MyUserControl() With {.Name = "FirstUserControl"}
MyForm.Controls.Add(uc)

uc = New MyUserControl() With {.Name = "SecondUserControl"}
MyForm.Controls.Add(uc)

MyForm.FirstUserControl.Label.Text = "Some caption"
MyForm.SecondUserControl.TextBox.Text = "Some text"
 
Back
Top