Text Color...

MFS

Member
Joined
Feb 6, 2007
Messages
10
Programming Experience
Beginner
Hello,

I wasn't sure where to put this thread but I thought this forum might be the closest match.

My question is this:

I have a program that gets its info from a database. I would like to have the textbox forecolor in a certain textbox (textbox1) change to red if the text in another textbox (textbox2) is "no". I have tried and I can get it to change but as soon as I navigate to another record the textbox forecolor of textbox1 is red even if there is nothing in textbox2.

Is there a way to change the text color uniquely for each record and then when you navigate to another record it returns to default?

Sorry if this sounds confusing. I am a beginner programmer and many seemingly simple concepts are difficult for me at this stage.

Thanks in advance
MFS
 
After navigating to another record, use an If Then Else statement:
VB.NET:
If TextBox2.Text.ToLower = "no" Then
    TextBox1.ForeColor = Color.Red
Else
    TextBox1.ForeColor = SystemColors.WindowText
End If
I used SystemColors.WindowText because that is the 'default' value of TextBox.ForeColor. If you want to account for times when the 'default' is not the class default color (if in the designer, you have changed the ForeColor value to something other than WindowText) you could store that value in a class variable after initializing the components.

If textBox2 is meant to answer a yes/no question, you would be better off using a CheckBox control. To accomodate the CheckBox in the above code, check the value of CheckBox.Checked instead of TextBox2.Text.ToLower.
 
Paszt,

That is practically the same code I had used before so I know I'm on the right track. What do you mean when you say "after navigating to another record"? Where would I put this code?

I have not yet learned to write database code so I am using the auto-generated code you get when using the data source wizard.

The textbox is meant to answer a yes/no question, however I do not know how to save checkbox data into a database (access) so I was using a textbox so I could just save the actual "text" of it.

I have a lot to learn, but I'm trying. :)

Thanks again for the help.
MFS
 
Read my post #14 in this thread to learn my suggested way to set up databinding in .NET 2: Filter listbox items by textbox. Use the 'details' setup as mentioned in the second paragraph to have individual controls instead of a datagridView, the checkbox will be created and bound automatically.
Once set up this way you can handle the BindingSource.PositionChanged event to set the color of your textbox.
 
Back
Top