[英]Allow only {'delete' , 'backspace' , one '.' , '-' , numbers '0 to 9'} on a textbox C#/winforms
我是一名初学者程序员,我正在尝试修改以下代码以仅允许 {'delete', 'backspace', one '.', one '-' sign in the beginning of input, numbers '0 to 9' } 在文本框 C#/winforms 上:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
(e.KeyChar != '.'))
{
e.Handled = true;
}
// only allow one decimal point
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
e.Handled = true;
}
}
有什么帮助吗?
这个问题与这个问题非常相似,除了你有额外的要求允许-
所以试试这个
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)) //bypass control keys
{
int dashIndex = textBox1.Text.IndexOf('-');
int dotIndex = textBox1.Text.IndexOf('.');
int selectionStart = textBox1.SelectionStart;
int selectionEnd = selectionStart + textBox1.SelectionLength;
if (char.IsDigit(e.KeyChar))
{
if (dashIndex == 0 && selectionStart == 0 && selectionEnd == 0)
e.Handled = true;
}
else if (e.KeyChar == '-')
{
if (dashIndex == -1)
{
if (selectionStart != 0)
e.Handled = true;
}
else
e.Handled = !(selectionStart <= dashIndex && selectionEnd > dashIndex);
}
else if (e.KeyChar == '.')
{
if (dotIndex == -1)
e.Handled = false;
else
e.Handled = !(selectionStart <= dotIndex && selectionEnd > dotIndex);
}
else
e.Handled = true;
}
}
在我看来,您想让操作员能够编辑一个可能为负数的小数点。
您的建议是在键入每个输入字符后立即检查它,并允许或禁止该字符。 这提供了一个相当笨拙的用户界面。
复制粘贴其他值“Price €76.12”并删除不需要的字符 (76.12) 是不可能的。
正确的界面应该是允许操作员执行所有操作,直到他向您发出信号表示他已完成号码编辑。 通常这是一个 OK 按钮,或者如果你想要 enter 按钮。 订阅适当的事件,然后才处理输入:
public void OnButtonOk_Clicked(object sender, ...)
{
string inputText = this.textBox1.Text;
this.ProcessInput(inputText);
}
// or if you want: react on enter:
public void OnKeyPress(object sender, ...)
{
// check if enter
bool enterPressed = ...
if (enterPressed)
{
this.ProcessInput(this.textBox1.Text);
}
}
public void ProcessInput(string inputText)
{
// check if the text can be converted to a double:
if (double.TryParse(inputText, double result))
{
// text ok: do what you need to do
}
else
{
// text not ok: warn the operator
}
}
下面的方法检查是否允许输入的字符:
private bool NotPermittedChar(char character)
{
return !char.IsDigit(character) &&
!character.Equals('-') &&
!character.Equals('.') &&
!char.IsControl(character);
}
然后下面的 KeyPress 方法代码将执行您需要的相关检查和条件:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (this.NotPermittedChar(e.KeyChar))
{
e.Handled = true;
return;
}
if (this.textBox1.Text.Contains(".") && e.KeyChar.Equals('.'))
{
e.Handled = true; //// Don't allow more than 1 decimal place
return;
}
if (this.textBox1.SelectionStart == 0 && e.KeyChar.Equals('.'))
{
e.Handled = true; //// Don't allow . to be at the beginning
return;
}
if (this.textBox1.SelectionStart != 0 && e.KeyChar.Equals('-'))
{
e.Handled = true; //// Don't allow - in other index than the beginning
return;
}
}
我在e.Handled
的旁边添加了一些注释,以便您知道正在检查哪个条件,根据我的测试,这一切似乎都有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.