对NumericUpDown控件进行子类化

时间:2013-02-03 13:24:20

标签: vb.net

我通过here

中的示例根据我的需要对文本框进行了子类化

同样地,我尝试将NumericUpDown控件子类化以获取它们的selectAll功能,并且如果将点(“。”)char按到逗号(“,”)char以适合更适合我的语言环境的NumericUpDown控件,则替换它。 / p>

但我不能这样做,因为NumericUpDown没有像textbox那样的属性,我希望这两个控件的行为方式相同。

在我的代码中,仍有两个问题,SelectAll和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

请帮助实现这一目标。

1 个答案:

答案 0 :(得分:1)

检查长度并选择所有文本试试这个:

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

并且对于替换char,请尝试此(未经测试):

If keyevent.KeyChar = "."c Then
      keyevent.Handled = True
      SendKeys.Send(",")
End If