如何将值从一个列表框传递到另一个列表框 [英] How to pass values from one listbox to another listbox

查看:76
本文介绍了如何将值从一个列表框传递到另一个列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在winform中将值从一个列表框传递到另一个列表框



我尝试过:



How to pass values from one listbox to another listbox in winform

What I have tried:

private void btnMovetoList_Click(object sender, EventArgs e)
        {
            listBox2.SelectionMode = SelectionMode.MultiSimple;
            try
            {
                if (listBox1.Items.Count > 0)
                {
                    DataRowView drv = null;
                    for (int i = 0; i < listBox1.SelectedItems.Count; i++)
                    {
                        drv = (DataRowView)listBox1.SelectedItems[i];

                        string valueOfItem = drv["Form_Name"].ToString();
                        //string  Item = drv["Form_No"].ToString();
                        listBox2.Items.Add(valueOfItem);
                        //listBox1.Items.Remove(Item);
                    }

                    
                    DataRowView rowView = listBox1.SelectedValue as DataRowView;

                    if (null == rowView)
                     {
                         return;
                     }

                    dt.Rows.Remove(rowView.Row);
                    dt.AcceptChanges();


                }

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

            chkSelectAll1.Checked = false;
        }


        private void btnMoveReturn_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < listBox2.SelectedItems.Count; i++)
                {
                    String valueOfItem = listBox2.SelectedItems[i].ToString();
                    dt.Rows.Add(valueOfItem);
                    listBox2.SelectionMode = SelectionMode.MultiSimple;
                }
                for (int i = listBox2.SelectedIndices.Count - 1; i >= 0; i--)
                {
                    listBox2.Items.RemoveAt(listBox2.SelectedIndices[i]);
                }
                chkSelectAll2.Checked = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

推荐答案

我解决了,工作正常

I solved, its working fine
private void btnMovetoList_Click(object sender, EventArgs e)
        {
            listBox2.SelectionMode = SelectionMode.MultiSimple;
            try
            {
                List<object> _Objimp = new List<object>();
                List<object> _ObjExp = new List<object>();

                object _ObjimpList = new object();
                object _ObjExpList = new object();
                object _ObjStayItem = new object();

                if (listBox2.Items.Count > 0)
                {
                    for (int _listtwo = 0; _listtwo < listBox2.Items.Count; _listtwo++)
                    {
                        _ObjStayItem = new object();
                        _ObjStayItem = listBox2.GetItemText(listBox2.Items[_listtwo]);
                        _Objimp.Add(_ObjStayItem);
                    }
                }

                for (int _listtwoSelected = 0; _listtwoSelected < listBox1.SelectedItems.Count; _listtwoSelected++)
                {
                    _ObjimpList = new object();
                    _ObjimpList = listBox1.GetItemText(listBox1.SelectedItems[_listtwoSelected]);
                    _Objimp.Add(_ObjimpList);
                }

                for (int _listboxone = 0; _listboxone < listBox1.Items.Count; _listboxone++)
                {
                    _ObjExpList = new object();
                    if (!listBox1.GetSelected(_listboxone))
                    {
                        _ObjExpList = listBox1.GetItemText(listBox1.Items[_listboxone]);
                        _ObjExp.Add(_ObjExpList);
                    }
                }
                listBox1.DataSource = null;
                listBox1.Items.Clear();

                listBox2.DataSource = null;
                listBox2.Items.Clear();

                listBox1.DataSource = _ObjExp.OrderBy(r => r).ToList();
                listBox2.DataSource = _Objimp.OrderBy(p => p).ToList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            chkSelectAll1.Checked = false;
        }
        private void btnMoveReturn_Click(object sender, EventArgs e)
        {
            try
            {
                List<object> _Objimp = new List<object>();
                List<object> _ObjExp = new List<object>();

                object _ObjimpList = new object();
                object _ObjExpList = new object();
                object _ObjStayItem = new object();

                if (listBox1.Items.Count > 0)
                {
                    for (int _listtwo = 0; _listtwo < listBox1.Items.Count; _listtwo++)
                    {
                        _ObjStayItem = new object();
                        _ObjStayItem = listBox1.GetItemText(listBox1.Items[_listtwo]);
                        _Objimp.Add(_ObjStayItem);
                    }
                }

                for (int _listtwoSelected = 0; _listtwoSelected < listBox2.SelectedItems.Count; _listtwoSelected++)
                {
                    _ObjimpList = new object();
                    _ObjimpList = listBox2.GetItemText(listBox2.SelectedItems[_listtwoSelected]);
                    _Objimp.Add(_ObjimpList);
                }

                for (int _listboxone = 0; _listboxone < listBox2.Items.Count; _listboxone++)
                {
                    _ObjExpList = new object();
                    if (!listBox2.GetSelected(_listboxone))
                    {
                        _ObjExpList = listBox2.GetItemText(listBox2.Items[_listboxone]);
                        _ObjExp.Add(_ObjExpList);
                    }
                }

                listBox1.DataSource = null;
                listBox1.Items.Clear();

                listBox2.DataSource = null;
                listBox2.Items.Clear();

                listBox2.DataSource = _ObjExp.OrderBy(r => r).ToList();
                listBox1.DataSource = _Objimp.OrderBy(p => p).ToList();

                chkSelectAll2.Checked = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


这篇关于如何将值从一个列表框传递到另一个列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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