My form1 has a checkedlistbox that is being populated by datatable, then there's a button to open form2 that will add records in checkboxlist. Now when the form2 closes, checkedlistbox from form1 will refresh. How to do this?

Recommended Answers

All 11 Replies

You can attach an event handler to the second form's Closed event when you open it and call code when it fires:

public partial class Parent : Form
{
    public Parent()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Child child = new Child(); //create new isntance of form
        child.FormClosed += new FormClosedEventHandler(child_FormClosed); //add handler to catch when child form is closed
        child.Show(); //show child
    }

    void child_FormClosed(object sender, FormClosedEventArgs e)
    {
        //when child form is closed, this code is executed
        this.Refresh();
    }
}

It duplicates the first form..

I'm sorry, i dont understand. What duplicates the first form?

I enter the form2 using the button from form1, which has the following code

Form2.ShowDialog();

then if the form2 closes it will refresh the form1 but because the form1 is still there, it has been duplicated because of creating an instance of form1.

If you show a form modally (using ShowDialog), code execution on the calling form pauses. When the second form closes the code resumes on the first form.
You dont need to use the code i showed you if you are using modal forms, you can just refresh the first form when the second closes:

Form2 frm = new Form2();
frm.ShowDialog(); //code pauses here until Form2 is closed
this.Refresh(); //put any code here that you want to run after form 2 is closed

Can you post more code, you shouldnt be creating a new instance of Form1 when the second form closes :/

Form1 has a button to go on form2

Form2 form2 = new Form2();
form2.ShowDialog();

Form2 has an add button, wherein if it successfully add it will close and form1's checkedlistbox will refresh..

Are you calling Form1 something = new Form1() anywhere inside Form2?
Showing and closing Form2 should not create a second copy of Form1.
If you only want to update the CheckedListBox if Form2 finsihed successfully you can use the DialogResult property of Form2:

public partial class Form2 : Form
{
    private void ButtonOK_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.OK;
        this.Close();
    }
}

public partial class Form1 : Form
{
    private void Button_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2();
        if(frm.ShowDialog() == DialogResult.OK)
        {
            //refresh checklistbox here
        }
    }
}

My bad..

This is the current code in my form1

private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (checkedListBox1.GetItemChecked(checkedListBox1.Items.IndexOf("Others, specify")))
            {
                Form2 form2 = new Form2();
                form2.FormClosed += new FormClosedEventHandler(form2_FormClosed);
                form2.ShowDialog();
            }
        }

        void form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Refresh();
        }

but if I close the second form, it doesn't refresh.

Sorry, your original post didn't mention that you were showing the form modally (which is what happens when you call ShowDialog), the code above was an example of how you can execute code in Form1 when Form2 is closed. this.Refresh() was an example, it just causes the form to repaint, it wouldn't update the listbox..sorry for any confusion.

You don't need this code at all. You are showing Form2 with ShowDialog so you can run the code to update the listbox in the same method that shows Form2 because the code will pause until Form2 is closed. See the code in my last post to see how you set the DialogResult in Form2 then run code based on the result in Form1.

I tried the code, if I click OK in form2 it doesn't refresh the checkedlistbox that is being generated by a datatable using .Refresh().

This is now my code to refresh checkedlistbox..

if (checkedListBox1.GetItemChecked(checkedListBox1.Items.IndexOf("Others, specify")))
            {
                Form2 form2 = new Form2();
                if (form2.ShowDialog() == DialogResult.OK)
                {
                    string query = "select author_name from author order by author_name";
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = query;
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;
                    dt.Clear();
                    checkedListBox1.Items.Clear();
                    checkedListBox1.Items.Add("Others, specify").ToString();
                    da.Fill(dt);

                    foreach (DataRow dr in dt.Rows)
                    {
                        checkedListBox1.Items.Add(dr["author_name"].ToString());
                    }
                }
            }
commented: you got it :) +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.