Help a student? Conditional formatting based on focus
I'm hoping someone can help me. I am working in VB.NET and I have three text boxes on a form, txtone, txttwo, and txtthree. While one of the text boxes has the focus, its text is red, when it loses the focus, the text returns to black. I tried an if statement:
Private Sub txtOne_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles txtOne.TextChanged
If txtOne.Focus Then
txtOne.ForeColor = Color.Red
Else
txtOne.ForeColor = Color.Black
End If
End Sub
But it didn't work. Can anyone assist me? Thx!
[640 byte] By [
wideawake] at [2007-11-11 8:03:49]

# 1 Re: Help a student? Conditional formatting based on focus
Private Sub txtOne_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles txtOne.TextChanged
txtOne.ForeColor = Color.Red
txtTwo.ForeColor = Color.Black
txtThree.ForeColor = Color.Black
End Sub
Private Sub txtTwo_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles txtOne.TextChanged
txtOne.ForeColor = Color.Black
txtTwo.ForeColor = Color.Red
txtThree.ForeColor = Color.Black
End Sub
Private Sub txtThree_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles txtOne.TextChanged
txtOne.ForeColor = Color.Black
txtTwo.ForeColor = Color.Black
txtThree.ForeColor = Color.Red
end sub
Something like this would be better, but not sufficient. If you set this code in the textchange event the colors only change when text is changing.
I only know 6.0 for now but I think you should place this code in the click event of the textboxes.
Benjamin
# 2 Re: Help a student? Conditional formatting based on focus
Use the GotFocus and LostFocus events.
Set the color of the current box in the GotFocus, and set the other boxes to black.
In the LostFocus, set everything to black.
# 4 Re: Help a student? Conditional formatting based on focus
Private Sub TextBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtone.GotFocus, txttwo.GotFocus, txtthree.GotFocus
If TypeOf sender Is Windows.Forms.Control Then DirectCast(sender, Windows.Forms.Control).ForeColor = Drawing.Color.Red
End Sub
Private Sub TextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtone.LostFocus, txttwo.LostFocus, txtthree.LostFocus
If TypeOf sender Is Windows.Forms.Control Then DirectCast(sender, Windows.Forms.Control).ForeColor = Drawing.SystemColors.WindowText
End Sub
AdamP at 2007-11-11 21:51:13 >
