簡體   English   中英

如何讓 TextBox 只接受字母字符?

[英]How to make a TextBox accept only alphabetic characters?

如何讓TextBox只接受帶空格的字母字符?

您可以嘗試通過處理文本框的KeyPress事件

void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back);
}

另外說如果您想刪除一些文本,請允許退格,這對您來說應該很好用

編輯

上面的代碼不適用於粘貼在我認為您必須使用TextChanged事件的字段中,但是如果您必須刪除不正確的字符或突出顯示它並為用戶放置光標,則會變得更加復雜進行更正 或者您可以在用戶輸入完整文本並關閉控件后進行驗證。

您可以使用以下代碼段:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (!System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "^[a-zA-Z ]"))
    {
        MessageBox.Show("This textbox accepts only alphabetical characters");
        textBox1.Text.Remove(textBox1.Text.Length - 1);
    }
}
private void textbox1_KeyDown_1(object sender, KeyEventArgs e)
{
    if (e.Key >= Key.A && e.Key <= Key.Z)
    {
    }
    else
    {
       e.Handled = true;
    }
}

最簡單的方法是處理 TextChangedEvent 並檢查輸入的內容:

string oldText = string.Empty;
    private void textBox2_TextChanged(object sender, EventArgs e)
    {
        if (textBox2.Text.All(chr => char.IsLetter(chr)))
        {
            oldText = textBox2.Text;
            textBox2.Text = oldText;

            textBox2.BackColor = System.Drawing.Color.White;
            textBox2.ForeColor = System.Drawing.Color.Black;
        }
        else
        {
            textBox2.Text = oldText;
            textBox2.BackColor = System.Drawing.Color.Red;
            textBox2.ForeColor = System.Drawing.Color.White;
        }
        textBox2.SelectionStart = textBox2.Text.Length;
    }

如果您願意,這是一個無正則表達式的版本。 它會使文本框在輸入錯誤時閃爍。 請注意,它似乎也支持粘貼操作。

在 Text_KeyPress 事件中編寫代碼為

 private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {

        if (!char.IsLetter(e.KeyChar))
        {
            e.Handled = true;
        }
    }

這個工作得很好......

 private void manufacturerOrSupplierTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (char.IsControl(e.KeyChar) || char.IsLetter(e.KeyChar))
        {
            return;
        }
        e.Handled = true;
    }

該解決方案使用正則表達式,不允許將無效字符粘貼到文本框中並保持光標位置。

using System.Text.RegularExpressions;

int CursorWas;
string WhatItWas;

private void textBox1_Enter(object sender, EventArgs e)
    {
        WhatItWas = textBox1.Text;
    }

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (Regex.IsMatch(textBox1.Text, "^[a-zA-Z ]*$"))
        {
            WhatItWas = textBox1.Text;
        }
        else
        {
            CursorWas = textBox1.SelectionStart == 0 ? 0 : textBox1.SelectionStart - 1;
            textBox1.Text = WhatItWas;
            textBox1.SelectionStart = CursorWas;
        }
    }

注意:textBox1_TextChanged 遞歸調用。

        if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "^[a-zA-Z]+$"))
        { 
        }
        else
        {
            textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
            MessageBox.Show("Enter only Alphabets");


        }

請試試這個

private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= '0' && e.KeyChar <= '9')
                e.Handled = true;
            else
                e.Handled = false;
        }

嘗試這個

            private void tbCustomerName_KeyPress(object sender, KeyPressEventArgs e)
    {
        e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back||e.KeyChar==(char)Keys.Space);
    }

它也允許空白

您可以嘗試以下在按鍵事件時發出警報的代碼

private void tbOwnerName_KeyPress(object sender, KeyPressEventArgs e)
    {

        //===================to accept only charactrs & space/backspace=============================================

        if (e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space))
        {
            e.Handled = true;
            base.OnKeyPress(e);
            MessageBox.Show("enter characters only");
        }

這是我的解決方案,它按計划工作:

string errmsg = "ERROR : Wrong input";
ErrorLbl.Text = errmsg;

if (e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space))
{
  ErrorLbl.Text = "ERROR : Wrong input";
}
else ErrorLbl.Text = string.Empty;
if (ErrorLbl.Text == errmsg)
{
  Nametxt.Text = string.Empty;
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
      if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar) &&
         (e.KeyChar !='.'))
        { 
          e.Handled = true;
          MessageBox.Show("Only Alphabets");
        }
    }

在文本框的 KeyPress 事件中嘗試以下代碼

if (char.IsLetter(e.KeyChar) == false  & 
        Convert.ToString(e.KeyChar) != Microsoft.VisualBasic.Constants.vbBack)
            e.Handled = true

就字符限制而言,這可以正常工作,如果不是 C 或 L,請使用我的代碼對錯誤 msg 提示提出任何建議

Private Sub TXTBOX_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTBOX.TextChanged

        Dim allowed As String = "C,L"
        For Each C As Char In TXTBOX.Text
            If allowed.Contains(C) = False Then

                TXTBOX.Text = TXTBOX.Text.Remove(TXTBOX.SelectionStart - 1, 1)
                TXTBOX.Select(TXTBOX.Text.Count, 0)

            End If

        Next
      
    End Sub

對我有用,即使不是最簡單的。

private void Alpha_Click(object sender, EventArgs e)
        {
            int count = 0;
            foreach (char letter in inputTXT.Text)
            {
                if (Char.IsLetter(letter))
                {
                    count++;
                }
                else
                {
                    count = 0;
                }
            }
            if (count != inputTXT.Text.Length)
            {
                errorBox.Text = "The input text must contain only alphabetic characters";
            }
            else
            {
                errorBox.Text = "";
            }
        }

試試這個。 空格和快捷鍵起作用

 if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar) && !char.IsSeparator(e.KeyChar))
        {
            e.Handled = true;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM