5

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 information 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, blah, blah

    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);
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amc_rtty
  • 3,662
  • 11
  • 48
  • 73

5 Answers5

12

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
}
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
5

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.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
  • 1
    That's what I'd do. Then if you need to get info back out of the child form, make a property with a get method that your parent class can access. IDK where you got that tutorial from, but its WAY out – Nobody Nov 22 '10 at 16:48
  • Thank you very much, this solved it. Just curious, would it have been possible with using delegates as well? I understand that using delegates is too much for this simple problem but i'm just wondering if it was possible with those too. – Amc_rtty Nov 22 '10 at 17:51
  • 1
    Yes, you could pass in the actual value, or you could pass in a reference to MainForm and add properties to MainForm for SettingsForm to call, or you could pass in a delegate for SettingsForm to call, if sometimes you wanted it to call different methods to get the value. That last one would be weird though. Seems like an excuse to try to show a delegate in action. – Kate Gregory Nov 22 '10 at 18:41
4

I would suggest adding a property to SettingsForm.

Then, call it like this:

SettingsForm settingsForm = new SettingsForm(); 
settingform.myProperty = myPropertyvalue;
settingsForm.ShowDialog();   
B Pete
  • 936
  • 1
  • 7
  • 16
2

Add a constructor to your settings form which takes parameters and pass in any data you need there.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike Dour
  • 3,616
  • 2
  • 22
  • 24
2

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

Daniel Perez
  • 4,292
  • 4
  • 29
  • 54