Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > An array of dynamic controls


Reply
 
Thread Tools Display Modes
  #1  
Old 06-04-2004, 04:55 AM
Qacer Qacer is offline
Newcomer
 
Join Date: May 2004
Posts: 12
Question An array of dynamic controls


Hello all,

I've been reading tutorials on creating dynamic controls. However, I can't seem to find how to create a certain number of them based on a user's input. I think I was able to do this before using Load(), but now, I'm using a different method that uses Set and adding it to the controls collection.

I'm just a beginner, so I really don't know VB well. What I'm trying to do is to generate a number of text boxes based on how many of them a user specifies. Then, I'm going to take each value from the text box and manipulate the values.

Any simple examples? Tutorials? Tips?

Thanks!

CD
Reply With Quote
  #2  
Old 06-04-2004, 05:05 AM
darkforcesjedi's Avatar
darkforcesjedi darkforcesjedi is offline
Trust me, I'm an

* Expert *
 
Join Date: Apr 2001
Location: In ur base, pwnin d00dz
Posts: 1,961
Default

Yes. Add the first text box in the array to the form. At design time set its index property to 0 (or 1 if you prefer). To add more controls to the array just Load Text1(Index). All the events of Text1 will return the Index of the text box that raised the event.
__________________
To err is human; to debug, divine.
Reply With Quote
  #3  
Old 06-04-2004, 05:06 AM
candela's Avatar
candela candela is offline
Senior Contributor
 
Join Date: May 2002
Location: Roma
Posts: 838
Default

i think the load method is the right one.
u'll have:
text1 as a collection, with count,item, lbound,ubound property
text1(1), text1(2), text1(3), ... as elements of collection, with textbox property

So u can add control simply:
Code:
dim sNEWindex
sNEWindex=text1.ubound+1
load text1(sNEWindex)
text1(sNEWindex).move 'some where on your form ;)
text1(sNEWindex).visible=true
hope this help u....

PS. of course u have to set the index property of the first textbox u put on the form to an integer value, not empty , usually 0
__________________
_ _ . _ ._. _._. _ _ _ ._._.
Reply With Quote
  #4  
Old 06-04-2004, 05:09 AM
KAL_0801's Avatar
KAL_0801 KAL_0801 is offline
Centurion
 
Join Date: Apr 2004
Location: New York City
Posts: 174
Default

When you load a new control to always change the 'Visible' property to 'True' and change its positon (like candela said). The default setting of every control's 'Visible' property is 'False', and its position is the same as the one's before it.
__________________

"If a little knowledge is dangerous, where is a man who has so much as to be out of danger?"
-Thomas Huxley
Reply With Quote
  #5  
Old 06-04-2004, 05:21 AM
shashidharkr shashidharkr is offline
Centurion
 
Join Date: Oct 2003
Posts: 165
Default

This example may help you.
Code:
Private Sub Command1_Click() Dim intNum As Integer For i = 1 To Val(Text2.Text) Load Text1(i) Text1(i).Left = Text1(i - 1).Left + Text1(i - 1).Width + 100 Text1(i).Top = Text1(i - 1).Top Text1(i).Visible = True Next End Sub
Here, Text2 is where user enters the numbers. You may have to manipulate the position of text boxes depending on your requirement. In the example mentioned above, they are placed next to each other with a gap of 100.
Reply With Quote
  #6  
Old 06-04-2004, 08:30 AM
Qacer Qacer is offline
Newcomer
 
Join Date: May 2004
Posts: 12
Default

Thanks, all!

My current code for generating 1 control looks like this:

Dim sstab1cntcmd As Control

Set sstab1cntcmd = Controls.Add("VB.CommandButton", "sstab1cntcmd1", SSTab1)

With sstab1cntcmd
.Visible = True
.Caption = "This is me"
.Left = 0.1 * SSTab1.Left + SSTab1.Left
.Top = 0.2 * SSTab1.Top + SSTab1.Top
End With

I was using Load() before, but I couldn't figure out how to add it to a container. I am able to do it with the above code. Any other tips?

Thanks!
Reply With Quote
  #7  
Old 06-04-2004, 08:32 AM
darkforcesjedi's Avatar
darkforcesjedi darkforcesjedi is offline
Trust me, I'm an

* Expert *
 
Join Date: Apr 2001
Location: In ur base, pwnin d00dz
Posts: 1,961
Default

If the first control is in a container and you use load to add more to the array, they'll be in the same container.
__________________
To err is human; to debug, divine.
Reply With Quote
  #8  
Old 06-04-2004, 08:40 AM
candela's Avatar
candela candela is offline
Senior Contributor
 
Join Date: May 2002
Location: Roma
Posts: 838
Default

the code i've post related to a textbox works fine...
as shashidharkr one do....

u just have to change text with other kind of control like commandbutton

what's the problems whit this 2 examples?

what do u mean whit other tips? or container?
read what i've post....

