Customized IntegerTextBox problem

  • Thread starter Thread starter Christian Soltenborn
  • Start date Start date
C

Christian Soltenborn

Hi,

I would like to have some customized TextBoxes, e.g. like this:

Public Class IntegerTextBox
Inherits System.Windows.Forms.TextBox

Protected Overrides Sub OnTextChanged(ByVal e As EventArgs)
Try
Integer.Parse(Me.Text)
Catch ex As Exception
MessageBox.Show("Please enter a valid number!")
End Try
End Sub

End Class

For this, I have two questions:
a) If I override onTextChanged(), the method is called for every char
entered. This would actually work for integers, but not for more complex
data like a TimeSpan. Therefore I would like to override a method which
is called if the complete data is entered (e.g. if the text box looses
focus). Is there a way to do this?
b) I also could not figure out how I can give the focus back to my
object after displaying the MessageBox. I didn't see anything like
Me.SetFocus(), I can just ask if I have focus.

Thanks,
Christian
 
For this, I have two questions:
a) If I override onTextChanged(), the method is called for every char
entered. This would actually work for integers, but not for more complex
data like a TimeSpan. Therefore I would like to override a method which
is called if the complete data is entered (e.g. if the text box looses
focus). Is there a way to do this?

Overriding OnValidating or OnValidated sounds like what you want.
OnValidating is most likely better, since it gives you the option of
stopping the control from losing focus if the input is invalid.

Also, be sure to call the base class method somewhere in there. In your
code, I notice there is no call to MyBase.OnTextChanged(e) in there
anywhere.
b) I also could not figure out how I can give the focus back to my
object after displaying the MessageBox. I didn't see anything like
Me.SetFocus(), I can just ask if I have focus.

Me.Focus = True

My gut reaction is also that setting the focus should be a method and
not a property, but what can you do?

Jeremy
 
Jeremy Todd said:
Me.Focus = True

My gut reaction is also that setting the focus should be a method and
not a property, but what can you do?

Er, I had a brain shutdown or something. "Focus" -is- a method, not a
property. "Focused" is the property, and it's read-only. Sorry for giving
bad advice!

Jeremy
 
Back
Top