Click here to Skip to main content
15,920,704 members
Home / Discussions / C#
   

C#

 
QuestionI try to dynamically generate a name for my objects Pin
Member 1019526215-Oct-15 10:41
Member 1019526215-Oct-15 10:41 
AnswerRe: I try to dynamically generate a name for my objects Pin
BillWoodruff15-Oct-15 18:56
professionalBillWoodruff15-Oct-15 18:56 
GeneralRe: I try to dynamically generate a name for my objects Pin
Member 1019526219-Oct-15 7:11
Member 1019526219-Oct-15 7:11 
QuestionAccessing dynamically generated textboxes Pin
KakitaIppatsu15-Oct-15 5:43
KakitaIppatsu15-Oct-15 5:43 
AnswerRe: Accessing dynamically generated textboxes Pin
OriginalGriff15-Oct-15 6:11
mveOriginalGriff15-Oct-15 6:11 
GeneralRe: Accessing dynamically generated textboxes Pin
KakitaIppatsu15-Oct-15 6:39
KakitaIppatsu15-Oct-15 6:39 
GeneralRe: Accessing dynamically generated textboxes Pin
OriginalGriff15-Oct-15 7:06
mveOriginalGriff15-Oct-15 7:06 
AnswerRe: Accessing dynamically generated textboxes Pin
BillWoodruff15-Oct-15 6:52
professionalBillWoodruff15-Oct-15 6:52 
The WinForms ControlCollection does allow you to use a string Key to access members:
C#
pn.Controls["txtProdName3"].Text = "hello";
When you set the 'Name property of a WinForm Control, you have created the Key (why MS documentation calls it a 'Key baffles me).

So, that is a way you could access the run-time created TextBoxes without keeping a direct reference to them in some data structure.

However, I strongly advise you not to rely on that, and suggest you do something like this:
C#
public List<TextBox> ListOfTextBoxes = new List<TextBox>();

public void createTextBoxes(int howMany)
{
    TextBox txtProdName;
    TextBox txtProdDesc;

    for(int i = 0; i < howMany; i++)
    {
        txtProdName = new TextBox();
        txtProdDesc = new TextBox();

        ListOfTextBoxes.Add(txtProdName); // keep a reference !
        ListOfTextBoxes.Add(txtProdDesc);
        
        txtProdName.Name = "txtProdName " + i;
        pn.Controls.Add(txtProdName);

        txtProdDesc.Name = "txtProdDesc " + i;
        pn.Controls.Add(txtProdDesc);
    }
}

// sample uses in code:

ListOfTextBoxes[2].Text = "blah";

// or, take advantage of the string Key of a ControlCollection

pn.Controls["txtProdName3"].Text = "hello";

string tbContent1 = ListOfTextBoxes[0].Text;
But, if you are going to use the Index to access the TextBoxes, you can see immediately that you'll have to remember which index to use for TextBoxes that are 'txtProdDesc, and which to use for 'txtProdName.

To make the code clearer, I would use two separate Lists. I would also consider using two Dictionaries of Type <string, TextBox>

If the two TextBoxes are designed to function as a "pair;" I'd consider another data structure, or consider creating a custom UserControl ... all depends on your Application and its scope, purpose, run-time load, etc.
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.


modified 15-Oct-15 14:53pm.

GeneralRe: Accessing dynamically generated textboxes Pin
KakitaIppatsu15-Oct-15 7:47
KakitaIppatsu15-Oct-15 7:47 
QuestionThe “Microsoft.CodeAnalysis.BuildTasks.Csc” task could not be loaded from the assembly Pin
Member 1204569215-Oct-15 3:31
Member 1204569215-Oct-15 3:31 
QuestionTime to draw 2 usercontrols on a form is depend on each other. Pin
LeHuuTien14-Oct-15 16:10
LeHuuTien14-Oct-15 16:10 
AnswerRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
BillWoodruff14-Oct-15 20:37
professionalBillWoodruff14-Oct-15 20:37 
GeneralRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
LeHuuTien14-Oct-15 22:27
LeHuuTien14-Oct-15 22:27 
GeneralRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
BillWoodruff14-Oct-15 22:38
professionalBillWoodruff14-Oct-15 22:38 
GeneralRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
LeHuuTien14-Oct-15 23:10
LeHuuTien14-Oct-15 23:10 
GeneralRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
Pete O'Hanlon14-Oct-15 23:13
mvePete O'Hanlon14-Oct-15 23:13 
GeneralRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
LeHuuTien14-Oct-15 23:22
LeHuuTien14-Oct-15 23:22 
GeneralRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
Pete O'Hanlon14-Oct-15 23:42
mvePete O'Hanlon14-Oct-15 23:42 
GeneralRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
LeHuuTien14-Oct-15 23:45
LeHuuTien14-Oct-15 23:45 
GeneralRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
BillWoodruff15-Oct-15 8:24
professionalBillWoodruff15-Oct-15 8:24 
AnswerRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
Gerry Schmitz15-Oct-15 9:38
mveGerry Schmitz15-Oct-15 9:38 
GeneralRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
LeHuuTien15-Oct-15 16:36
LeHuuTien15-Oct-15 16:36 
GeneralRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
Gerry Schmitz15-Oct-15 17:10
mveGerry Schmitz15-Oct-15 17:10 
GeneralRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
LeHuuTien15-Oct-15 21:49
LeHuuTien15-Oct-15 21:49 
GeneralRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
Gerry Schmitz16-Oct-15 8:21
mveGerry Schmitz16-Oct-15 8:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.