hi frds,
I want to check items of checked list box of one form from second form .
while i put the same items in listbox at second form.
I just want to check mark on checkedlist box control on form one if i select listbox item on
second form.

thank you

Let me re-state your question to see if I understand you correctly.

You have two forms. First form has a check list box, and the second form has a listbox.
When the user checks an item in the check list box, you want that item to be added to the listbox on form 2. If the user unchecks the checkbox item, then do nothing.

If the user clicks an item in the listbox, you want to make sure the checklist box item on form 1 is checked.

To do this, form 2 must be visible to form1, and the listbox must have its modifier property set to public.

Create an event handler on the checklistbox, so that when a user changes the state of a check box to checked, it will see if this item exists in the listbox. If not, then add it.

When creating form2 , add an event handler in form1 for the listbox Click event, and have it verify the corresponding checkbox is checked.

public partial class Form1 : Form
    {
        private Form2 form2;
        
        public Form1()
        {
            InitializeComponent();
            form2 = new Form2();
            form2.listBox1.Click += new EventHandler(listBox1_Click);
            form2.Show();
        }

        void listBox1_Click(object sender, EventArgs e)
        {
            int i = form2.listBox1.SelectedIndex;
            string item = form2.listBox1.Items[i].ToString();
            int y = checkedListBox1.Items.IndexOf(item);
            checkedListBox1.SetItemCheckState(y, CheckState.Checked);
        }

        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.NewValue == CheckState.Checked)
            {
                string item = checkedListBox1.Items[e.Index].ToString();

                if (form2.listBox1.Items.IndexOf(item) == -1)
                    form2.listBox1.Items.Add(item);
            }
        }
    }

Error checking not included...

hi frd.
some problem in this block, here show error messege" It takes zero argument ;"

public Form1()
        {
            InitializeComponent();
            form2 = new Form2();
            form2.listBox1.Click += new EventHandler(listBox1_Click);
            form2.Show();
        }

what i do ?

Edited 3 Years Ago by Dani: Formatting fixed

Okay,
I assume you must be using VS2008, since it is less forgiving than VS2005.

You need to change it a little, and add some error checking.

public partial class Form1 : Form
    {
        private Form2 form2;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void listBox1_Click(object sender, EventArgs e)
        {
            int i = form2.listBox1.SelectedIndex;
            if (i > -1)
            {
                string item = form2.listBox1.Items[i].ToString();
                int y = checkedListBox1.Items.IndexOf(item);
                checkedListBox1.SetItemCheckState(y, CheckState.Checked);
            }
        }

        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.NewValue == CheckState.Checked)
            {
                string item = checkedListBox1.Items[e.Index].ToString();

                if (form2.listBox1.Items.IndexOf(item) == -1)
                    form2.listBox1.Items.Add(item);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (form2 == null)
            {
                form2 = new Form2();
                form2.listBox1.Click += new EventHandler(listBox1_Click);
                form2.Show();
            }
        }
    }

As I stated earlier, there was no error checking provided in the example.
Anyway, the listBox1_Click event handler now checks to make sure the index value is valid before processing it.

I just posted a reply on this thread:
"How To pass multidimensional array from one form to another"

My reply should cover your requirement as well.

Hi..

I have a same need as of above but i also need to retain the status of checkedlistbox and the listbox when the form loads next time..

Can anyone please help me??

bhagyap
Are you wanting to store the data to a file so that when the application starts it performs the checks and listbox actions? or just retain what the user did in this session for redisplay when the form is reloaded?
Really does not make much difference if you create a simple structure with two elements. The StringValue and the CheckValue. Then pass that in a collection to the form when it opens and it can iterate though to perform the checks and listbox load.
Now if it were me and I wanted to reload those values anytime the application started, I would create a simple data table with two columns (StrValue,CheckValue), populate it with the defaults if it did not exist. If it does exist, then load it from disk (built in function of a datatable). Pass this table to the form. Anytime the form works with this datatable, it will make sure it is updated, then when the application closes, just write the datatable to disk as XML (built in function of a datatable).
Are you looking for code to do this.. or can you figure it out on your own?

hi..

i just want to retain the previous session whenever the form loads..

Plz help me with the code...


Thanks in advance..

i want to know one more thing please help me..

i add items from checkedlistbox to listbox tat is fine..

but i want to add it from one form(which contains checkedlistbox) and later view it in another form(form which contains listbox)..

can u please help me with this also??

Thanks..

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