hi there,

I have to list boxes and in one list box there is all the names of the employees, there is two buttons which represent the arrows to sending the selected item from list box to another, how can I get the values in on list box to a text box separated by a comma, how can I solve this,

thanxxx

You are binding listbox with comma seperated list. instead of this just create a class which will have Key, Value property. prepare List<Object of class> and bind it with listbox and set DisplayMember and ValueMember as per your requirement.

When you select item(s) from one listbox and move those to another listbox just add clone of selected item' object(here object is the object of class you have created for binding).

You are binding listbox with comma seperated list. instead of this just create a class which will have Key, Value property. prepare List<Object of class> and bind it with listbox and set DisplayMember and ValueMember as per your requirement.

When you select item(s) from one listbox and move those to another listbox just add clone of selected item' object(here object is the object of class you have created for binding).

hey no no,

when i select one item in the first list box and press the button it should remove from the current list box and go to the second list box, how can i do this, and vise versa??

thanxx

adding in my previous answer, When you select item from one listbox and move to another listbox just add clone of selected item'object to another listbox and remove from first listbox.

alternate, instead of adding, you need to bind same list of employees to both listbox. here you need to first time hide all items of destination listbox. while you move item from source listbox just hide this item and set visibility of same item in destination listbox. and you can use visible items from destination listbox for further operation.

adding in my previous answer, When you select item from one listbox and move to another listbox just add clone of selected item'object to another listbox and remove from first listbox.

alternate, instead of adding, you need to bind same list of employees to both listbox. here you need to first time hide all items of destination listbox. while you move item from source listbox just hide this item and set visibility of same item in destination listbox. and you can use visible items from destination listbox for further operation.

hey can you give more details????

i have attached a doc file and the question is over in the document with the interface

thanx

Here i am writing code for select items from source list. same as reverse you can do

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        List<Employee> employeeList = new List<Employee>();
        List<Employee> selectedEmployeeList = new List<Employee>();

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonUnSelect_Click(object sender, EventArgs e)
        {

        }

        private void buttonSelect_Click(object sender, EventArgs e)
        {
            if (listBoxSource.SelectedItems.Count > 0)
            {
                foreach (int index in listBoxSource.SelectedIndices)
                {
                    selectedEmployeeList.Add(employeeList[index].Clone());
                }

                for (int index = listBoxSource.SelectedIndices.Count - 1; index >= 0; index--)
                {
                    employeeList.RemoveAt(listBoxSource.SelectedIndices[index]);
                }

            }

            RefreshSourceList();
            RefreshDestinationList();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            employeeList.Add(new Employee(1, "Emp1"));
            employeeList.Add(new Employee(2, "Emp2"));
            employeeList.Add(new Employee(3, "Emp3"));
            employeeList.Add(new Employee(4, "Emp4"));
            employeeList.Add(new Employee(5, "Emp5"));
            employeeList.Add(new Employee(6, "Emp6"));

            listBoxSource.DataSource = employeeList;
            listBoxSource.DisplayMember = "Name";
            listBoxSource.ValueMember = "EmployeeId";


        }

        private void RefreshSourceList()
        {
            listBoxSource.DataSource = null;
            listBoxSource.DataSource = employeeList;
            listBoxSource.DisplayMember = "Name";
            listBoxSource.ValueMember = "EmployeeId";
        }

        private void RefreshDestinationList()
        {
            listBoxSelected.DataSource = null;
            listBoxSelected.DataSource = selectedEmployeeList;
            listBoxSelected.DisplayMember = "Name";
            listBoxSelected.ValueMember = "EmployeeId";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string selectedEmps = string.Empty;
            foreach (Employee emp in selectedEmployeeList)
            { 
                    selectedEmps  += "\n\r" + emp.Name;
            }

            MessageBox.Show("You have selected Following employees.\n\r\n\r" + selectedEmps);

        }
    }

    public class Employee
    {
        private int _employeeId;
        private string _name;

        public Employee(int id, string name)
        {
            _employeeId = id;
            _name = name;
        }
        public int EmployeeId
        {
            get { return _employeeId; }
            set { _employeeId = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public Employee Clone()
        {
            return this.MemberwiseClone() as Employee;
        }
    }
}

Here i am writing code for select items from source list. same as reverse you can do

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        List<Employee> employeeList = new List<Employee>();
        List<Employee> selectedEmployeeList = new List<Employee>();

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonUnSelect_Click(object sender, EventArgs e)
        {

        }

        private void buttonSelect_Click(object sender, EventArgs e)
        {
            if (listBoxSource.SelectedItems.Count > 0)
            {
                foreach (int index in listBoxSource.SelectedIndices)
                {
                    selectedEmployeeList.Add(employeeList[index].Clone());
                }

                for (int index = listBoxSource.SelectedIndices.Count - 1; index >= 0; index--)
                {
                    employeeList.RemoveAt(listBoxSource.SelectedIndices[index]);
                }

            }

            RefreshSourceList();
            RefreshDestinationList();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            employeeList.Add(new Employee(1, "Emp1"));
            employeeList.Add(new Employee(2, "Emp2"));
            employeeList.Add(new Employee(3, "Emp3"));
            employeeList.Add(new Employee(4, "Emp4"));
            employeeList.Add(new Employee(5, "Emp5"));
            employeeList.Add(new Employee(6, "Emp6"));

            listBoxSource.DataSource = employeeList;
            listBoxSource.DisplayMember = "Name";
            listBoxSource.ValueMember = "EmployeeId";


        }

        private void RefreshSourceList()
        {
            listBoxSource.DataSource = null;
            listBoxSource.DataSource = employeeList;
            listBoxSource.DisplayMember = "Name";
            listBoxSource.ValueMember = "EmployeeId";
        }

        private void RefreshDestinationList()
        {
            listBoxSelected.DataSource = null;
            listBoxSelected.DataSource = selectedEmployeeList;
            listBoxSelected.DisplayMember = "Name";
            listBoxSelected.ValueMember = "EmployeeId";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string selectedEmps = string.Empty;
            foreach (Employee emp in selectedEmployeeList)
            { 
                    selectedEmps  += "\n\r" + emp.Name;
            }

            MessageBox.Show("You have selected Following employees.\n\r\n\r" + selectedEmps);

        }
    }

    public class Employee
    {
        private int _employeeId;
        private string _name;

        public Employee(int id, string name)
        {
            _employeeId = id;
            _name = name;
        }
        public int EmployeeId
        {
            get { return _employeeId; }
            set { _employeeId = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public Employee Clone()
        {
            return this.MemberwiseClone() as Employee;
        }
    }
}

hey,
in line 14 and 15, hwat is Employeee???

#
List<Employee> employeeList = new List<Employee>();
#
List<Employee> selectedEmployeeList = new List<Employee>();

Note : i load the values to the source list box from a function that gets all the employees names

Employee is the class i have created in code i had given.
can u re-check code again?

hey i don't hve a call like that what sholud i put over there???

thankx
appriciate a lot if ouy could help me

i don't know in which form your input employee data. if its just employee id and name than you can create object of Employee from your input data. can you tell me in which form your data is?

i don't know in which form your input employee data. if its just employee id and name than you can create object of Employee from your input data. can you tell me in which form your data is?

hey i have a function in the user calss called fillEmpName()
so i call that method

User u = new User();
u.fillEmpName();

and then i add it to the list box....

that is the way how i do it....

This article has been dead for over six months. Start a new discussion instead.