将项目添加到Windows窗体应用程序的listBox中, [英] Adding Items to a listBox in a Windows Forms Application,

查看:99
本文介绍了将项目添加到Windows窗体应用程序的listBox中,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果单击perfomed按钮,如何将两个textBoxesradioButton(有两个,但只能选中一个)中的项目添加到listbox?

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?

这是我的代码:

            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:

推荐答案

好吧,看来您正在首先添加名称TextBox中的值,然后又添加了出生日期TextBox中的值.由于此处的出生日期为空,因此在列表框上将获得一个空字符串(因此为一个空行).

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.

如果您不想添加空值,则应在调用Add方法之前检查它们:

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.

这篇关于将项目添加到Windows窗体应用程序的listBox中,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