none
How to set UserForm TextBox Background color based on TabIndex

    Question

  • Hi

    I want to set background color as TextBox_enter event occurs and set it back to its original background color as i tab to the next TabIndex

    any suggestion is appreciated

    Thanks

    Thursday, January 27, 2011 1:05 PM

Answers

  • Indeed, Enter/Exit events are not exposed in a TextBox WithEvents class, you should have indicated that's what you were using.

    A partial solution is almost the same, in the KeyDown & KeyUp events (which are exposed in the class) -

    If KeyCode = vbKeyTab Then
          tbx.BackColor =  myColour
    End If

    Where myColour is the in-focus colour in the KeyUp and the default in the KeyDown. Trouble is user could of course exit the textbox with the mouse or an Alt keystroke. It means more work. If tab is keycode in the KeyUp you could assign a variable back in the form that refers to the textbox (its name or index). You'll then check the mouse and key up events of all controls to see if such a variable has been flagged, and if so reset the corresponding textbox's colour an clear the variable. Even more to do if you need to cater for other ways a user might enter the textbox. Tedious but doable.

    Peter Thornton

    • Marked as answer by BR99 Thursday, January 27, 2011 6:05 PM
    Thursday, January 27, 2011 3:58 PM

All replies

  • Private Sub TextBox1_Enter()
    TextBox1.BackColor = RGB(200, 220, 255)
    End Sub

    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    TextBox1.BackColor = vbWhite
    End Sub

    Peter Thornton

    Thursday, January 27, 2011 1:29 PM
  • Thank you Peter;

    Since i have about 25 textbox i'm looking for a TabIndex based solution.

    I created a class using WithEvents just to realize the enter/exit events are not available as they are within the UserForm environment. (change event does work)

     

    Option Explicit
    Private WithEvents mTextBox As TextBox
    
    Public Property Set Text_Box(TxtBox As TextBox)
      Set mTextBox = TxtBox
    End Property
    
    Private Sub mTextBox_Change()
      'MsgBox "Changed " & fmBCMcontact.ActiveControl.TabIndex
    End Sub
    
    Private Sub mTextBox_enter() '<==== unavailable !!!!
    MsgBox "Entered " & fmBCMcontact.ActiveControl.TabIndex
    End Sub

     

    Is the only solution is to have 25 enter and 25 exit event routines within my form code ?

     

    Thanks

     

    • Edited by BR99 Thursday, January 27, 2011 2:11 PM added code
    Thursday, January 27, 2011 1:51 PM
  • Indeed, Enter/Exit events are not exposed in a TextBox WithEvents class, you should have indicated that's what you were using.

    A partial solution is almost the same, in the KeyDown & KeyUp events (which are exposed in the class) -

    If KeyCode = vbKeyTab Then
          tbx.BackColor =  myColour
    End If

    Where myColour is the in-focus colour in the KeyUp and the default in the KeyDown. Trouble is user could of course exit the textbox with the mouse or an Alt keystroke. It means more work. If tab is keycode in the KeyUp you could assign a variable back in the form that refers to the textbox (its name or index). You'll then check the mouse and key up events of all controls to see if such a variable has been flagged, and if so reset the corresponding textbox's colour an clear the variable. Even more to do if you need to cater for other ways a user might enter the textbox. Tedious but doable.

    Peter Thornton

    • Marked as answer by BR99 Thursday, January 27, 2011 6:05 PM
    Thursday, January 27, 2011 3:58 PM
  • Thanks Peter
    Thursday, January 27, 2011 6:05 PM