C#将列表绑定到ListBox的正确方法

时间:2015-06-11 11:38:18

标签: c# forms button listbox

假设我有以下代码:

   private void button1_Click(object sender, EventArgs e)
        {
            string[] selection = comboEmail.GetItemText(comboEmail.SelectedItem).Split(',');

            Employee add = new Employee(Convert.ToInt32(selection[0]), selection[1], selection[2], selection[3]);

            if (!(comboEmail.SelectedIndex == 0))
            {

                if(!(listEmail.Items.Contains(add))){

                      displayEmp.Add(add);;
                      listEmail.DataSource = null;
                      listEmail.DataSource = displayEmp;
                }
                else
                {
                    MessageBox.Show(add.ToString() + " Already Added.");
                }

            }

        }

在我的添加按钮处理程序中:

private void button2_Click(object sender, EventArgs e)
{

    int indexRemoval = listEmail.SelectedIndex;

    if (indexRemoval != -1)
    {
        displayEmp.RemoveAt(indexRemoval);
        listEmail.DataSource = null;
        listEmail.DataSource = displayEmp;
    }

}

我的删除按钮处理程序:

// Move the password variable outside of the method scope,
// so it can be used by all buttonXX_Click methods.
private string password = "login";

private void button14_Click(object sender, EventArgs e) // Main Screen Password Login Button
{
    // No password variable defined here, instead we'll be using the one
    // we declared above.

    if (textBox1.Text == password)
    {
        // no changes here, omitted to make the answer shorter
    }
    else
    {
        MessageBox.Show("Password is Incorrect");
        textBox1.Clear();
    }
}

private void button19_Click(object sender, EventArgs e) // Admin Confirm Old Password Button
{
    // no changes here, omitted to make answer shorter
}
private void button17_Click(object sender, EventArgs e) // Admin Update New password Button
{
    if (textBox3.Text == textBox4.Text)
    {
        // By removing the local password variable and using the one
        // declared at the top, your change will be "remembered".

        password = textBox3.Text;
        MessageBox.Show("Password Changed");
        textBox1.Clear();
        textBox2.Clear();
        textBox3.Clear();
        textBox4.Clear();
        textBox3.Enabled = false;
        textBox4.Enabled = false;
    }
    else
    {
        MessageBox.Show("Password Does Not Match");
        textBox2.Clear();
        textBox3.Clear();
        textBox4.Clear();
        textBox3.Enabled = false;
        textBox4.Enabled = false;
    }
    button17.Click += ResetTimer;
}

我在ComboBox中有一个员工列表,选中后,我会添加到列表框中。在我的添加/删除按钮处理程序中,我正确地做了吗?如果您有一个绑定到控件的集合,并且您想要添加/删除项目,那么正确的做法是什么?

2 个答案:

答案 0 :(得分:0)

是的,您正确绑定了您的收藏。

我也会这样做:

listEmail.DisplayMember = "Name";

名称是您希望在列表框中显示的“员工”中的任何属性,否则它将尝试将对象转换为字符串。

答案 1 :(得分:0)

通常的做法是使用ObservableCollection<T>代替List<T>

    private ObservableCollection<Employee> displayEmp;

    public Form1()
    {
        InitializeComponent();
        displayEmp = new ObservableCollection<Employee>();
        // you need to assign DataSource only once
        listEmail.DataSource = displayEmp;
    }

ObservableCollection实现INotifyCollectionChanged接口,该接口通知ListView集合中的所有更改(添加和删除intem)。 当ObservableCollection通知有关更改时,您不需要强制刷新绑定,因此不需要

    listEmail.DataSource = null;
    listEmail.DataSource = displayEmp;