CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2008
    Posts
    10

    How can a Form window return a parameter ?

    I have my main form, and I would like to add a button that open a form with option. I want the value that are changed to update the one on the main form.

    What is the best or a good approach to do this ? (delegate, properties, etc... ?)

    If someone can point me to a good article about this or something. Cause I did a search on the website and on the forum and couldn't come up with something.

    Thank You
    Last edited by JNic; March 10th, 2008 at 09:57 PM.

  2. #2
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: How can a Form window return a parameter ?

    Open the form with what kind of option?

    The thread title suggests you should use DialogResult --- that's if you want to know what button the user clicked to close the form... Other than that, please elaborate
    It's not a bug, it's a feature!

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: How can a Form window return a parameter ?

    If you want to update controls in the main form from the second form, you can pass a reference to the main form to the second, and use it to update the controls.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Mar 2008
    Posts
    72

    Re: How can a Form window return a parameter ?

    If you're doing everything synchronously, then this approach would work well. If you're doing it asynchronously, that's a little more effort.

    Make a form (called "form2" in this example). Put at least one button on it, and set the DialogResult of the button to OK. Add a public property to the form, like this:

    Code:
        public partial class Form2 : Form
        {
            private string aProperty = "";
            public string AProperty
            {
                get { return this.aProperty; }
                set { this.aProperty = value; }
            }
    
            public Form2()
            {
                InitializeComponent();
            }
        }
    Modify that public property, adding whatever information you need to pass back to the main form.

    On your main form, in the button handler that pops up the second form, put this code:

    Code:
    // create form
                Form2 f = new Form2();
    
                // show form
                if (DialogResult.OK.Equals(f.ShowDialog()))
                {
                    // get value from form
                    // do whatever you want with it --> f.AProperty;
                }
    f.AProperty will contain the information that was put there by "form2".

    Does that help?

  5. #5
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: How can a Form window return a parameter ?

    Quote Originally Posted by cilu
    If you want to update controls in the main form from the second form, you can pass a reference to the main form to the second, and use it to update the controls.
    Nooo.. because then that means that the question form depends on the form that asks the question. Youre achieving the OO no no (heh) of high coupling..

    You'd either show the question form as a blocking modal dialog of the first form, or you'd expose events on the question form that allow the answering of a question to fire an event that the asking form will pick up on. It all depends how you want control flow to go. The simplest thing to understand is the blocking modal dialog approach
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  6. #6
    Join Date
    Feb 2008
    Posts
    10

    Re: How can a Form window return a parameter ?

    Thank you Mike, work just like I wanted

  7. #7
    Join Date
    Mar 2008
    Posts
    72

    Re: How can a Form window return a parameter ?

    Cool, glad I could help.

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