Passing variable between winforms

2019-01-03 18:35发布

I have a problem concerning delegates in a Windows.Forms application.

There are two forms:

  1. the main form, which has a button named "Settings".

  2. the "settings" form, this is the "child" form.

When I click the "Settings" button in the main form, it opens an instance of the Settings form.

My problem is that I need to pass a variable to the Settings form, when I open it. So that the new form will show the variable text. I don't know how to retrieve the information in the child "Settings" form. I did this by following a tutorial online and could not understand from the tutorial how to read the info in the destination form.

Here's what I've done so far, the code in the main form:

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);
    }
}

5条回答
Fickle 薄情
2楼-- · 2019-01-03 19:13

Pass any information you want to in to the constructor of Settings form, and provide accessor methods for things you need out of there.

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

In the above "example" you can construct a SettingForm with a string and get at an integer it may use. In your code:

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
}
查看更多
乱世女痞
3楼-- · 2019-01-03 19:14

The Setttings form is a class. It's yours now and you can do what you like with it. So add a parameter (or however many you want) to its constructor. Then in your MainForm call SettingsForm(whatever) and you're all set.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-03 19:22

Why don't you add a constructor to your settings form which takes parameters and pass in any data you need there?

查看更多
男人必须洒脱
5楼-- · 2019-01-03 19:33

You can create a parametrized constructor for your settings form which accepts the text, and sets it to a property in the form

public partial class SettingsForm : Form
{

   public string DisplayText {get;set;}

   public SettingsForm(string text)
   {
        DisplayText = text;
   }
}

then, you'd just initialize the settings from like this (From your mainform)

var settingsForm = new SettingsForm("my init text");

your settings form will be properly initialized, and you have the desired text in the DisplayText property ready to use

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-01-03 19:40

I would suggest adding a property to SettingsForm.

Then, call it like this:

SettingsForm settingsForm = new SettingsForm(); 
settingform.myProperty = myPropertyvalue;
settingsForm.ShowDialog();   
查看更多
登录 后发表回答