2011-05-27 7 views
17

मैंने यह बयान दिया कि टेक्स्टबॉक्स खाली है या नहीं, लेकिन संदेशबॉक्स हमेशा दिखाता है कि टेक्स्टबॉक्स खाली है या नहीं।जांचें कि क्या टेक्स्टबॉक्स खाली है और संदेशबॉक्स लौटाएं?

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

मदद करता है, कृपया कोड नमूना को ठीक करें। ब्रेसिज़ को खोलने और बंद करने पर ध्यान दें – Dyppl

उत्तर

2

उपयोग जैसे कुछ निम्नलिखित:

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

यह व्हाइटस्पेस को संभाल नहीं पाएगा – abhilash

+0

uffff वही समस्या काम नहीं कर रहा है :( – sam

+0

धन्यवाद आदमी यह काम करता है यह एकमात्र गलती है मेरे कोड – sam

46

बजाय इस हालत का प्रयास करें:

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

यह कुछ तार कि इसमें केवल रिक्त स्थान वर्ण हो की देखभाल करेंगे और आप नहीं होगा स्ट्रिंग समानता से निपटना होगा जो कभी-कभी मुश्किल हो सकता है

+0

मदद के लिए धन्यवाद लेकिन एक ही समस्या काम नहीं कर रहा है !!!: – sam

+0

@ ghost_j1: कृपया, कुछ संदर्भ प्रदान करें। शायद पूरी विधि – Dyppl

+0

@ghost को शामिल करने के लिए अपना कोड नमूना बढ़ाएं, फिर आप कुछ और गलत कर रहे हैं। – lincolnk

0

क्या जोड़ रहा है @ Tjg184 कहा, तुम कुछ की तरह कर सकता है ...

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

...

+0

धन्यवाद आदमी यह काम करता है यह एकमात्र गलती है मेरे कोड – sam

11

ठीक है, तुम सही इससे पहले कि आप जाँच यदि वह रिक्त हो पाठ बॉक्स समाशोधन रहे

/* !! 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

हाँ धन्यवाद, मैंने देखा कि यह बेवकूफ गलती मेरे द्वारा की गई है ... लेकिन इस संदेश के बाद डेटाग्रिडव्यू नई पंक्ति डालें तो क्या वहां है इसे हटाने का तरीका है या शायद पूरी डाली गई पंक्ति या प्रगति को त्यागना ??? – sam

1

निम्नलिखित

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

आशा है कि मैं कोशिश कर रहा हूं टी