Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh parent form when in child form is button clicked?

Tags:

c#

winforms

then closing child form is working this command:

private void listView1_MouseDoubleClick(object sender, EventArgs e)
{
    ListViewItem item = listView1.SelectedItems[0];
    string s = item.SubItems[6].Text;
    q = m;
    CommercialOfferEditProperties ob = new CommercialOfferEditProperties(s, q);
    ob.FormClosed += new FormClosedEventHandler(ob_FormClosed);
    ob.Show(); //show child
}

void ob_FormClosed(object sender, FormClosedEventArgs e)
{
   some action
}

But how to run action ob_FormClosed or run created new action, when in child form is button clicked?

like image 212
Ignas Avatar asked Sep 13 '11 18:09

Ignas


2 Answers

  1. Add a OnClick event to your child form (CommercialOfferEditProperties)
  2. Subscribe to in in the parent form.
  3. Trigger OnClick every time the child forms button is clicked.

That way you will be able to notify the parent.

Example:

//Child form declaration

public class CommercialOfferEditProperties:Form
{

public event EventHandler ButtonClicked;

public void NotifyButtonClicked(EventArgs e)
{
       if(ButtonClicked != null)
       ButtonClicked(this,e);

}

...

}

Parent form:

private void listView1_MouseDoubleClick(object sender, EventArgs e)
        {
            ListViewItem item = listView1.SelectedItems[0];
            string s = item.SubItems[6].Text;
            q = m;
            CommercialOfferEditProperties ob = new CommercialOfferEditProperties(s, q);
            ob.FormClosed += new FormClosedEventHandler(ob_FormClosed);
            ob.ButtonClicked += new EventHandler(ob_ButtonClicked);
            ob.Show(); //show child
        }

        void ob_FormClosed(object sender, FormClosedEventArgs e)
        {
           //process form close
        }

        void ob_ButtonClicked(object sender, EventArgs e)
        {
           //process button clicked
        }
like image 142
Maxim V. Pavlov Avatar answered Nov 16 '22 03:11

Maxim V. Pavlov


You could declare the button as public or (better) create a readonly property exposing your button publicly.

public Button TheButton { get { return button1; } }

and then do

ob.TheButton.Clicked += new ....
like image 35
Olivier Jacot-Descombes Avatar answered Nov 16 '22 05:11

Olivier Jacot-Descombes