0

我从这里通过示例根据我的需要对文本框进行了子类化

同样,我尝试对 NumericUpDown 控件进行子类化以获取它们的 selectAll 功能并替换 if point (".") char is press to comma (",") char 以适应更适合我的语言环境的 NumericUpDown 控件。

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

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

请帮助使其正常工作。

4

1 回答 1

2

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

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

对于字符替换,试试这个(未经测试):

If keyevent.KeyChar = "."c Then
      keyevent.Handled = True
      SendKeys.Send(",")
End If
于 2013-02-03T13:37:26.040 回答