按下按钮后如何移除空间

时间:2013-10-17 09:59:07

标签: c# button listbox space

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

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

2 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分: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();
            }
        }
    }