Click here to Skip to main content
15,918,624 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Need help with this:

1. I have a cbDroppedEarly combo box - choices are YES or NO - defaulted to NO
2. I have a tbLinehaulPaid text box

I would like to change the background color of tbLineHaul only if cbDroppedEarly = "YES"
else keep the default color - does not matter what value is in the tbLineHaulPaid - the default is $0.00 as BackColor is solely based on the combobox value.

Can this be done? I tried the code below and it will not work. If not any suggestions would be greatly helpful and appreciated.

What I have tried:

VB
Private Sub tbLinehaulPaid_Change()
    Select Case cbDroppedEarly.ToString
        Case "YES" : tbLinehaulPaid.BackColor = Color.Teal
        Case Else : tbLinehaulPaid.BackColor = Color.WhiteSmoke
    End Select
End Sub
Posted
Updated 8-Apr-16 7:47am

I guess the only thing wrong in your original code was this:

VB.NET
Private Sub tbLinehaulPaid_Change()
    Select Case cbDroppedEarly.ToString 
    Select Case cbDroppedEarly.Selecteditem.ToString
        Case "YES" : tbLinehaulPaid.BackColor = Color.Teal
        Case Else : tbLinehaulPaid.BackColor = Color.WhiteSmoke
    End Select
End Sub


In order to make work the solution 2, make sure that the "YES" value is the same that the index that you have selected.


If you say that still not working... mmm... then, Make sure that you are using properly the event "cbDroppedEarly.selctedindexchanged". Take a look at the control events, maybe the link between the code and the control has broken.
 
Share this answer
 
v2
Comments
K3JAE 8-Apr-16 14:01pm    
Using the original code and changed as noted, still no luck. the tbLineHaulPaid when I choose YES in the combobox.

Question - does the tbLineHaulPaid box have to be a rich text box for this to work?
Ivan_FM 8-Apr-16 14:02pm    
If you say that still not working... mmm... then, Make sure that you are using properly the event "cbDroppedEarly.selctedindexchanged". Take a look at the control events, maybe the link between the code and the control has broken.
K3JAE 8-Apr-16 14:28pm    
I apologize as my coding skills are weak at best. I am not sure what you refer to by "Take a look at the control events, maybe the link between the code and the control has broken."
Ivan_FM 8-Apr-16 14:36pm    
No problem. Take a look here TextBox Events (System.Windows.Forms)[^]


I think the problem is in the event. Try this again.

Private Sub cbDroppedEarly_SelectedIndexChanged(ByVal sender as Object, ByVal e As EventArgs) handles cbDroppedEarly.SelectedIndexChanged
Select Case cbDroppedEarly.Selecteditem.ToString
Case "YES" : tbLinehaulPaid.BackColor = Color.Teal
Case Else : tbLinehaulPaid.BackColor = Color.WhiteSmoke
End Select
End Sub

Note the "handles cbDroppedEarly.SelectedIndexChanged". This links the event to the textbox.
K3JAE 8-Apr-16 14:40pm    
EXCELLENT. That was it. I was going to ask if a "handle" was needed.

Kudos!!
You have to use the SelectedIndexChanged event of your ComboBox, and change the BackColor property of the TextBox accordingly.
VB
Private Sub cbDroppedEarly_SelectedIndexChanged(ByVal sender as Object, ByVal e As EventArgs)
   Dim index As Integer = cbDroppedEarly.SelectedIndex
   Select Case index
      Case 0 : tbLinehaulPaid.BackColor = Color.Teal
      Case Else : tbLinehaulPaid.BackColor = Color.WhiteSmoke
   End Select
End Sub

Hope this helps.
 
Share this answer
 
Comments
K3JAE 8-Apr-16 14:02pm    
I changed the code to what you have above. Unfortunately this made no change to my tbLineHaulPaid background color when I changed the cbDroppedEarly to "yes."

Did I miss something?
phil.o 9-Apr-16 3:52am    
You have to associate the event handler you created to the combobox event.
Creating Event Handlers on the Windows Forms Designer[^]
Applied to your case:
To create an event handler in C# or C++
Click the ComboBox control.
In the Properties window, click the Events button.
In the list of available events, find the SelectedIndexChanged event.
In the box to the right of the event name, select the cbDroppedEarly_SelectedIndexChanged method.
Then test again.
You should also have a look at Event Handling in Windows Forms[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900