Passing values between windows forms

Hi,

can anyone please suggest what is the most efficient method of passing values between Windows forms. I am developing an application For smart devices where we dont have the concept of MDI forms. So how do we pass values between windows forms Is static variables the only option.

Please reply asap.

Thanks,

anzrul



Answer this question

Passing values between windows forms

  • Borko

    Usually when you have multiple windows in an application, one form opens another by creating an instance of the new window class, then calling the .Show() method. The first window, the opener, will have a reference to the opened window, so you can use this reference to comminicate. You might put a reference to the opening window on the opened window if you wish to communicate the other way.

    Remember to change the scope of any variables on the opened window to be public in scope so that it can be accessed by the opening window.


  • Neil D

    Here's an example. An opening window (let's call it frmMain) pops up a new form with a file name parameter in the constructor. The opened form (imgPr) allows the user to change the value of the file name. When the form is dismissed, the main form (frmMain) may read the value of the filename to determine whether it's been changed, via a textbox which is public in scope (named "FullFileName" on the imgPr form):

    -- Code within a method on main form (frmMain) --

    // Show Image Properties form
    ImageProperties imgPr = new ImageProperties(
    @"c:\temp\file1.jpg");
    imgPr.ShowDialog();

    // Validate that file still exists after dismissing form
    FileInfo fileInfo = new FileInfo(
    imgPr.FullFileName.Text);


    -- Code on ImageProperties (imgPr) --

    // Definition of public textbox
    public System.Windows.Forms.TextBox FullFileName;

    // Constructor:
    public ImageProperties(
    string fullFileName)
    {
    // Capture file name
    this.FullFileName.Text = fullFileName;

    // Do other work...
    ....
    }

    Note that in this case, I use a modal dialog, but the same method will work with non-modal forms as well (called using .Show() rather than .ShowDialog())


  • Passing values between windows forms