【问题标题】:how to remove space after pressing buttons按下按钮后如何删除空格
【发布时间】:2013-10-17 09:59:07
【问题描述】:

当我遇到错误时,我正在执行 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();
        }
    }

当我将文本框留空并单击添加按钮时,它会显示错误消息,但仍会添加一个空格。我该如何阻止这种情况发生?

【问题讨论】:

    标签: c# button listbox space


    【解决方案1】:

    如果不想执行更多代码,需要在方法中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;
        }
    

    【讨论】:

      【解决方案2】:

      首先,如果您使用 CarNameData 列表作为通用集合列表 List&lt;string&gt;,它允许像 CarNameData.Sort(); 这样的内置排序方法

      第二件事你必须把你的代码像这样放在 else 部分

          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();
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2021-11-16
        • 2020-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-18
        相关资源
        最近更新 更多