Line Up TextBox to Label Dynamically

Status
Not open for further replies.

chalupabatman

Member
Joined
Jun 3, 2014
Messages
16
Programming Experience
Beginner
I am want to (by any option from the toolbox) have 2 columns...1 with the field and 1 with answer. What I have trouble with is how to line-up the answer with the field when the field could have multiple answers. I want this output
VB.NET:
field ----  answer
EmpID     ID
Phone     Phone1   Phone2
Address  Address

Notice how phone should have two textboxes on the line and address should only have 1. How can I get 2 textboxes on the same line?
 
If you really must have Labels and TextBoxes then the most obvious option seems to be to use a TableLayoutPanel. You can create columns and rows and then put a child control in any cell.
 
How would I create the association of textboxes to labels so when there are 2 associated labels they both appear on the same line?
 
How would I create the association of textboxes to labels so when there are 2 associated labels they both appear on the same line?

There is no specific association. Like I said, you can put a child control in any cell you like in the TLP. If you controls on the same row then you put them in cells on the same row. In the six minutes between my post and yours, what efforts have you made to find out how a TableLayoutPanel works?
 
I currently had it set-up as to use a TLP, but when was unable to get the formatting exactly like I was after (with 2 textboxes on some lines, and only one on others) I thought I would reach out and see if 1) I was doing it properly and 2) if there was a better way to accomplish the same result.
 
I currently had it set-up as to use a TLP, but when was unable to get the formatting exactly like I was after (with 2 textboxes on some lines, and only one on others) I thought I would reach out and see if 1) I was doing it properly and 2) if there was a better way to accomplish the same result.

Using a TLP is the best option in my opinion. As for whether you're doing it properly, we'd have to know what you're doing to know whether it's the best way or not. You would call the Controls.Add method of your TLP to add a child control and that method will accept column and row indexes. If you want two controls on the same row then use the same row index when you add them.
 
Only increment the index if the textbox needs to be on a new line...
VB.NET:
foreach (string label in c.LabelsNeeded)
{
	//Adding in a label
	Label lbl = new Label();
	lbl.Name = "lbl_" + index;
	lbl.Text = label;
	lbl.AutoSize = true;    
	tableLayoutPanel1.Controls.Add(lbl);
	//Adding in a textbox
	TextBox tbx = new TextBox();
	tbx.Name = "txt_" + index;
	tbx.AutoSize = true;
	tableLayoutPanel1.Controls.Add(tbx);
	//Only increment the index if the label is not null
	//meaning there was a new label added
	if (string.IsNullOrEmpty(lbl) != true) { index++; }
}
 
I re-read my post from last night and that is incorrect syntax in one location as well as iterating the index of my foreach loop not the index of the TLP...
I think I am following what you mean about using the reowindex of the TLP - but my syntax is still not thar.

Google helps :)

VB.NET:
int rowinde = 0;
foreach (string label in c.LabelsNeeded)
{
    //Adding in a label
    Label lbl = new Label();
    lbl.Name = "lbl_" + index;
    lbl.Text = label;
    lbl.AutoSize = true;    
    tableLayoutPanel1.Controls.Add(lbl);
    //Adding in a textbox
    TextBox tbx = new TextBox();
    tbx.Name = "txt_" + index;
    tbx.AutoSize = true;
    //Only increment the index if the label is not null
    //meaning there was a new label added
    if (string.IsNullOrEmpty(lbl.Text) != true) { tableLayoutPanel1.Controls.Add(tbx); }
    else { tableLayoutPanel1.Controls.Add(tbx, rowindex++) } 
    index++
}
 
Last edited:
Why are you posting C# code for a start? As the name suggests, this is a VB.NET-specific site. We have a C# sister site so I would encourage you to post your there (link at the top of this page) if you want help with C# code.
 
Status
Not open for further replies.

Latest posts

Back
Top