Change colour of text, based on combo box selection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nasher
    New Member
    • Feb 2008
    • 7

    Change colour of text, based on combo box selection

    i have a combo box (combo box 1) in a form header that users use to selct item numbers from.
    I also have a combo box (combo box 2) that users use to select a priority number (1,2,3) for that item. This is then stored on the items record.

    I was wondering if the item has a priority 1 in combo box 2, then it could display the text in combo box 1 as red (or posibly the background, or something to make it stand out).

    Also i would like priority 2 items to display as amber and priority 3 items to display as green.
  • sierra7
    Recognized Expert Contributor
    • Sep 2007
    • 446

    #2
    Hi
    The short answer is 'Yes'

    If you want the colour to change immediately after combo2 has been set then you will need to put code in the After_Update event of combo2.

    If you want the colours to persist and change according to the priority when you are browsing through the records you will have to put the same code into the On_Current event of the Form.

    You could change the colour of the text in the combo or it's back colour, or if you wanted to go overboard you could change the colour of the Header section etc

    [CODE=vb]Private Sub combo2_AfterUpd ate()
    If Me.combo2 = 2 Then Me.combo2.BackC olor = RGB(250, 0, 0)
    If Me.combo2 = 2 Then Me.combo2.ForeC olor = RGB(255, 255, 255)
    If Me.combo2 = 2 Then Me.Form.Section (acHeader).Back Color = vbRed
    If Me.combo2 = 2 Then Me.Form.Section (acDetail).Back Color = vbBlack
    If Me.combo2 = 2 Then Me.Form.Section (acFooter).Back Color = vbBlue
    End Sub[/CODE]

    You can use the set 'vb' colours or mix your own using the RGB() function.

    Get the idea? (don't forget to drop the u in colour)

    Sorry, I have just read your last line! You will have to use If ...ElseIf construction to do what you want. I'll let you do that bit and you can have fun mixing your amber and green colours !


    S7
    Last edited by sierra7; Feb 22 '08, 11:28 AM. Reason: Had not read last line

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #3
      I would rather suggest using a Select Case statement for this (personal preference and an alternative idea).
      Code:
      Private Sub Combo_Box_2_AfterUpdate()
        Select Case [Combo Box 2]
        Case 1
          Me.[Combo Box 1].BackColor = vbRed
        Case 2
          Me.[Combo Box 1].BackColor = vbYellow
          'Me.[Combo Box 1].BackColor = RGB(255, 128, 0)
        Case 3
          Me.[Combo Box 1].BackColor = vbGreen
        End Select
      End Sub
      If Yellow isn't a good enough match, then the (commented) RGB() line may be used instead. You can even play with the values to match the colour you want.
      Good luck :)

      PS. Hi Sierra. I didn't know you were a Brit too (spelling of colour) ;)
      PPS. As is Nasher too by the looks of things :)

      Comment

      • sierra7
        Recognized Expert Contributor
        • Sep 2007
        • 446

        #4
        Originally posted by NeoPa
        PS. Hi Sierra. I didn't know you were a Brit too (spelling of colour) ;)
        True!
        I noticed that Nasher had spelt it 'correctly' too which was why I mentioned it because (not wanting to be condescending here Nasher) it was a Newbie sort of question, so a typo could compound the problem.

        I thought I would help further by showing the syntax for reference to Header etc because I remember how long it took me the first time.

        Incidentally, I feel that vbGreen is a misnomer. Grass is green, trees are green but the only time I have seen vbGreen has been inside a baby's nappy, or should that be diaper?
        S7

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          Originally posted by sierra7
          ...
          Incidentally, I feel that vbGreen is a misnomer. Grass is green, trees are green but the only time I have seen vbGreen has been inside a baby's nappy, or should that be diaper?
          S7
          Yes, I liked that you gave different examples of the code :)
          As to vbGreen being a bit off, I think that as Green is a hue (Primary Colour) the value (65,280 or 0xFF00) is simply saying 100% of Green and 0% of both Red & Blue. How a graphics card (or more likely monitor) manages to express this is independant of the software I would think.

          PS. Thanks for the reminder :( I've not had to deal with that sort of thing for many years :)
          PPS. Congratulations on the 200 posts when you reply to this :D

          Comment

          • sierra7
            Recognized Expert Contributor
            • Sep 2007
            • 446

            #6
            Originally posted by NeoPa
            PPS. Congratulations on the 200 posts when you reply to this :D
            Thank you. It's amazing how quickly they add up.

            We must be sad doing this on a Sunday afternoon though!

            Incidentally I have used you Case Statement on another thread for changing colours of a Tab Control

            It get's repeatative after a bit doesn't it!

            S7

            Comment

            • Nasher
              New Member
              • Feb 2008
              • 7

              #7
              Cheers guys for the help.

              And you guessed right, i am a english newbie, so that "color" spelling could have really messed things up for me. Well done for being so alert.

              Comment

              • sierra7
                Recognized Expert Contributor
                • Sep 2007
                • 446

                #8
                Your welcome Nasher

                Just looking over my first posting I hope you realise that it was intended to be a guide to the syntax for a SINGLE STATEMENT.

                Looking at it now I think "Why did I put all those If's ?" Obviously, you would not write an 'If Statement' like this in a program if you wanted to change MULTIPLE attributes! (I'm sure you know how to write an If statement but have a look at the Help if you have been brought up on Java or C# or something)

                NeoPa's suggestion to use 'Select Case' is far more elegant and I wish I could get into the habit of using it but my formative years were spent learning Fortran V and old habits die hard.

                S7

                Comment

                Working...