Changing Labels based on Combobox selection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drudge
    New Member
    • Aug 2007
    • 6

    Changing Labels based on Combobox selection

    I have a combo box named cboGear. I have my selections for that combo box added when the Form loads. (ie. cboGear.AddItem "Guitar", cboGear.AddItem "Amps".)

    What I would like to happen is when an item is selected from cboGear, six labels that I have made will populate with info. For example if "Guitar" is selected from cboGear, I would like label1 to read, "1 cord", label2 to read "2 extra strings", label3 to read "3 tuner".

    I really dont have much, if any, background in vb. I'm just learning on my own in my spare time, so I apologize for ignorance early, heheh.
    Last edited by SammyB; Aug 27 '07, 10:10 PM. Reason: Non-descriptive title
  • lee123
    Contributor
    • Feb 2007
    • 556

    #2
    hey you could use the if statement on this in there ok :

    Code:
    Private Sub cbogear_Click()
        
          If cbogear = "guitar" Then
            Text1 = "cord"
            Text2 = " 2 extra strings"
            Text3 = "tuner"
    
        End If


    lee123

    Comment

    • drudge
      New Member
      • Aug 2007
      • 6

      #3
      Originally posted by lee123
      hey you could use the if statement on this in there ok :

      Code:
      Private Sub cbogear_Click()
          
            If cbogear = "guitar" Then
              Text1 = "cord"
              Text2 = " 2 extra strings"
              Text3 = "tuner"
      
          End If


      lee123

      I added that code to the cboGear_Click() and it did nothing for me. the labels and text boxes kept their initial values. no run-time errors or anything when testing. we have vb6 at the lab.

      Comment

      • lee123
        Contributor
        • Feb 2007
        • 556

        #4
        it's because i used text boxes instead of labels but it worked with text boxes

        lee123

        Comment

        • lee123
          Contributor
          • Feb 2007
          • 556

          #5
          it won't do it with labels but so far i have tried textboxes and it shows and option buttons(checkbo xes) and it will do the same sorry i didn't read you question carefully i seen the word text and thought textboxes sorry..

          lee123

          Comment

          • lee123
            Contributor
            • Feb 2007
            • 556

            #6
            well try this in the click event:

            Private Sub cbogear_Click()

            If cbogear = "guitar" Then
            Label1 = "cord"
            Label2 = " 2extra strings"
            Label3 = "tuner"

            End If

            because it worked for me..

            lee123

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Another suggestion, just for more easily readable code. Rather than IF, use Select Case. For example...

              [CODE=vb]Private Sub cbogear_Click()
              Select Case Ucase(cboGear.T ext)
              Case "GUITAR"
              Label1 = "1 Cord"
              Label2 = "2 Extra strings"
              Label3 = "3 Tuner"
              Case "AMPS"
              Label1 = "Whatever"
              ' ...
              End Select
              End Sub[/CODE]Note also, if your IF tests are failing, it's possible the case doesn't match. Which is why I converted to UPPERCASE before comparing, just to be sure.

              Comment

              • drudge
                New Member
                • Aug 2007
                • 6

                #8
                what if I have 3 label boxes and I want to add up the numbers from label1 and label2 and display them in label3 once an item has been selected from a combo box?

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by drudge
                  what if I have 3 label boxes and I want to add up the numbers from label1 and label2 and display them in label3 once an item has been selected from a combo box?
                  Have you tried it? Shouldn't be too hard.

                  Comment

                  • drudge
                    New Member
                    • Aug 2007
                    • 6

                    #10
                    Originally posted by Killer42
                    Have you tried it? Shouldn't be too hard.
                    only thing i can do is to get the label to show both values that were given when the choices wer emade in the combobox. instead of adding 45 and 13 it just shows 45+13.

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by drudge
                      only thing i can do is to get the label to show both values that were given when the choices wer emade in the combobox. instead of adding 45 and 13 it just shows 45+13.
                      Can we see the code? At a guess though, you've got quotes around things that you shouldn't.

                      You should be looking at something simple like Text1 = Text2 + Text3. (Note that .Text is the default property of a textbox, so you can often leave it out.)

                      Depending on how flexible/helpful VB feels like being, you might need to throw in a conversion function such as Val() somewhere along the line.

                      Comment

                      • drudge
                        New Member
                        • Aug 2007
                        • 6

                        #12
                        Originally posted by Killer42
                        Can we see the code? At a guess though, you've got quotes around things that you shouldn't.

                        You should be looking at something simple like Text1 = Text2 + Text3. (Note that .Text is the default property of a textbox, so you can often leave it out.)

                        Depending on how flexible/helpful VB feels like being, you might need to throw in a conversion function such as Val() somewhere along the line.

                        I have this list under the cboBox_click():

                        "Label1 = cloakDX + bodyDX"

                        cloakDX is a label that changes to a number and bodyDX is a label that also changes to a number. and instead of trying to add those two numbers together and showing the sum in label1, it will simply display both numbers side-by-side in label1.

                        Comment

                        • Ali Rizwan
                          Banned
                          Contributor
                          • Aug 2007
                          • 931

                          #13
                          Originally posted by drudge
                          I have a combo box named cboGear. I have my selections for that combo box added when the Form loads. (ie. cboGear.AddItem "Guitar", cboGear.AddItem "Amps".)

                          What I would like to happen is when an item is selected from cboGear, six labels that I have made will populate with info. For example if "Guitar" is selected from cboGear, I would like label1 to read, "1 cord", label2 to read "2 extra strings", label3 to read "3 tuner".

                          I really dont have much, if any, background in vb. I'm just learning on my own in my spare time, so I apologize for ignorance early, heheh.
                          Hello,
                          I think u want to do some thing like as in thee attachment:
                          Attached Files

                          Comment

                          • drudge
                            New Member
                            • Aug 2007
                            • 6

                            #14
                            I did some further poking around in these forums and yes, I did need to add the Val()! Thank you guys for helping me!

                            Comment

                            Working...