CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2010
    Posts
    11

    Exclamation Accessing Objects Between Forms

    Hi all,

    I'm fairly new to C# and have run into a problem. I have one form mainForm which creates a new form optionsForm when a button is clicked.

    private void button_Click(object sender, EventArgs e)
    {
    Options optionsForm = new Options(this);
    DialogResult result = optionsForm.ShowDialog(this);
    }

    In the optionsForm, I've tried to pass mainForm by reference so I could access its objects.

    private form_VisionSystem mainForm = null;
    public Options(Form callingForm)
    {
    mainForm = callingForm as form_VisionSystem;
    InitializeComponent();

    }


    I would like to access an object from mainForm and that object's methods from optionsForm. Is this possible, and if so how could I go about achieving this.

    Any help would be greatly appreciated.
    Regards,
    Antonio.

  2. #2
    Join Date
    Mar 2010
    Posts
    11

    Re: Accessing Objects Between Forms

    Woops. I forgot to mention i used .NET 3.5.

  3. #3
    Join Date
    Feb 2010
    Posts
    4

    Re: Accessing Objects Between Forms

    you can create public properties to your forms, so that other forms can access it data or methods.
    you can also use delegates for your methods.

  4. #4
    Join Date
    Mar 2010
    Posts
    11

    Re: Accessing Objects Between Forms

    Thanks for the reply,

    I didn't know that public properties could be used to access methods,
    I thought they were only for getting and setting variable values.

    Regards,
    Antonio.

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Accessing Objects Between Forms

    Quote Originally Posted by Antonio69 View Post
    Thanks for the reply,

    I didn't know that public properties could be used to access methods,
    I thought they were only for getting and setting variable values.

    Regards,
    Antonio.
    Properties are methods. I would not go this route as you will have to pass references to your main form back and forth and that gets messy fast. I would instead use events to tell your main form when to update the UI.
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured