1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

VB Combo Box

Discussion in 'Software Development' started by bear, Oct 12, 2003.

Thread Status:
Not open for further replies.
Advertisement
  1. bear

    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
     
  2. AbvAvgUser

    AbvAvgUser

    Joined:
    Oct 3, 2002
    Messages:
    2,281
    Your query is not clear. What exactly do you want to do?

    Explain clearly.
     
  3. 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
     
  4. coderitr

    coderitr

    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
     
  5. bear

    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???
     
  6. coderitr

    coderitr

    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.
     
  7. bear

    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
     
  8. AbvAvgUser

    AbvAvgUser

    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?
     
  9. coderitr

    coderitr

    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.
     
  10. bear

    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
     
  11. coderitr

    coderitr

    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
     
  12. bear

    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
     
  13. AbvAvgUser

    AbvAvgUser

    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
     
  14. coderitr

    coderitr

    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.
     
  15. bear

    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.....
     
  16. Sponsor

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/171446

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice