2010-04-07 31 views
7

Làm cách nào tôi có thể trả lại khóa ?, có nghĩa là nếu tôi muốn chỉ cho phép giá trị nguyên trong hộp văn bản, làm cách nào để tôi không cho phép người dùng không nhập số nguyên, liên quan đến sự kiện KeyPress. có những cách khác như biểu thức để khớp với giá trị chuỗi, nhưng tôi không muốn gán giá trị không hợp lệ cho hộp văn bản.Hủy sự kiện báo chí chính

if ((value >0 a&&(value <=9)) then 
    assigned 
else 
    return 

Trả lời

14

Sử dụng các tài sản Cầm

e.Handled = true; 

Ví dụ từ MSDN: link

// Boolean flag used to determine when a character other than a number is entered. 
private bool nonNumberEntered = false; 

// Handle the KeyDown event to determine the type of character entered into the control. 
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) 
{ 
    // Initialize the flag to false. 
    nonNumberEntered = false; 

    // Determine whether the keystroke is a number from the top of the keyboard. 
    if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9) 
    { 
     // Determine whether the keystroke is a number from the keypad. 
     if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9) 
     { 
      // Determine whether the keystroke is a backspace. 
      if(e.KeyCode != Keys.Back) 
      { 
       // A non-numerical keystroke was pressed. 
       // Set the flag to true and evaluate in KeyPress event. 
       nonNumberEntered = true; 
      } 
     } 
    } 
    //If shift key was pressed, it's not a number. 
    if (Control.ModifierKeys == Keys.Shift) { 
     nonNumberEntered = true; 
    } 
} 

// This event occurs after the KeyDown event and can be used to prevent 
// characters from entering the control. 
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) 
{ 
    // Check for the flag being set in the KeyDown event. 
    if (nonNumberEntered == true) 
    { 
     // Stop the character from being entered into the control since it is non-numerical. 
     e.Handled = true; 
    } 
} 
0

Bạn có thể sử dụng MaskedTextBox và buộc nó phải số nguyên duy nhất.

+0

Đối với WPF, bạn có thể cần phải tham khảo trang này: http://stackoverflow.com/questions/481059/where-can-i-find-a-free-masked-textbox-in-wpf – Vlad

0

Bạn có thể kế thừa từ TextBox và sau đó:

Protected Overrides Sub OnTextInput(ByVal e As System.Windows.Input.TextCompositionEventArgs) 

     Dim newChar As Char = Convert.ToChar(e.Text) 
     If Not [Char].IsDigit(newChar) Then e.Handled = True 

End Sub 

C# phiên bản

protected override void OnTextInput(System.Windows.Input.TextCompositionEventArgs e) 
{    
    char newChar = Convert.ToChar(e.Text);        
    if (!Char.IsDigit(newChar)) e.Handled = true; 
} 
3

Bạn có thể sử dụng sự kiện bấm phím như dưới đây. Sử dụng e.Handled to true để hủy đầu vào của người dùng

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (!Char.IsDigit(e.KeyChar)) e.Handled = true; 
    } 
5

Tạo một chuỗi có các ký tự bạn cho phép người dùng nhập.

Sử dụng KeyDown hoặc KeyUp để xử lý các phím đặc biệt

private void tbN1_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    String sKeys = "1234567890ABCDEF"; 
    if (!sKeys.Contains(e.KeyChar.ToString().ToUpper())) 
     e.Handled = true; 
} 
0

Về mặt kỹ thuật này là sai vì bạn gắn thẻ câu hỏi của bạn WPF. Nhưng kể từ khi bạn chấp nhận câu trả lời Windows Forms khác, tôi sẽ đăng giải pháp của tôi mà làm việc cho số thực hơn là số nguyên. Nó cũng được bản địa hóa để chỉ chấp nhận dấu phân cách thập phân của ngôn ngữ hiện tại.

private void doubleTextBox_KeyPress (object sender, KeyPressEventArgs e) 
{ 
    var textBox = sender as TextBoxBase; 
    if (textBox == null) 
     return; 

    // did the user press their locale's decimal separator? 
    if (e.KeyChar.ToString() == CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator) 
    { 
     if (textBox.Text.Length == 0) // if empty, prefix the decimal with a 0 
     { 
      textBox.Text = "0" + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator; 
      e.Handled = true; 
      textBox.SelectionStart = textBox.TextLength; 
     } 
     // ignore extra decimal separators 
     else if (textBox.Text.Contains(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator)) 
      e.Handled = true; 
    } 
    // allow backspaces, but no other non-numeric characters; 
    // note that arrow keys, delete, home, end, etc. do not trigger KeyPress 
    else if (e.KeyChar != '\b' && (e.KeyChar < '0' || e.KeyChar > '9')) 
     e.Handled = true; 
} 
Các vấn đề liên quan