Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 10th, 2005, 2:01 AM   #1
chepfaust
Programmer
 
chepfaust's Avatar
 
Join Date: Feb 2005
Posts: 62
Rep Power: 4 chepfaust is on a distinguished road
need some general optimization help :)

hello,

i have eight comboboxes, called cb1 to cb8. if i wanted to perform a function on each combobox, is there any way i could use a <for x = 1 to 8> loop to perform the action on combobox cb(x), or am i stuck having to maintain eight individual and separate sets of code?

my rationale is that every time need to change something with one combobox, i have to go tweak the code for seven other comboboxes. it would be easier to change it once and let a loop handle the rest, but i'm not sure if it's possible to do this in vb. sorry if this seems like a trite question

thanks
chepfaust is offline   Reply With Quote
Old Mar 10th, 2005, 9:50 AM   #2
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
Yup for sure. You just need to creat a control array. Instead of naming your combo boxes cb1, cb2 etc. Name the first one then copy the control, and the Microsoft Visual Basic Forum editor (i don't know what it's really called) will ask you if you want to create a array. Say yes and keep copying the combo boxes till you have enough.
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Old Mar 10th, 2005, 2:54 PM   #3
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
Depending on what you've got going you could try a
For each MyComboBox in Me
type loop, where MyComboBox is well... a combobox.
Otherwise do an array of controls, however it's till neater (and faster) to use a For Each rather than an arbitrary number (lbound and ubound will not work on control arrays)
Rory is offline   Reply With Quote
Old Mar 10th, 2005, 7:41 PM   #4
chepfaust
Programmer
 
chepfaust's Avatar
 
Join Date: Feb 2005
Posts: 62
Rep Power: 4 chepfaust is on a distinguished road
pizientos: thanks for pointing out the value of control arrays, i'll definitely look into those if i can't figure out what rory's getting at.

rory: the concept of control arrays i can understand, but i'm not wholly understanding your usage of the "for each" loop and how it would work in my favor. if i were to type "for each cb1 in me" (what is "me," by the way?) would i have to rename all the comboboxes to "cb1" inside a control array or else how does vb know to tell the difference between all the comboboxes?

thanks guys.

edit- hey, a quick general vb question. when declaring subroutines, what exactly is the trailing () for? i've figured out after some research following a lengthy trial and error period that in order to make a control array subroutine work, i need to declare a sub like:

private sub cb_click(index as integer)

but when i look at others' code for various solutions to other problems, i see all sorts of stuff like byval __ and whatnot in the parentheses. exactly what is the purpose of these parentheses?

Last edited by chepfaust; Mar 10th, 2005 at 10:19 PM.
chepfaust is offline   Reply With Quote
Old Mar 14th, 2005, 3:01 PM   #5
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
For each will iterate through each element of the control variable's type on the parent object. So in VB.NET if you did For Each MyControl as TextBox in Form1 the iterator would run once for each textbox on Form1, the MyControl object pointing in turn to each textbox. However, VB6 in its own way won't like it, and you have to use a variant control variable and check if it is a textbox, meaning that you're coding blind (no autocomplete).
This is not ideal, but the other method of using a number to access the index property would have to be hardcoded (basically ubound and lbound don't work, which you would need for a normal For loop).

The index argument in the event just tells you the index of the control that raised the event, so if a clicked control number 3 in an array of command buttons, the event would fire with the index parameter being equal to 2 (control arrays start at 0).
Strictly speaking, this should be passed ByVal, and as the first argument in each event definition, so the first argument of MouseDown would be Index, followed by Shift, x and y.
Rory is offline   Reply With Quote
Old Mar 14th, 2005, 9:24 PM   #6
chepfaust
Programmer
 
chepfaust's Avatar
 
Join Date: Feb 2005
Posts: 62
Rep Power: 4 chepfaust is on a distinguished road
Quote:
Originally Posted by Rory
For each will iterate through each element of the control variable's type on the parent object. So in VB.NET if you did For Each MyControl as TextBox in Form1 the iterator would run once for each textbox on Form1, the MyControl object pointing in turn to each textbox. However, VB6 in its own way won't like it, and you have to use a variant control variable and check if it is a textbox, meaning that you're coding blind (no autocomplete).
This is not ideal, but the other method of using a number to access the index property would have to be hardcoded (basically ubound and lbound don't work, which you would need for a normal For loop).

The index argument in the event just tells you the index of the control that raised the event, so if a clicked control number 3 in an array of command buttons, the event would fire with the index parameter being equal to 2 (control arrays start at 0).
Strictly speaking, this should be passed ByVal, and as the first argument in each event definition, so the first argument of MouseDown would be Index, followed by Shift, x and y.
i appreciate your efforts to explain the 'for each' concept, but i'm still not understanding the implementation of it as it pertains to vb6. in the meantime my regular 'for' loops seem to work, so i'll leave well enough alone. my program isn't that much of a memory hog that looping methods should be a concern :\

the purpose of event arguments, on the other hand, i now do understand. thanks
chepfaust is offline   Reply With Quote
Old Mar 15th, 2005, 6:07 PM   #7
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
Ok no problem!
Rory is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:27 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC