开发者

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

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?


  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
        }


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 ....


I believe you are attempting to do this in the wrong location... However the only way to achieve what you are wanting is to pass the parent control to the child in the constructor or set it as a property (BAD IDEA for both, for different reasons) or you can add an event that your parent form registers for. Then call the Parent.Refresh() method.

You should have some code that is similar to in the parent form:

ChildDialog dialog = new ChildDialog();
dialog.ShowDialog(this);

you can add the following code:

if(dialog.DialogResult == DialogResult.OK)
{
   this.Refresh();
}

Note: the Dialog.OK may need changed based on your dialog results for the the child form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