usercontrol textbox and receiving focus

rustyd

Centurion
Joined
Mar 5, 2003
Messages
112
I have a usercontrol

Code:
Public Class Ritext
  Inherits System.Windows.Forms.TextBox

When this control receives focus, it should change to the user defined color held in a public variable, which it does. In my forms, I have a routine that binds all controls Enter event to a single sub:

Code:
        AddHandler c1.Enter, AddressOf Me.OnSetFocus

In OnSetFocus, if the key field is entered and the user presses enter or tabs to the next control, if it is an inherited textbox, the text is highlighted even if the clear property is set to false. This only happens on the first textbox after the key textbox.

After debugging, I find that the base class On_Enter event of ritext is fired, then the forms OnSetFocus is fired populating the form's fields and highlighting the inherited textboxes text.

Code:
  Public Sub On_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Enter
    Try
      sender.BackColor = Color.FromArgb(255, InRGB)
      msSavedText = Me.Text
      If IsNothing(DirectCast(sender, Ritext).RICSFormLabel) Then
        DirectCast(sender, Ritext).RICSFormLabel.Text = ""
      End If
    Catch ex As Exception
    End Try
    Try
      If _RICSClear Then
        DirectCast(sender, Ritext).SelectAll()
      Else
        If DirectCast(sender, Ritext).Text.Length > 0 And DirectCast(sender, Ritext).SelectionLength = 0 And (DirectCast(sender, Ritext).SelectionStart = 0 Or DirectCast(sender, Ritext).SelectionStart = DirectCast(sender, Ritext).Text.Length) Then
          DirectCast(sender, Ritext).SelectionStart = DirectCast(sender, Ritext).Text.Length
        End If
      End If
    Catch ex As Exception
    Finally
      _dtEntered = Now
    End Try
  End Sub

If I add the call sender.On_Enter(Sender, e) in the form's OnSetFocus routine, it "works" like it is supposed to, but every time a textbox receives focus it is calling the base class twice.

Any suggestions are appreciated.
 
I should clarify this a bit. I have a control that inherits from WIndows.Forms.Textbox. In this control, I have the On_Enter event. When the control receives focus it checks the data type for the field the control is holding. If the control is for a numeric field, it highlights the text, if it isn't, it places the cursor at the end of the string.

So, I have 2 two of these controls on a form. The first is a numeric keyfield that to identify the record, the second is a string description. What I'm seeing is the On_Enter of the base class is firing before the form level OnEnter.

1. From first textbox, enter an existing record id and press enter.
2. Second textbox base class On_Enter event fires, since the record hasn't
been retrieved yet there is no description text.
3. The form level on_enter event fires and the record is retrieved.
4. The description text is now highlighted

If I make a call to sender.On_Enter(Sender, e) in step 3 after the record is retrieved, it works like expected, but this means on forms with more than the 2 controls, this would fire the On_Enter twice for each textbox that receives focus. How can I have the form level on_enter fire before the base class? Is that possible?
 
I'm not entirely sure I'm completely understanding you but here goes... As far as I'm aware you can't make the form level on_enter event trigger before the base on_enter event of the control. If your trying todo what I think you are I would achieve it like this, add a custom method to the control and call it from the on_enter event of the form. Any code that you wished to be performed by the base on_enter event is then put into this public method.

Thus
1. From first textbox, enter an existing record id and press enter.
2. Second textbox base class On_Enter event fires (but there is no custom code in it)
3. The form level on_enter event fires and the record is retrieved.
4. The public method is called from the form level on_enter achieving whatever your aim was.
 
Thanks for the reply. That will work. Hindsight being 20/20, we should have used OnLeave for validation.
 
Back
Top