2013-10-17 70 views
0

我正在做一個ITP分配,當我得到一個錯誤。對有問題的部分的代碼:如何按下按鈕後刪除空間

 private void btnAddWord_Click(object sender, EventArgs e) 
    { 
     //if the textbox is empty 
     if (string.IsNullOrEmpty(tbxAddWord.Text)) 
     { 
      MessageBox.Show("You have entered no characters in the textbox."); 
      tbxAddWord.Focus(); 
     } 
     //if the number of items in the listbox is greater than 29 
     else if (lbxUnsortedList.Items.Count > 29) 
     { 
      MessageBox.Show("You have exceeded the maximum number of words in the list."); 
      tbxAddWord.Text = ""; 
     } 
     //error message for entering word that is already in the list 
     bool contains = false; 
     for (int i = 0; i < lbxUnsortedList.Items.Count; i++) 
     { 
      if (lbxUnsortedList.Items[i].ToString().ToLower() == this.tbxAddWord.Text.ToString().ToLower()) 
      { 
       contains = true; 
      } 
     } 
     //if there is no match in the list 
     if (!contains) 
     { 
      //add word to the listbox 
      lbxUnsortedList.Items.Add(tbxAddWord.Text); 
      //update tbxListBoxCount 
      tbxListboxCount.Text = lbxUnsortedList.Items.Count.ToString(); 
      //onclick, conduct the bubble sort 
      bool swapped; 
      string temp; 
      do 
      { 
       swapped = false; 
       for (int i = 0; i < lbxUnsortedList.Items.Count - 1; i++) 
       { 
        int result = CarNameData[i].ToString().CompareTo(CarNameData[i + 1]); 
        if (result > 0) 
        { 
         temp = CarNameData[i]; 
         CarNameData[i] = CarNameData[i + 1]; 
         CarNameData[i + 1] = temp; 
         swapped = true; 
        } 
       } 

      } while (swapped == true); 
      tbxAddWord.Text = ""; 
     } 
     // if there is a match in the list 
     else 
     { 
      MessageBox.Show("The word that you have added is already on the list"); 
      tbxAddWord.Text = ""; 
      tbxAddWord.Focus(); 
     } 
    } 

,當我離開的文本框爲空,然後單擊添加按鈕,它自帶了錯誤信息,但還是增加了一個空白。我如何阻止這種情況發生?

回答

0

您需要return從方法,如果你不想執行更多的代碼:

if (string.IsNullOrEmpty(tbxAddWord.Text)) 
    { 
     MessageBox.Show("You have entered no characters in the textbox."); 
     tbxAddWord.Focus(); 
     return; 
    } 
    //if the number of items in the listbox is greater than 29 
    else if (lbxUnsortedList.Items.Count > 29) 
    { 
     MessageBox.Show("You have exceeded the maximum number of words in the list."); 
     tbxAddWord.Text = ""; 
     return; 
    } 
0

的第一件事是,如果你使用的是CarNameData列表,泛型集合列表List<string>它允許像CarNameData.Sort();內置的排序方法

你必須把你的代碼中的其他部分像這樣

private void btnAddWord_Click(object sender, EventArgs e) 
    { 
     //if the textbox is empty 
     if (string.IsNullOrEmpty(tbxAddWord.Text)) 
     { 
      MessageBox.Show("You have entered no characters in the textbox."); 
      tbxAddWord.Focus(); 
     } 
     else 
     { 
      //if the number of items in the listbox is greater than 29 
      if (lbxUnsortedList.Items.Count > 29) 
      { 
       MessageBox.Show("You have exceeded the maximum number of words in the list."); 
       tbxAddWord.Text = ""; 
      } 
      //error message for entering word that is already in the list 
      bool contains = false; 
      for (int i = 0; i < lbxUnsortedList.Items.Count; i++) 
      { 
       if (lbxUnsortedList.Items[i].ToString().ToLower() == this.tbxAddWord.Text.ToString().ToLower()) 
       { 
        contains = true; 
       } 
      } 
      //if there is no match in the list 
      if (!contains) 
      { 
       //add word to the listbox 
       lbxUnsortedList.Items.Add(tbxAddWord.Text); 
       //update tbxListBoxCount 
       tbxListboxCount.Text = lbxUnsortedList.Items.Count.ToString(); 
       //onclick, conduct the bubble sort 
       bool swapped; 
       string temp; 
       do 
       { 
        swapped = false; 
        for (int i = 0; i < lbxUnsortedList.Items.Count - 1; i++) 
        { 
         int result = CarNameData[i].ToString().CompareTo(CarNameData[i + 1]); 
         if (result > 0) 
         { 
          temp = CarNameData[i]; 
          CarNameData[i] = CarNameData[i + 1]; 
          CarNameData[i + 1] = temp; 
          swapped = true; 
         } 
        } 

       } while (swapped == true); 
       tbxAddWord.Text = ""; 
      } 
      // if there is a match in the list 
      else 
      { 
       MessageBox.Show("The word that you have added is already on the list"); 
       tbxAddWord.Text = ""; 
       tbxAddWord.Focus(); 
      } 
     } 
    } 
第二件事