2010-04-02 9 views
0
if (textBox1.Text != "") // this forces user to enter something 
{ 
    // next line is supposed to allow only 0-9 to be entered but should block all... 
    // ...characters and should block a backspace and a decimal point from being entered.... 
    // ...but it is also allowing characters to be typed in textBox1 
    if(!IsNumberInRange(KeyCode,48,57) && KeyCode!=8 && KeyCode!=46) // 46 is a "." 
    { 
    e.Handled=true; 
    } 
    else 
    { 
    e.Handled=false; 
    } 

    if (KeyCode == 13) // enter key 
    { 
    TBI1 = System.Convert.ToInt32(var1); // converts to an int 
    Console.WriteLine("TBI1 (var1 INT)= {0}", var1); 
    Console.WriteLine("TBI1= {0}", TBI1); 
    } 

    if (KeyCode == 46) 
    { 
    MessageBox.Show("Only digits...no dots please!"); 
    e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar); 
    } 
} 
else 
{ 
    Console.WriteLine("Cannot be empty!"); 
} 

// If I remove the outer if statement and skip checking for an empty string, then 
// it prevents letters from being entered in the textbox. I need to do both, prevent an 
// empty textbox AND prevent letters from being entered. 
// thanks, Sonny5 

Répondre

1

Je pense que vous pourriez utiliser la fonction IsDigit.

Quelque chose le long de ces lignes:

string textBoxText = "12kj3"; 

if (!textBoxText.Equals(String.Empty)) // this forces user to enter something 
{ 
    foreach (char c in textBoxText.ToArray()) 
    { 
     if (!Char.IsDigit(c)) 
     { 
      //return false; 
     } 
    } 

    //return true; 
} 
else 
{ 
    Console.WriteLine("Cannot be empty!"); 
} 

Espoir vous obtenez l'idée.

2

Vous n'avez pas spécifié où ce code s'exécute, mais mon hypothèse serait qu'il s'exécute sur la touche enfoncée. Comme la clé est reçue avant le traitement du caractère et que la propriété Text est mise à jour, votre recherche de .Text == "" empêche le reste de la validation en cours, au moins pour le premier caractère.

Vous devez déplacer le contrôle de la valeur vide sur un événement différent du contrôle de la touche enfoncée.

+0

Vous obtiendriez un +1 s'il me restait un vote. Le commentaire dans le code 'ceci force l'utilisateur à entrer quelque chose' est faux. Ce n'est pas du tout ce que fait cette ligne. –

0

Vous pouvez utiliser le code d'entrée suivant pour vérifier qu'il s'agit d'un nombre "^ \ d + $" et requis.

0
bool bV=false; 
    private void textBox1_Validated(object sender, EventArgs e) 
    { 
     TextBox textBoxText = sender as TextBox; 

     if (!textBoxText.Equals(String.Empty)) 
     { 
      foreach (char c in textBoxText.Text.ToArray()) 
      { 
       if (!Char.IsDigit(c)) 
       { 
        if (!bV) 
        { 
         MessageBox.Show("Input value not valid plase Insert Integer Value"); 
         bV = true; 
         textBox1.Text = String.Empty; 
         break; 
        } 
       } 
      } 
     } 
    } 

    private void textBox1_KeyDown(object sender, KeyEventArgs e) 
    { 
     bV = false; 
    }