只允许 {'delete' , 'backspace' , 一个 '.' , '-' , 数字 '0 到 9'} 在文本框 C#/winforms
Allow only {'delete' , 'backspace' , one '.' , '-' , numbers '0 to 9'} on a textbox C#/winforms
我是一名初级程序员,我正在尝试修改以下代码以仅允许 {'delete' , 'backspace' ,一个 '.' , 一个 '-' 符号在输入的开头,数字 '0 到 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;
}
}
有什么帮助吗?
这个问题与 this one 非常相似,除了你有允许 -
的额外要求
所以试试这个
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;
}
}
在我看来,您想让操作员能够编辑一个可能为负数且精确到小数点后一位的数字。
你的建议是每输入一个字符就检查一次,允许或禁止该字符。这提供了一个相当笨拙的用户界面。
- 运算符类型 9876
- 哦不,这是错误的数字,我需要 -76"
- 操作员使用方向键返回到7之前,输入减号(中级:98-76),使用方向键向左走一位,删除delete删除98(中级:9-76,-76)
复制粘贴其他值“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
旁边添加了一些注释,所以您知道正在检查哪个条件,根据我的测试,这一切似乎都有效。
我是一名初级程序员,我正在尝试修改以下代码以仅允许 {'delete' , 'backspace' ,一个 '.' , 一个 '-' 符号在输入的开头,数字 '0 到 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;
}
}
有什么帮助吗?
这个问题与 this one 非常相似,除了你有允许 -
所以试试这个
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;
}
}
在我看来,您想让操作员能够编辑一个可能为负数且精确到小数点后一位的数字。
你的建议是每输入一个字符就检查一次,允许或禁止该字符。这提供了一个相当笨拙的用户界面。
- 运算符类型 9876
- 哦不,这是错误的数字,我需要 -76"
- 操作员使用方向键返回到7之前,输入减号(中级:98-76),使用方向键向左走一位,删除delete删除98(中级:9-76,-76)
复制粘贴其他值“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
旁边添加了一些注释,所以您知道正在检查哪个条件,根据我的测试,这一切似乎都有效。