VB Combo Box

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

bear

Thread Starter
Joined
Jan 1, 1999
Messages
110
Help,
I am needing some help with a combo box. I am wanting to be able to get labels off the screen from and have them be listed in the combo box, for user choice. Is it possible to do this, and if so what coding do I use and where should the coding be placed??

Thanks,

Bear
 

bear

Thread Starter
Joined
Jan 1, 1999
Messages
110
Sorry about that......let explain it this way....I have a a Form called Form1.frm, and on this form are labels of stations. I am wanting to be able to get those station names into the combo box on Form2.frm, without typing them into the combo box. How do I get those station names in Form1.frm to list into Form2.frm without typing them in???. What coding would I use in the sub to do that???

Bear
 
Joined
Oct 12, 2003
Messages
3,015
Code a menu item like "Add to Form2 Combo". Make the parent menu invisible.

Now on the label click event test if it was a right-click and using the PopupMenu function display the popup.

When the user clicks the menu option ...

Form2.Combo1.AddItem Label1.Caption
 

bear

Thread Starter
Joined
Jan 1, 1999
Messages
110
Ok, Here is what I did, but for some reason it still will not get the captions off form1

in form1, in the Form1_Paint sub() I put the following code

TWC.cbofrom.additem

When I try to add the following this

Namlbl.caption

after additem I get this error

Method or Data Member Not Found


What I have forgot to do??? Should I dim the Namelbl something other than a string???
 
Joined
Oct 12, 2003
Messages
3,015
The Namelbl -- or whatever is to the left of .Caption needs to be the name of the label control on your form.
 

bear

Thread Starter
Joined
Jan 1, 1999
Messages
110
I must be doing something wrong i the declaration or something. It will not work using Namelbl.caption ...... However when I use the caption for the main form, it shows up great in the combo box in form2.....something is wrong some where...

Bear
 
Joined
Oct 3, 2002
Messages
2,281
In the form which contains the combo box, have this code.

cboList.AddItem form1.label1.caption

I think this is the syntax. Does this not work?
 
Joined
Oct 12, 2003
Messages
3,015
You cannot use the .Caption with a local variable. The caption property applies to the label object. You add a label object to your form, set the properties and in some event procedure you add the text to the combo box on the other form by the following code:

Form2.Combo1.AddItem Label1.Caption

Label1 is an object -- not a variable!!

Also, if you put the above code in the Form_Paint event you will find that the text is added to the combo box more than once. The Form_Paint event is fired every time the window is moved, resized, covered by another window, gains the focus, etc. Try putting a msgbox call in Form_Paint and see how many times it comes up. You should write your own code to add the text to the combo on demand -- like a button or menu click procedure.
 

bear

Thread Starter
Joined
Jan 1, 1999
Messages
110
Could the problem be that the Namelbl is indexed??? When I put the following code for the combo box, it works for that one label:

cbofrom.addtiem Ns1.Namelbl(5).caption

But when I try to put the following code in there:

cbofrom.additem NS1.Namelbl(1 to 30).caption

I get an error
 
Joined
Oct 12, 2003
Messages
3,015
I threw together a sample program to test what (I think) you are trying to do. I found that if you code the additem method in an event procedure of a button or menu item you don't know anymore which label you clicked on. If you click on the label itself you can do this with one line of code in the Label1_Click event:

Form2.Combo1.AddItem Label1.Caption

If Label1 is indexed (part of a control array) then the sub definition and call will include the index of the array node you clicked on and the above code will read:

Form2.Combo1.AddItem Label1(Index).Caption

You could have one label and one button for each and set up a control array on both sets. If you make the index the same for each label/button pair then you can assume that the index passed to your button click event is the same as the index of the label that you're after.

If you're trying to add all of the items at once (the 1 to 30 thing) then you cannot perform loops like that. If thats what you want to do then do this ...

Dim n as Long
For n = 1 to 30
Form2.Combo1.AddItem Label1(n).Caption
Next
 

bear

Thread Starter
Joined
Jan 1, 1999
Messages
110
The For - Next loop worked the way i needed it to........THANKS!!!!!!!!
Now, one more question

If I don't know the actual number of the index arrary, then what coding do I add in the line

for n = 1 to (number of labels)
form.combo1. additem Label1(n).caption
next


The program I am trying to write, the number of labels is based on the user input.

Again, thanks for you help, as it is much appreciated.

Bear
 
Joined
Oct 3, 2002
Messages
2,281
See if For Each...Next Statement is of any use.

Here's the MSDN explanation on the same
For Each...Next Statement


Repeats a group ofstatements for each element in anarray orcollection.

Syntax

For Each element In group
[statements]
[Exit For]
[statements]

Next [element]

The For...Each...Next statement syntax has these parts:

Part Description
element Required.Variable used to iterate through the elements of the collection or array. For collections, element can only be aVariant variable, a generic object variable, or any specific object variable. For arrays, element can only be a Variant variable.
group Required. Name of an object collection or array (except an array ofuser-defined types).
statements Optional. One or more statements that are executed on each item in group.


Remarks

The For...Each block is entered if there is at least one element in group. Once the loop has been entered, all the statements in the loop are executed for the first element in group. If there are more elements in group, the statements in the loop continue to execute for each element. When there are no more elements in group, the loop is exited and execution continues with the statement following the Next statement.

Any number of Exit For statements may be placed anywhere in the loop as an alternative way to exit. Exit For is often used after evaluating some condition, for example If…Then, and transfers control to the statement immediately following Next.

You can nest For...Each...Next loops by placing one For...Each...Next loop within another. However, each loop element must be unique.

Note If you omit element in a Next statement, execution continues as if element is included. If a Next statement is encountered before its corresponding For statement, an error occurs.

You can't use the For...Each...Next statement with an array of user-defined types because a Variant can't contain a user-defined type.
 
Joined
Oct 12, 2003
Messages
3,015
Dim n as Long
For n = 1 to UBound(Label1) - 1
Form2.Combo1.AddItem Label1(n).Caption
Next

UBound returns the upper boundary of an array. The -1 is because arrays are 0 based -- meaning that if you have 10 elements they are number 0 - 9 not 1 - 10.
 

bear

Thread Starter
Joined
Jan 1, 1999
Messages
110
Thanks again for your help, as every suggestion on the FOR...NEXT loop worked.....It gives me something to work with now.....
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top