0

I subclassed a textbox according to my needs by example from here

On same way I try to subclass NumericUpDown control to get selectAll functionality on them and to replace if point (".") char is pressed to comma (",") char to suit NumericUpDown control more suitable to my locale.

But I can't do that because NumericUpDown haven't a same properties like textbox has and I would like that both those controls behave at same way.

In my code two problems remains, SelectAll and SelectionLength :

Option Explicit On
Option Strict On

Public Class xNumericUpDown
Inherits NumericUpDown

Private alreadyFocused As Boolean

Protected Overrides Sub OnLeave(ByVal e As EventArgs)
    MyBase.OnLeave(e)

    Me.alreadyFocused = False

End Sub

Protected Overrides Sub OnGotFocus(ByVal e As EventArgs)
    MyBase.OnGotFocus(e)

    If MouseButtons = MouseButtons.None Then

        Me.SelectAll() ' HOW TO MAKE SELECTALL SUB FOR NUMERICUPDOWN?
        Me.alreadyFocused = True

    End If
End Sub

Protected Overrides Sub OnMouseUp(ByVal mevent As MouseEventArgs)
    MyBase.OnMouseUp(mevent)

    If Not Me.alreadyFocused AndAlso Me.SelectionLength = 0 Then ' HOW TO CHECK SELECTIONLENGTH FOR NUMERICUPDOWN?

        Me.alreadyFocused = True
        Me.SelectAll() ' HOW TO MAKE SELECTALL SUB FOR NUMERICUPDOWN?

    End If
End Sub

Protected Overrides Sub OnKeyPress(ByVal keyevent As KeyPressEventArgs)
    MyBase.OnKeyPress(keyevent)

    If keyevent.KeyChar = "." Then
        keyevent.KeyChar = CChar(",")
    End If
End Sub

End Class

Please help to get this working.

Community
  • 1
  • 1
Wine Too
  • 4,515
  • 22
  • 83
  • 137

1 Answers1

2

for checking the length and selecting all text try this :

If Not Me.alreadyFocused AndAlso Me.Text.Length = 0 Then 'Though I suspect it should be <> 0

   Me.alreadyFocused = True
   Me.Select(0, Me.Text.Length)

End If

and for the char replacement, try this (untested) :

If keyevent.KeyChar = "."c Then
      keyevent.Handled = True
      SendKeys.Send(",")
End If
Abdusalam Ben Haj
  • 5,343
  • 5
  • 31
  • 45
  • Replace a char works OK, selecting with keyboard OK but selecting with mouse don't work. – Wine Too Feb 03 '13 at 13:47
  • By replacing Me.Text.Length = 0 to Me.Text.Length <> 0 as you mentioned selecting with mouse(up) also works as expected. Thank you very much! – Wine Too Feb 03 '13 at 13:50