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

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

4

2 回答 2

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;
    }
于 2013-10-17T10:02:31.403 回答
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();
            }
        }
    }
于 2013-10-17T10:22:13.243 回答