none
Pass variable to another form

    Question

  • Let's say I have form1 and form2. Form1 and Form2 are open. You click a button on Form2 and it adds 1 to a variable called myNumber on Form1.

    Now the problem is I can't use the new keyword and create a new instance of Form1, because Form1 declares myNumber setting it to 0 on creation of the form. If myNumber gets set to 0 again then all the 1's that were added to that variable were added for nothing....

    How do I pass a variable from Form2 to Form1 without creating a new instance of Form1? 
    Sunday, April 11, 2010 11:41 PM

Answers

  • read this page.

    http://www.akadia.com/services/dotnet_user_controls.html

    you want a Delegate and event.

     

     

                public delegate void SubmitClickedHandler(string message);
                // Declare the event, which is associated with our
                // delegate SubmitClickedHandler(). Add some attributes
                // for the Visual C# control property.
                [Category("Action")]
                [Description("Fires when the Submit button is clicked.")]
                public event SubmitClickedHandler SubmitClicked;
                // Add a protected method called OnSubmitClicked().
                // You may use this in child classes instead of adding
                // event handlers.
                protected virtual void OnSubmitClicked(string message)
                {
                    // If an event has no subscribers registerd, it will
                    // evaluate to null. The test checks that the value is not
                    // null, ensuring that there are subsribers before
                    // calling the event itself.
                    if (SubmitClicked != null)
                    {
                        SubmitClicked(string message);  // Notify Subscribers
                    }
                }
                // Handler for Submit Button. Do some validation before
                // calling the event.
                private void btnSubmit_Click(object sender, System.EventArgs e)
                {
                    if (txtbox.Text.Length == 0)
                    {
                        MessageBox.Show("Please enter your name.");
                    }
                    else
                    {
                        OnSubmitClicked(txtbox.Text);
                    }
                }

     

    if you add this code to form2. than in form1 you do .... 
    
    form2.SubmitClicked += <tab button, tab button> visual studio will add the callback for you.
    void Form1()
    {
    Form form2 = new Form();
    form2.Show();
    form2.SubmitClicked += new form2.SubmitClicked(form2_SubmitClicked);
    }
    
    private string myvaraible;
    void form2_SubmitClicked(string message){
    myvaraible = message; 
    } 
    p.s. this was loosely typed, can contain typo's or might not look simular. just to show the basics, of what you want.
    • Proposed as answer by JohnGrove Monday, April 12, 2010 2:26 AM
    • Marked as answer by Bin-ze Zhao Friday, April 16, 2010 6:24 AM
    Monday, April 12, 2010 1:25 AM
  • See this thread also:

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/03a738df-d59d-47c4-a211-f4dd97b97106

     

    Noam B
    _________________________________________________________

    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Proposed as answer by Noam B Monday, April 12, 2010 11:30 AM
    • Marked as answer by Bin-ze Zhao Friday, April 16, 2010 6:25 AM
    Monday, April 12, 2010 11:30 AM

