Global variables project-wide

[Migrated content. Thread originally posted on 18 April 2012]

How would I go about storing variables and/or methods that can be accessed from any Form in the project? Coming from vb.net, I'm used to modules and am not sure what to do without them.

Alternately, is there a way a child Form can access its parent Form's user-defined properties and controls? So far I can only access and alter properties such as Self::Owner::Width, but no luck with the ones I created myself. For another vb analogy, that could be done using CType, so an equivalent might help if I knew of one. Thanks in advance
Parents
  • Verified Answer

    Here is one way of accomplishing this task...

    I have attached a demo project that uses a class with static data to show you how this can be accessed from within different forms.

    When Form1 is created it saves its "self" reference in a static data item in class shareddata.
    Form1 then creates a new instance of Form2 and does ShowDialog on it.

    When the button is clicked it will set the value of a textbox to the value of a string property defined in Form1.
    It does this by accessing the stored reference to Form1 in the shareddata class.

    Thanks.

    parentform.zip
Reply
  • Verified Answer

    Here is one way of accomplishing this task...

    I have attached a demo project that uses a class with static data to show you how this can be accessed from within different forms.

    When Form1 is created it saves its "self" reference in a static data item in class shareddata.
    Form1 then creates a new instance of Form2 and does ShowDialog on it.

    When the button is clicked it will set the value of a textbox to the value of a string property defined in Form1.
    It does this by accessing the stored reference to Form1 in the shareddata class.

    Thanks.

    parentform.zip
Children
No Data