当我将列表框项目从listbox1移动到WPF中的列表框2时出现问题?

时间:2011-08-24 14:48:10

标签: c# wpf

我试图在这些行中实现一些东西,我有两个列表框listbox1和listbox2,我分别有添加和删除按钮将项目从一个列表框移动到另一个。由于某种原因我无法选择项目经过几次搬迁后,从列表框中。

我正在使用以下代码

添加按钮的逻辑

ArrayList listitems1 = new ArrayList();
        ArrayList listitems2 = new ArrayList();
        if (listBox2.SelectedItem == null)
        {
            System.Windows.MessageBox.Show("*Please Select unassigned Wks ");
            return;
        }
        else
        {
            try
            {
                foreach (string st in listBox2.Items)
                {
                    listitems1.Add(st);
                }

                foreach (string st1 in listBox1.Items)
                {
                    listitems2.Add(st1);
                }
                if (listBox2.SelectedIndex > -1)
                {
                    string value = listBox2.SelectedItem.ToString();
                    string text = listBox2.SelectedItem.ToString();
                    listitems2.Add(value);
                    listBox1.ItemsSource = listitems2;
                    listitems1.Remove(value);
                    listBox2.ItemsSource = listitems1;

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }



      **logic for delete button:**

        ArrayList listitems1 = new ArrayList();
        ArrayList listitems2 = new ArrayList();
        if (listBox2.SelectedItem == null)
        {
            System.Windows.MessageBox.Show("*Please Select unassigned Wks ");
            return;
        }
        else
        {
            try
            {
                foreach (string st in listBox2.Items)
                {
                    listitems1.Add(st);
                }

                foreach (string st1 in listBox1.Items)
                {
                    listitems2.Add(st1);
                }
                if (listBox2.SelectedIndex > -1)
                {
                    string value = listBox2.SelectedItem.ToString();
                    string text = listBox2.SelectedItem.ToString();
                    listitems2.Add(value);
                    listBox1.ItemsSource = listitems2;
                    listitems1.Remove(value);
                    listBox2.ItemsSource = listitems1;

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        ArrayList listitems1 = new ArrayList();
        ArrayList listitems2 = new ArrayList();
        try
        {
            foreach (string st in listBox2.Items)
            {
                listitems1.Add(st);
            }

            foreach (string st1 in listBox1.Items)
            {
                listitems2.Add(st1);
            }
            if (listBox1.SelectedIndex > -1)
            {
                string value = listBox1.SelectedItem.ToString();
                string text = listBox1.SelectedItem.ToString();
                listitems1.Add(value);
                listBox2.ItemsSource = listitems1;
                listitems2.Remove(value);
                listBox1.ItemsSource = listitems2;

            }
        }

1 个答案:

答案 0 :(得分:1)

试试这个。对于你的添加方法:

if (listBox2.SelectedItem == null)
{
    System.Windows.MessageBox.Show("*Please Select unassigned Wks ");
    return;
}
else
{
    if (listBox2.SelectedIndex > -1)
    {
        Object obj = listBox2.SelectedItem;
        listBox1.Items.Add(obj);
        listBox2.Items.Remove(obj);
    }
}

对delete方法执行类似操作。我认为问题可能是您使用ArrayLists作为ListBox项的源,并且ArrayLists超出范围。