2011-05-27 29 views
17

Hice esta declaración para comprobar si TextBox está vacío, pero MessageBox siempre aparece si el TextBox está vacío o no.¿Verifica si TextBox está vacío y devuelve MessageBox?

private void NextButton_Click(object sender, EventArgs e) 
    { 
     decimal MarkPoints, x, y; 
     x = HoursNumericUpDown.Value; 
     y = MarkNumericUpDown.Value; 
     MarkPoints = x * y; 

     //decimal MarkPoints = (decimal)HoursNumericUpDown.Value * (decimal)HoursNumericUpDown.Value; 


     DataGridViewRow dgvRow = new DataGridViewRow(); 
     DataGridViewTextBoxCell dgvCell = new DataGridViewTextBoxCell(); 

     dgvCell = new DataGridViewTextBoxCell(); 
     dgvCell.Value = MaterialTextBox.Text; 
     dgvRow.Cells.Add(dgvCell); 

     dgvCell = new DataGridViewTextBoxCell(); 
     dgvCell.Value = HoursNumericUpDown.Value; 
     dgvRow.Cells.Add(dgvCell); 

     dgvCell = new DataGridViewTextBoxCell(); 
     dgvCell.Value = MarkNumericUpDown.Value; 
     dgvRow.Cells.Add(dgvCell); 

     dgvCell = new DataGridViewTextBoxCell(); 
     dgvCell.Value = MarkPoints; 
     dgvRow.Cells.Add(dgvCell); 

     dataGridView1.Rows.Add(dgvRow); 

     MaterialTextBox.Clear(); 
     HoursNumericUpDown.Value = HoursNumericUpDown.Minimum; 
     MarkNumericUpDown.Value = MarkNumericUpDown.Minimum; 

     if (String.IsNullOrEmpty(MaterialTextBox.Text)) 
     { 
      MessageBox.Show("Enter Material Name Please.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
      //dataGridView1.Rows.Clear(); 
     } 
     else 
     { 
      /*if (MarkNumericUpDown.Value < 50) 
      { 
       int index = dataGridView1.Rows.Add(); 
       dataGridView1.Rows[1].Cells[4].Value = "F"; 
      } 
      else if (MarkNumericUpDown.Value > 50 && MarkNumericUpDown.Value <= 64) 
      { 
       dataGridView1.Rows[index].Cells[4].Value = "F"; 
      }*/ 
+2

Corrija el ejemplo del código. Preste atención a abrir y cerrar llaves – Dyppl

Respuesta

2

uso algo como lo siguiente:

if (String.IsNullOrEmpty(MaterialTextBox.Text)) 
+1

Esto no manejará los espacios en blanco – abhilash

+0

uffff mismo problema no funciona :( – sam

+0

gracias hombre funciona el único error fue mi código – sam

46

Pruebe esta condición en su lugar:

if (string.IsNullOrWhiteSpace(MaterialTextBox.Text)) { 
    // Message box 
} 

Esto se hará cargo de algunas cadenas que sólo contienen espacios en blanco y no se le tiene que lidiar con la igualdad de cadenas que a veces puede ser difícil

+0

gracias por la ayuda pero no funciona el mismo problema !!!: | – sam

+0

@ ghost_j1: por favor, proporcione algún contexto. Tal vez extienda su ejemplo de código para incluir todo el método – Dyppl

+0

@ghost, entonces está haciendo algo diferente. – lincolnk

0

Agregando a lo que @ Tjg184 dijo, se podría hacer algo como ...

if (String.IsNullOrEmpty(MaterialTextBox.Text.Trim())) 

...

+0

gracias hombre funciona el único error fue mi código – sam

11

Bueno, que están limpiando el cuadro de texto a la derecha antes de comprobar si está vacío

/* !! This clears the textbox BEFORE you check if it's empty */ 
MaterialTextBox.Clear(); 

HoursNumericUpDown.Value = HoursNumericUpDown.Minimum; 
MarkNumericUpDown.Value = MarkNumericUpDown.Minimum; 

if (String.IsNullOrEmpty(MaterialTextBox.Text)) 
{ 
     MessageBox.Show("Enter Material Name Please.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
      //dataGridView1.Rows.Clear(); 
} 
+0

Sí, gracias, noté este error estúpido hecho por mí ... pero después de este mensaje aparece el datagridview inserte una nueva fila así que hay ¿Es una forma de eliminarlo o quizás descartar toda la fila insertada o el progreso? – sam

0
if (MaterialTextBox.Text.length==0) 
{ 
message 

} 
1

Intente hacer lo siguiente

if (String.IsNullOrEmpty(MaterialTextBox.Text) || String.IsNullOrWhiteSpace(MaterialTextBox.Text)) 
    { 
     //do job 
    } 
    else 
    { 
     MessageBox.Show("Please enter correct path"); 
    } 

Hope i t ayuda a

Cuestiones relacionadas