簡體   English   中英

單擊子窗體中的按鈕時如何刷新父窗體?

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

然后關閉子窗體正在使用此命令:

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
}

但是如何在子窗體中單擊按鈕時運行操作ob_FormClosed或運行創建的新操作?

  1. 將 OnClick 事件添加到您的子表單 ( CommercialOfferEditProperties )
  2. 在父表單中訂閱。
  3. 每次單擊子窗體按鈕時觸發 OnClick。

這樣你就可以通知家長了。

例子:

//Child form declaration

public class CommercialOfferEditProperties:Form
{

public event EventHandler ButtonClicked;

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

}

...

}

父表格:

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
        }

您可以將按鈕聲明為 public 或(更好)創建一個只讀屬性公開您的按鈕。

public Button TheButton { get { return button1; } }

然后做

ob.TheButton.Clicked += new ....

我相信您正試圖在錯誤的位置執行此操作...但是,實現您想要的唯一方法是將父控件傳遞給構造函數中的子控件或將其設置為屬性(兩者的壞主意,對於不同的原因)或者您可以添加您的父表單注冊的事件。 然后調用Parent.Refresh()方法。

您應該有一些類似於父表單中的代碼:

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

您可以添加以下代碼:

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

注意:可能需要根據子窗體的對話結果更改 Dialog.OK。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM