我正在构建一个自定义文本框,允许用户在键盘中键入一些字符。我覆盖System::Forms::MaskedTextBox
如下:
public ref class MyMaskedTextBox : System::Windows::Forms::MaskedTextBox
{
public:
MyMaskedTextBox();
~MyMaskedTextBox();
void OnKeyPress(System::Windows::Forms::KeyPressEventArgs^ e) override;
};
void MyMaskedTextBox::OnKeyPress(System::Windows::Forms::KeyPressEventArgs^ e)
{
if ((e->KeyChar >= 'A' && e->KeyChar <= 'Z') ||
(e->KeyChar >= '0' && e->KeyChar <= '9') ||
e->KeyChar == '*' ||
e->KeyChar == '-' ||
e->KeyChar == '/' ||
e->KeyChar == '.' ||
e->KeyChar == ' ' ||
e->KeyChar == (char) System::Windows::Forms::Keys::Back ||
e->KeyChar == (char) System::Windows::Forms::Keys::Delete)
{
e->Handled = false;
}
else
{
e->Handled = true;
}
}
我正在调用我的函数,我的MyMaskedTextBox
行为很奇怪,比如不限制其MaxLength
属性中定义的字符数,而不是在键入delete时删除文本等等。
我的继承是错的还是我应该在处理逻辑后调用基类?如果是这样,我该如何调用基类?