Get variable value from Main form

How can I get Main varialbe's value from another form.
for example: get menu item's text, or get program caption's text, etc.

thanks
Edward


Answer this question

Get variable value from Main form

  • jmurray_mi

    you would need a reference to the other form/object and then access its properties however you need to make the properties/methods you wish to access public so this class can see it.

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=729974&SiteID=1

    take a look at the link above, it shows you on passing the object from one class to another and accessing it from this other class



  • gabo_uy

    You could give your dialog form a new public property of type MainForm. Before you call ShowDialog on the dialog form set that property to the current MainForm which you can reach as "this". In your dialog form you can than use the stored MainForm reference to access its properties. Like so:

    // somewhere in a click handler or menu handler in you MainForm:

    SecondForm f = new SecondForm();
    f.MainForm = this;
    f.ShowDialog();

    // in your SecondForm form class:

    private MainForm mainForm;

    public property MainForm MainForm
    {
    get { return mainForm; }
    set { mainForm = value; }
    }

    // use the mainForm field to access the MainForm instance in any code of your dialog form which I just called SecondForm in absense of the real name

    --
    SvenC


  • maddman

    thanks, but what I want to do is:
    I have the main or principal form already open, is where my app start, over this I open another form using ShowDialog(), in this form I would like to get the text property that the main form have, how can I get this info from an already open form.

    If I use MainForm dlg = new MainForm() in order to get properties info, I am creating a new one, and InitializeComponent is executed again, what I need is properties values from the original main application form.

    Is there a method or something like that to access already open forms. or main form is a kind of special form than have some restrictions

    thanks in advance for your help,
    Edward

  • MA2005

    Make the controls public or add public properties or methods to that form so that they are accessible to other parts of your app.

    --
    SvenC


  • Get variable value from Main form