Thread: Event handling between forms

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    36

    Event handling between forms

    Hello,

    I am writing a C# game which consists of a number of forms which pass information between each other. I am having trouble creating an event handler that will enable form1 to do something once form2, which form1 has created, has performed a task.

    For example,

    Code:
    public class form1 : form
    {
      form2 myForm2;
      Forms.Button button = new Forms.Button();
      int x;
    
      private void button_Click(object sender, EventArgs e)
      {
        myForm2 = new form2();
        myForm2.Show();
        //Perform function y;
      }
    }
    form2 consists of a text box in which the user inputs an integar number, and I want to get it and store it as form1's x variable. There is an "OK" button on form2, which I want to use to send this value back to form1, and then close form2. Only once I have stored this value into x do I then want to perform function y.

    I have tried newbie things like while loops, to wait until form2 is closing and then read its value from the text box, but this kind of crashes it. I have also triend sending form1 as an argument when it creates form2, so that x can be set directly from form2. However, this way, function y will be called before the user has had time to put in the value into the text box, which is no acceptable.

    So I need to do some sort of event handling here, so that form1 will wait until form2 has had its OK button pressed, and then form1 can perform function y. But I am a little unsure as to what exactly this would require...can anybody help?

    Thanks!

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Try changing this

    Code:
    private void button_Click(object sender, EventArgs e)
      {
        myForm2 = new form2();
        myForm2.Show();
        //Perform function y;
      }
    to this

    Code:
    private void button_Click(object sender, EventArgs e)
      {
        myForm2 = new form2();
        myForm2.ShowDialog();
        //Perform function y;
      }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. event handling is serialized in MS Visual Studio C++ 2005 ??
    By mynickmynick in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2008, 04:47 AM
  2. Standard Win Forms Event Handling? (& #develop)
    By Zeusbwr in forum C# Programming
    Replies: 1
    Last Post: 04-22-2005, 02:59 PM
  3. Event handling
    By gvector1 in forum C# Programming
    Replies: 4
    Last Post: 07-09-2003, 03:32 AM
  4. Event Handling
    By jaypro78 in forum C++ Programming
    Replies: 1
    Last Post: 12-15-2001, 09:55 AM