All replies

  • You can make the variable in Form1 public. When you create Form2, you can pass the instance of Form1 into the the Form2 constructor.  Form2 now "has" Form1 (along with the public variable).

    So in form2 you can write oForm1.myPublicNumber += 1.

     

    When form2 closes, Form1's variable (myPublicNumber) should have the correct value.


    Monday, April 12, 2010 12:28 AM
  • read this page.

    http://www.akadia.com/services/dotnet_user_controls.html

    you want a Delegate and event.

     

     

                public delegate void SubmitClickedHandler(string message);
                // Declare the event, which is associated with our
                // delegate SubmitClickedHandler(). Add some attributes
                // for the Visual C# control property.
                [Category("Action")]
                [Description("Fires when the Submit button is clicked.")]
                public event SubmitClickedHandler SubmitClicked;
                // Add a protected method called OnSubmitClicked().
                // You may use this in child classes instead of adding
                // event handlers.
                protected virtual void OnSubmitClicked(string message)
                {
                    // If an event has no subscribers registerd, it will
                    // evaluate to null. The test checks that the value is not
                    // null, ensuring that there are subsribers before
                    // calling the event itself.
                    if (SubmitClicked != null)
                    {
                        SubmitClicked(string message);  // Notify Subscribers
                    }
                }
                // Handler for Submit Button. Do some validation before
                // calling the event.
                private void btnSubmit_Click(object sender, System.EventArgs e)
                {
                    if (txtbox.Text.Length == 0)
                    {
                        MessageBox.Show("Please enter your name.");
                    }
                    else
                    {
                        OnSubmitClicked(txtbox.Text);
                    }
                }

     

    if you add this code to form2. than in form1 you do .... 
    
    form2.SubmitClicked += <tab button, tab button> visual studio will add the callback for you.
    void Form1()
    {
    Form form2 = new Form();
    form2.Show();
    form2.SubmitClicked += new form2.SubmitClicked(form2_SubmitClicked);
    }
    
    private string myvaraible;
    void form2_SubmitClicked(string message){
    myvaraible = message; 
    } 
    p.s. this was loosely typed, can contain typo's or might not look simular. just to show the basics, of what you want.
    • Proposed as answer by JohnGrove Monday, April 12, 2010 2:26 AM
    • Marked as answer by Bin-ze Zhao Friday, April 16, 2010 6:24 AM
    Monday, April 12, 2010 1:25 AM
  • Please read:

    Passing Values Between Forms

    This discusses parent to child, sibling to sibling. This is one of the most asked questions of beginners, so I hope this answers you thoroughly. Print it out and also use it as a reference.


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Monday, April 12, 2010 2:22 AM
  • A delegate is sort of an object oriented pointer to a method. Look carefully at my example in the link I provided. You are encapsulated invocation and passing it to any subscribers.

    //Let's say form2 writes a message via a click event which reads a textbox. We first need to create an event which will pass the message to any subscribers listening.

    first thing to do to have a fit and trim event is to extend EventArgs.

    //this will hold and pass the message to any subscribing to the event.
    public class MessageEventArgs : EventArgs
    {
        public String Message {get; private set;}
        public MessageEventArgs(String message)
        {
            this.Message = message;
        }
    }

    With me so far? Ok, next is to actually create the event from Form2.

    //This belongs on Form2
    public event EventHandler<MessageEventArgs> GiveMessage;

    //In this example, a button event will read a hypothetical textbox and pass its data
    private void button1_Click(object sender, EventArgs e)
    {
         if (textBox1.Text.Length > 0)
         { 
              //use this for best practice to avoid a race condition
              EventHandler<MessageEventArgs> handler = this.GiveMessage;
              
              //check for null which means if it isn't null we have subscribers who want this message
              if (handler != null)
              {
                   handler(this, new MessageEventArgs(textBox1.Text));               
              }
              this.Close();
         }
    }

    //Now we want Form1 to listen to subscribe to the event so we can get the data that was written from form2.

    private Form2 form2 = new Form2();
    form2.GiveMessage += new EventHandler<MessageEventArgs>(form2_GiveMessage);

    private void form2_GiveMessage(object sender, MessageEventArgs e)
    {
        //this will get the dat from Form2
        MessageBox.Show(e.Message);
    }

    Please let me know if you have any questions or need further information.


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com

    Monday, April 12, 2010 2:31 AM
  • very nice, i never tried this.

    thnx

    Monday, April 12, 2010 8:53 AM
  • See this thread also:

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/03a738df-d59d-47c4-a211-f4dd97b97106

     

    Noam B
    _________________________________________________________

    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    Monday, April 12, 2010 11:30 AM
  • See this thread also:

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/03a738df-d59d-47c4-a211-f4dd97b97106

     

    Noam B
    _________________________________________________________

    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Proposed as answer by Noam B Monday, April 12, 2010 11:30 AM
    • Marked as answer by Bin-ze Zhao Friday, April 16, 2010 6:25 AM
    Monday, April 12, 2010 11:30 AM