Getting the contents of TextBoxes created "on the fly"

desmo

Member
Joined
Jan 3, 2018
Messages
14
Programming Experience
Beginner
I've got a TableLayoutPanel (called userPanel) where I create 3 rows containing one label and two text boxes. At the application startup, I create them like this:

C#:
[FONT=courier new]public MainForm() {[/FONT]
[FONT=courier new]   InitializeComponent();

[/FONT][FONT=courier new]   for (int i = 0; i < userPanel.RowCount; i++) {[/FONT]
[FONT=courier new]      userPanel.Controls.Add(new Label() { Text = "Login"});[/FONT]
[FONT=courier new]      userPanel.Controls.Add(new TextBox());[/FONT]
[FONT=courier new]      userPanel.Controls.Add(new TextBox());[/FONT]
[FONT=courier new]   }
}[/FONT]

Now, is it possible to reference these labels and text boxes created above? Since they don't have names, I haven't found a way to get the contents.

(Specifically, what I want to do is to store the contents in the application.settings, and then load them at the next startup. Which I know how to do.)
 
Last edited:
You can get Item from Controls either by int index or string key. Key is the Name property of the control that you may set, for example by using the row index as part of the name.
 
Hm. I can't see how? There's .Control.IndexOf(), but that requires a named reference to the control, as far as I can see? I don't see a way to get the contents of a for instance "control number 1" in the table?

I'm creating 3 rows with 3 controls on each row. What I want is a way to say "Give me the Text contents of controls number 2 and 3 in each row".

The reason I'm not creating and naming controls, which would make this easier, is that I have an "Add row" button which lets me add extra rows.
 
userPanel.Controls.Item[index or name]
The reason I'm not creating and naming controls, which would make this easier, is that I have an "Add row" button which lets me add extra rows.
You could still give names utilizing RowCount, that is also what the indexer of your loop targets.
What I want is a way to say "Give me the Text contents of controls number 2 and 3 in each row".
Your current loop steps 1 (i++), instead step by 3 (i+=3) to get the first index of each row - then you can add 1 or 2 to that to get the second and third control each row.
 
Thanks. The looping is not a problem - I'm not THAT much of a noob :D

I'm not able to get .Item[] to work: "TableLayoutControlCollection does not contain a definition for 'Item'..."

However, after much messing and looking around, I found this:

userPanel.GetControlFromPosition(1, 0).Text
 
You can omit ".Item" > ...Controls[index or name]
GetControlFromPosition is also useful.
 
Back
Top Bottom