n00b VB 2005 problem

mic_y

Expert Member
Joined
Dec 23, 2004
Messages
1,645
Ok, basically, I have created a whole bunch of textboxes on a form during runtime, and now, want to, once a button is clicked, send the text contained in all of them to a listbox.

all the textboxes that are created are named txtbox0 - txtboxn.

Now how would I go about doing this in a for loop?
 

mic_y

Expert Member
Joined
Dec 23, 2004
Messages
1,645
Were you by any chance a VB 6 developer, and looking for the old control array way of doing things?

Have you added those textboxes to the form using the designer? If so it is possible to iterate over your textboxes but you have to go something like:
Code:
foreach (Control control in Me.Controls)
   if (control is TextBox) then
     ' Do stuff
   end if
Next

Maybe look at this on how to add the TextBox's to a list and then iterate over them.

Hey dequadin,

No, I am not a VB 6 dev. technically I am not even a dev at all, just need to get some stuff done for a university project, and am really struggling here ;)

No, the controls are added at runtime, and I assign each one a name at runtime of "txtBox" & i.

Looked at those about.com articles, but it all seems a bit confusing...

Basically, from what I understand in VB 6 it was possible to do something like this:

Code:
For i = 1 to n
    listbox1.items.add(txtbox(i).name)
Next

And this is not possible any more in VB 2005/.net

Anything else I can try to achieve a similar result?
 
Last edited:

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
So you want to add textboxes to a listbox / panel at runtime? Why not just add them to a Panel? It's main purpose is to act as a container for other controls... You can then do the following, assuming that you have a Panel named "pnlContainer":
Code:
for (int i = 0; i < n; i++)
{
    Textbox txtBox = new Textbox();
    txtBox.Name = "txtBox" + i.ToString();
    pnlContainer.Controls.Add(txtBox);
}
 

mic_y

Expert Member
Joined
Dec 23, 2004
Messages
1,645
uuum, not quite Farlig...

I want to add the text that was entered into the textboxs into a listbox, but since the number of textboxes is unknown (they are generated at runtime), not quite sure how to do this...
 

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
Hey dequadin,

No, I am not a VB 6 dev. technically I am not even a dev at all, just need to get some stuff done for a university project, and am really struggling here ;)

No, the controls are added at runtime, and I assign each one a name at runtime of "txtBox" & i.

Looked at those about.com articles, but it all seems a bit confusing...

Basically, from what I understand in VB 6 it was possible to do something like this:

Code:
For i = 1 to n
    listbox1.items.add(txtbox(i).name)
Next
[\Code]

And this is not possible any more in VB 2005/.net

Anything else I can try to achieve a similar result?[/quote]

The code mentioned in his reply to you will work because at runtime, the textboxes are created.

Me.Controls will expose all of the textboxes (that have been created) for you on a button click. (As per the example)

Unless you have an infinite loop going and textboxes are being created all the time....???

Remember kids. Once the onload finishes, you can do pretty much whatever you want with the code suggested in the original reply
 

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
FYI, self-proclaimed n00b. Just try the code before saying it won't work. You'd be surprised how much we (non-n00bs) know
 

Nerfherder

Honorary Master
Joined
Apr 21, 2008
Messages
29,703
uuum, not quite Farlig...

I want to add the text that was entered into the textboxs into a listbox, but since the number of textboxes is unknown (they are generated at runtime), not quite sure how to do this...

listbox1.items.add(txtbox1.text)
listbox1.items.add(txtbox2.text)
istbox1.items.add(txtbox3.text)
listbox1.items.add(txtbox4.text)
 

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
listbox1.items.add(txtbox1.text)
listbox1.items.add(txtbox2.text)
istbox1.items.add(txtbox3.text)
listbox1.items.add(txtbox4.text)

yea but the textboxes are unknown up and till runtime?



Again, maybe this hasn't sunk into the OP yet. After Form Load event fired and generated the textboxes, the code in the original reply will work because after the form load event fired and generated the textboxes. it stops. Then onclick of the button, it will then loop through all of those textboxes.

get it? got it? no? education FAIL :(
 

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
@Acid his problem isn't adding the textboxes, it iterating over them to get the data out.

I know that. But he clearly doesn't understand when these magically texty-textboxes are being created and how your code would've worked in the first place.
 

mic_y

Expert Member
Joined
Dec 23, 2004
Messages
1,645
Hey guys, thanks for the help so far...

As dequadin understood, i am not having issues adding textboxes at run time.

I made a mistake of not explaining that the textboxes are not added with the onload event. I have a button on the form that adds the textboxes, and then another button to place contents of the textboxes into a listbox...

Obviously, didnt outline that in the OP... sorry...
 

mic_y

Expert Member
Joined
Dec 23, 2004
Messages
1,645
And now, I have another issue...

how can I create a click event handler for a label that was created during runtime?
 

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
Hey guys, thanks for the help so far...

As dequadin understood, i am not having issues adding textboxes at run time.

I made a mistake of not explaining that the textboxes are not added with the onload event. I have a button on the form that adds the textboxes, and then another button to place contents of the textboxes into a listbox...

Obviously, didnt outline that in the OP... sorry...

No worries dude, I guess the problem you're facing is a process flow problem. What would you want to do and how it flows so the text then goes to this listbox from all the dynamically created textboxes. Because a user can add another textbox right after he clicked the "send text to listbox" button.

Up to you to decide how to handle the problem because it is your program so you'd know best. We just show you the code dude...
 

mic_y

Expert Member
Joined
Dec 23, 2004
Messages
1,645
Thx Acid, worked perfectly, with a bit of extra googling... I seriously dont think i have learnt this much in one day in ages...
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
uuum, not quite Farlig...

I want to add the text that was entered into the textboxs into a listbox, but since the number of textboxes is unknown (they are generated at runtime), not quite sure how to do this...

Oh, alright. I misread the OP. Glad the other guys were able to help you...
 
Top