[英]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.