Groups | Blog | Home
all groups > visual studio .net general > february 2006 >

visual studio .net general : Help a student? problem with "focus"



wideawake
2/21/2006 6:51:31 AM
I'm hoping someone can help me. 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

pvdg42
2/21/2006 9:36:37 AM

[quoted text, click to view]

You always get a red text color, correct?
I think your textbox already has focus if the TextChanged event is firing.
Another issue is that calling the Focus() method does not test whether or
not your textbox has focus, it sets the focus to the textbox.
You need to look at the Focused property to check for focus, but in your
case, I believe you also need to look at the GotFocus event, which will fire
when your textbox receives focus.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.

AddThis Social Bookmark Button