亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在winform之間傳遞變量

在winform之間傳遞變量

智慧大石 2019-12-06 15:10:02
我在Windows.Forms應用程序中遇到有關委托的問題。有兩種形式:主窗體,其中有一個名為“設置”的按鈕?!霸O置”形式,這是“子”形式。當我單擊主窗體中的“設置”按鈕時,它將打開“設置”窗體的一個實例。我的問題是,當我打開變量時,需要將其傳遞給“設置”表單。這樣新表單將顯示變量文本。我不知道如何在子“設置”表單中檢索信息。我通過遵循在線教程來做到這一點,但無法從教程中了解如何讀取目標表單中的信息。到目前為止,這是我做的主要形式的代碼:public partial class MainForm : Form{    /// <summary>    /// delegate to send data between forms    /// </summary>    public delegate void PageInfoHandler(object sender, PageInfoEventArgs e);    /// <summary>    /// event of the delegate    /// </summary>    public event PageInfoHandler PageInfoRetrieved;    //other stuff, events blabla    private void toolStripBtnSettings_Click(object sender, EventArgs e)    {        PageInfoEventArgs args = new PageInfoEventArgs(SomeString);        this.OnPageInfoRetrieved(args);        SettingsForm settingsForm = new SettingsForm();        settingsForm.ShowDialog();      }    private void OnPageInfoRetrieved(PageInfoEventArgs args)    {        if (PageInfoRetrieved != null)            PageInfoRetrieved(this, args);    }}
查看完整描述

3 回答

?
飲歌長嘯

TA貢獻1951條經驗 獲得超3個贊

將所需的任何信息傳遞到“設置”表單的構造函數中,并提供所需的訪問器方法。


public class SettingsForm : WinForm

{

    private string m_Data;

    private int m_nExample = 0;


    // ctor

    public SettingsForm(string _data)

    {

        m_Data = data;  // you can now use this in SettingsForm

    } // eo ctor


    public int Example {get{return(m_nExample);} }

} // eo class SettingsForm

在上面的“示例”中,您可以使用字符串構造一個SettingForm并獲取它可能使用的整數。在您的代碼中:


private void toolStripBtnSettings_Click(object sender, EventArgs e)

{

    PageInfoEventArgs args = new PageInfoEventArgs(SomeString);

    this.OnPageInfoRetrieved(args);


    SettingsForm settingsForm = new SettingsForm("some data to pass");

    settingsForm.ShowDialog();  


    int result = settingsForm.Example; // retrieve integer that SettingsForm used

}


查看完整回答
反對 回復 2019-12-06
?
長風秋雁

TA貢獻1757條經驗 獲得超7個贊

結算表格是一個類。現在是您的事了,您可以使用它來做自己喜歡的事。因此,向其構造函數添加參數(或任意多個參數)。然后在MainForm中調用SettingsForm(無論如何),就一切就緒。


查看完整回答
反對 回復 2019-12-06
  • 3 回答
  • 0 關注
  • 591 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號