dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Help a student? Conditional formatting based on focus

  1. #1
    Join Date
    Feb 2006
    Location
    Green Bay, Wisconsin
    Posts
    2

    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!

  2. #2
    Join Date
    Feb 2006
    Posts
    133
    Code:
    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

  3. #3
    Join Date
    Dec 2005
    Location
    South of Nowhere, Maine
    Posts
    309
    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. #4
    Join Date
    Feb 2006
    Location
    Green Bay, Wisconsin
    Posts
    2
    thanks guys! it helped

  5. #5
    Join Date
    Feb 2006
    Posts
    25

    Might try this as well.

    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

Similar Threads

  1. StudentList Class
    By WillisTi in forum Java
    Replies: 2
    Last Post: 02-25-2005, 07:35 AM
  2. JAVA help
    By Munchkin in forum Java
    Replies: 3
    Last Post: 03-16-2001, 05:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center