0

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:

enter image description here

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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


    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.