Adding Items to a listBox in a Windows Forms Application,

不问归期 提交于 2019-12-12 06:38:27

问题


How I can add Items from two textBoxes and a radioButton (there are two but only one can be checked) to a listbox if you perfomed a click on a button?

This is my code:

            listBox1.Items.Clear();
            for (int i = 1; i < listBox1.Items.Count; i++)
            {
                if (textBox1.Text == listBox1.Items[i].ToString())
                {
                    equal1 = true;
                }
                else
                {
                    equal1 = false;
                    break;
                }

            }
            if (equal1 == false)
            {
                listBox1.Items.Add(textBox1.Text);
            }
            for (int i = 1; i < listBox1.Items.Count; i++)
            {
                if (textBox2.Text == listBox1.Items[i].ToString())
                {
                    equal2 = true;
                }
                else
                {
                    equal2 = false;
                    break;
                }
            }
            if (equal2 == false)
            {
                listBox1.Items.Add(textBox2.Text);
            }
            if (radioButton1.Checked == true)
        {
            listBox1.Items.Add("Male");
        }
        else if (radioButton2.Checked == true)    
        {
            listBox1.Items.Add("Female");
        }

I want those items to be added with no blank space between them because this what I get:


回答1:


Well, it looks like you're first adding the value from the name TextBox and then the value from the Birth date TextBox. Since birth date is empty here, you'll get an empty string (thus an empty row) on the ListBox.

If you don't want empty values added, you should check for them before the call to the Add method:

if (equal2 == false && !string.IsNullOrEmpty(textBox2.Text)
{
    listBox1.Items.Add(textBox2.Text);
}

However, what you're doing doesn't seem very straight forward. I'd recommend yo rethink the way you check for values and process them.




回答2:


From looking at your screen, it looks like one of your birth date text boxe is either blank or contains whitespaces. Perhaps this could be the problem?

In any event, this problem should be easily solved by debugging. Put a breakpoint at each listbox1.Items.Add function call and check what value is being added to the list box.




回答3:


Yes, That's exactly what I did :) my code looks like this now:

private void button1_Click(object sender, EventArgs e)
    {

        listBox1.Items.Clear();

        if(! String.IsNullOrEmpty(textBox1.Text))
        {
            listBox1.Items.Add(textBox1.Text);
        }
        if(! String.IsNullOrEmpty(textBox2.Text))
        {
            listBox1.Items.Add(textBox2.Text);
        }

        if (radioButton1.Checked == true)
        {
            listBox1.Items.Add("Male");
        }
        else if (radioButton2.Checked == true)

        {
            listBox1.Items.Add("Female");
        }


    }


来源:https://stackoverflow.com/questions/42161690/adding-items-to-a-listbox-in-a-windows-forms-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!