|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionIf you have ever written a data-entry application, there's a big chance you used the The other side of the coin is that
That's why I decided to subclass it, fixing these points and adding missing features and properties. Missing TextBox propertiesI needed some missing NumericUpDown exposes a Select(int Start, int Lenght) method you can call to select all text. At first try, I attached to the GotFocus event to call Select(0, x) but, hey, wait a moment... what should I use for x? It seems that any value is accepted, even if greater than the text length. OK, let's say x=100 and proceed. This works well with the keyboard focus keys (like TAB), but it's completely useless with the mouse: a mouse click raises the GotFocus event (where I select all the text), but as soon as you release the button, a zero-selection is done, leaving the control with no selection. OK, I thought, let's add a SelectAll on the MouseUp event too, but this way, the user cannot perform a partial selection anymore; each time the mouse button is released, all the text is selected. I need to know if a partial selection exists; in a TextBox, I can test it with SelectionLength > 0, so I need to access the underlying TextBox control.
Now comes the tricky part: Friend upDownEdit As UpDownEdit ' UpDownEdit inherits from TextBox
We'll obtain a reference to this field using Reflection; this is done in the control creator. ''' <summary>
''' object creator
''' </summary>
Public Sub New()
MyBase.New()
' extract a reference to the underlying TextBox field
_textbox = GetPrivateField(Me)
If _textbox Is Nothing Then
Throw New ArgumentNullException(Me.GetType.FullName _
& ": Can't find internal TextBox field.")
End If
' ...
End Sub
''' <summary>
''' Extracts a reference to the private underlying textbox field
''' </summary>
Private Shared Function GetPrivateField _
(ByVal ctrl As NumericUpDownEx) As TextBox
' find internal TextBox
Dim textFieldInfo As Reflection.FieldInfo _
= GetType(NumericUpDown).GetField("upDownEdit", _
Reflection.BindingFlags.FlattenHierarchy _
Or Reflection.BindingFlags.NonPublic _
Or Reflection.BindingFlags.Instance)
' take some caution... they could change field name
' in the future!
If textFieldInfo Is Nothing Then
Return Nothing
Else
Return TryCast(textFieldInfo.GetValue(ctrl), TextBox)
End If
End Function
Now that we have the underlying <Browsable(False)> _
<DesignerSerializationVisibility( _
DesignerSerializationVisibility.Hidden)> _
Public Property SelectionStart() As Integer
Get
Return _textbox.SelectionStart
End Get
Set(ByVal value As Integer)
_textbox.SelectionStart = value
End Set
End Property
And finally, we can have a perfectly working mouse management: ' MouseUp will kill the SelectAll made on GotFocus.
' Will restore it, but only if user have not made
' a partial text selection.
Protected Overrides Sub OnMouseUp(ByVal mevent As MouseEventArgs)
If _autoSelect AndAlso _textbox.SelectionLength = 0 Then
_textbox.SelectAll()
End If
MyBase.OnMouseUp(mevent)
End Sub
Mouse events not raised properlyThe original As said above,
The "control" area is the one between the red and the green rectangles; when you fly over it with the mouse, you'll receive the The better way to raise these events, now that we can access the underlying MouseWheel management
After your user puts the focus inside one of them, the mouse wheel is captured by the A fix could be to kill the The This is done by keeping track of the mouse state in the
How to use the controlSimply include NumericUpDownEx.vb in your project and use the control like you'll do with the standard
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||