the only suggestion is that shashidharkr code couldn'b be used recursivity...
cause it refers to an absolute index...
otherwise u have to use a relative index like Ubound... as i post before
__________________
_ _ . _ ._. _._. _ _ _ ._._.
Reply With Quote
  #9  
Old 06-04-2004, 08:57 AM
Diurnal Diurnal is offline
Enthusiast

Retired Leader
* Expert *
 
Join Date: Apr 2002
Location: Bellevue, WA.
Posts: 3,233
Default

Maybe you know this already...
Code:
'Example: Set Command1.Container = Frame1
Reply With Quote
  #10  
Old 06-06-2004, 04:37 PM
Qacer Qacer is offline
Newcomer
 
Join Date: May 2004
Posts: 12
Default

Thanks all!

Now, I have some more questions. Is there someway to remove the loaded controls (such as Unload() ) or reuse existing controls?

My problem now is that when I generated the controls and try to change the number again it gives me an "Object is loaded error message."

Would there be a better approach to unloading an object and re-loading it?
After running my little program a few times, it seems that there is a very slight pause when loading the dynamically generated objects.

Last edited by Qacer; 06-06-2004 at 04:53 PM.
Reply With Quote
  #11  
Old 06-07-2004, 09:45 AM
candela's Avatar
candela candela is offline
Senior Contributor
 
Join Date: May 2002
Location: Roma
Posts: 838
Default

u can do 2 things:
1) unload all the control loaded (u just have to leave one of them)
Code:
for each myCTR in me.TEXT1
     if myCTR.index>0 then unload myCTR
next
' now load all the control u need....
otherwise u have to unload some control if they are too much, or load some, if u need them...
Code:
if (newNUMBER-1) < me.text1.UBOUND THEN
    for i = newNUMBER to me.text1.UBOUND
        unload me.text1(i)
    next
else
    for i = me.text1.UBOUND to (newNUMBER-1)
        load me.text1(i)
        ' make it visible and position it....
    next
endif
__________________
_ _ . _ ._. _._. _ _ _ ._._.
Reply With Quote
  #12  
Old 06-08-2004, 08:13 AM
Qacer Qacer is offline
Newcomer
 
Join Date: May 2004
Posts: 12
Default

Thank you very much, candela!

I'm looking at this code:

for each myCTR in me.TEXT1
if myCTR.index>0 then unload myCTR
next


Can you clarify what myCTR and TEXT1 are?

I'm guessing myCTR is the name of my control array and TEXT1 is my form name? Sorry, I'm only a beginner.

In my code, I have txtInput as my control array. I tried doing the following:

Dim i, indxCount as Integer

indxCount = txtInput().count

For i = 1 to indxCount - 1
Unload txtInput(i)
Next

But it didn't work. It said something about object already loaded. I would like to try out your code, but I can't figure out the myCTR and TEXT1 variables.

Thank you once again!
Reply With Quote
  #13  
Old 06-08-2004, 08:37 AM
candela's Avatar
candela candela is offline
Senior Contributor
 
Join Date: May 2002
Location: Roma
Posts: 838
Default

well, it's my foult... is better to explain that write just code

so i make a loop through all the control of my collection
and there is two way of doing this, using, as u have done, an index (integer), that you use from a lover value to an upper one.
Otherwise (that's what i've done) u can make a loop through the objects of the collection, so instead of an index that grow, u have a control itself.

Let me explain:
for i = 1 to 10 makes 10 loops, and u can do what u want with index i

for each myCTR in me.TEXT1 means that me.TEXT1 has to been an array (not a single text box), so myCTR won't be an index but a text box of the collection, and the loop start for the first, and finisch with last one (u r sure about this .
so my loop discard all the textbox i've created, except the one whoose index=0, cause i can't discard all.

in the second case i'm sure that i do what i want, with no way of mistake, in the other way it's quite the same, but u have to think a little more about index and counter...

just a stupid example:
i can do:
LOAD me.text1(1)
LOAD me.text1(5)
LOAD me.text1(13)
LOAD me.text1(25)
it's stupid of course.... but not wrong!!!
i just can unload them using the second method...
cause it takes the control (it is not interested in their index )

so if txtInput is your control
Code:
for each myCTR in txtInput
if myCTR.index>0 then unload myCTR
next
the type of myCTR is the type of a single element of txtInput
u can leave it undefined
dim myCTR

u can define it in general:
dim myCTR as Control

or u u can give it the type of txtInput
dim myCTR as textbox if is a textbox
dim myCTR as Label if is a Label
... and so on...

that's what u were looking for?
__________________
_ _ . _ ._. _._. _ _ _ ._._.
Reply With Quote
  #14  
Old 06-10-2004, 11:20 AM
Qacer Qacer is offline
Newcomer
 
Join Date: May 2004
Posts: 12
Default

Thanks again, candela! You came through again.



I actually got my mini-program working. I just need to add some events to the original control textbox to do some error correcting and such.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->