howto emulate events?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • EntryTeam
    New Member
    • Aug 2009
    • 55

    howto emulate events?

    I'm writing A.I. for logic game, and I need to know how do I call methods like this:
    Code:
    public void MyButton_Click(object sender, System.EventArgs e) {}
    ...and what arguments do I pass to them?

    One more: I need event handler for a MessageBox appearance, so I could close it progammably.
    That means, somehow to determine if messagebox has popped up, and close it programmably.

    P.S. I running out of time and I need your's help very bad. I have deadlines, and I guess, I miscalculated my work plan time.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Its just a function, call it like any other function.

    If you have:
    [code=c#]
    void mybutton_Click( object sender, EventArgs e)
    {
    //your code here
    }
    [/code]

    You can call that function manually with
    [code=c#]
    mybutton_Click( mybutton,new EventArgs());
    [/code]

    From a coding perspective I like to do things something like this:
    [code=c#]
    void mybutton_Click( object sender, EventArgs e)
    {
    MyButtonClick() ;
    }

    void MyButtonClick()
    {
    //code here
    }
    [/code]

    Then I can just call MyButtonClick()

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      For the message box thing, I'd recommend making your own class to handle showing a message box. Something like...

      Code:
      public class MyMessageBox
      {
        public static void ShowMessage( ... ) // whatever params you want
        {
          MessageBox.Show(...); // whatever you want here
          // Fire your event here
        }
      }
      A google search should turn up a good amount of information on how to create custom events. Here's one hit that seemed reasonably helpful, but there are many others :)

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        That means, somehow to determine if messagebox has popped up, and close it programmably.
        What do you mean by 'determine *if* a messagebox has opened"? It's your code. It opens if you tell it to open. There isn't a lot of "determine if" about it. Or are you trying to close a messagebox from another program not your own?

        Gary is right about making your own MessageBox class. I would add to his suggestion with this... Include a timer as well as a .Close() method. That way you can give it an external Close() command, as well has have it close itself if nobody responds in 10 seconds.

        Comment

        • EntryTeam
          New Member
          • Aug 2009
          • 55

          #5
          Originally posted by tlhintoq
          What do you mean by 'determine *if* a messagebox has opened"? It's your code. It opens if you tell it to open. There isn't a lot of "determine if" about it. Or are you trying to close a messagebox from another program not your own?

          Gary is right about making your own MessageBox class. I would add to his suggestion with this... Include a timer as well as a .Close() method. That way you can give it an external Close() command, as well has have it close itself if nobody responds in 10 seconds.
          Well, my code consists of 7 classes and around 1630 lines of code.
          Lets say, it a kinda difficult to trace all the call and arguments... doesn't matter, I've already did it, and got myself method ShowMessage().

          Now, there's another issue: I was trying to handle messagebox closing by adding following code to my ShowMessage method - it didn't work:
          Code:
          public void ShowMessage(string msg, ref PlayerStatus ps, ref GUI gui)
          {
              MessageBox.Show(msg); 
                  if(ps.PlayerName=="CPU") {
                      gui.Wait(500); // custom delay method
                      SendKeys.Send("{ESC}"); // doesn't close messagebox, ENTER key too
                  }
          }
          Now, I'm planning to choose another object to the role of message box. The only requirement to that object is - existing close routine or window handler.
          I was thinking about creating Form object each time and close it after, but maybe someone can suggest less heavyweight object? Something like tooltiptext?

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            MessageBox.Show () is a blocking call

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              I was thinking about creating Form object each time and close it after, but maybe someone can suggest less heavyweight object? Something like tooltiptext?
              That's pretty much what we were suggesting. Make a class that inherits from Form. Put a text box on it for the text of the message. Put an [OK] and [Cancel] button on it. Put a timer in it if you want to to close on it's own. Give it a public method for "public void ExternalClose() " that will close the form and be a method you can call from all your other classes.

              Comment

              • EntryTeam
                New Member
                • Aug 2009
                • 55

                #8
                Originally posted by tlhintoq
                That's pretty much what we were suggesting. Make a class that inherits from Form. Put a text box on it for the text of the message. Put an [OK] and [Cancel] button on it. Put a timer in it if you want to to close on it's own. Give it a public method for "public void ExternalClose() " that will close the form and be a method you can call from all your other classes.
                ok, did it. thanks. Now trying to block somehow mainform while MessageBoxForm active...

                Comment

                • GaryTexmo
                  Recognized Expert Top Contributor
                  • Jul 2009
                  • 1501

                  #9
                  Use the ShowDialog method instead of the Show method when you make it appear.

                  Comment

                  • EntryTeam
                    New Member
                    • Aug 2009
                    • 55

                    #10
                    Originally posted by GaryTexmo
                    Use the ShowDialog method instead of the Show method when you make it appear.
                    Ok, it's almost perfect. Is there any way to force this kind of form close?
                    Code:
                    public void ShowMessage(string msg, ref PlayerStatus ps, ref GUI gui)
                    {
                    	MyMessageBox CustomMsgBox = new MyMessageBox(msg); 
                    	
                    	CustomMsgBox.ShowDialog(); 
                    	if(ps.PlayerName=="CPU") {
                    		gui.Wait(2000); 
                    
                    		CustomMsgBox.DialogResult = DialogResult.Cancel; 
                    		// cannot close CustomMsgBox form... what's wrong? 
                    		CustomMsgBox.Dispose(); 
                    	}
                    }

                    Comment

                    • tlhintoq
                      Recognized Expert Specialist
                      • Mar 2008
                      • 3532

                      #11
                      Internally this.close()
                      Externally call the method that has "this.close ()"

                      Comment

                      • tlhintoq
                        Recognized Expert Specialist
                        • Mar 2008
                        • 3532

                        #12
                        Code:
                        CustomMsgBox.DialogResult = DialogResult.Cancel; 
                                // cannot close CustomMsgBox form... what's wrong? 
                                CustomMsgBox.Dispose();
                        You aren't going to tell it from the outside what it's dialog result is.

                        You need a method inside the form that closes and returns a given result.

                        You can then call that method from the outside

                        Comment

                        • tlhintoq
                          Recognized Expert Specialist
                          • Mar 2008
                          • 3532

                          #13
                          Code:
                          public void ShowMessage(string msg, ref PlayerStatus ps, ref GUI gui)
                          {
                              MyMessageBox CustomMsgBox = new MyMessageBox(msg); 
                           
                              CustomMsgBox.ShowDialog(); 
                              if(ps.PlayerName=="CPU") {
                                  gui.Wait(2000); 
                           
                                  CustomMsgBox.DialogResult = DialogResult.Cancel; 
                                  // cannot close CustomMsgBox form... what's wrong? 
                                  CustomMsgBox.Dispose(); 
                              }
                          }
                          If you want a dialog result from this, you need to get it as part of the ShowDialog()

                          NOTE the additional parameter to send to your custom dialog telling it how long to wait before autoclosing

                          Code:
                          public void ShowMessage(string msg, ref PlayerStatus ps, ref GUI gui,[B][I] int AutoTimeOut)[/I][/B]
                          {
                              MyMessageBox CustomMsgBox = new MyMessageBox(msg); 
                             
                              DialogResult TheResult = CustomMsgBox.ShowDialog(); 
                              // You are stuck here.  Stopped. That's it.  Until the dialog closes
                              // That's why you need to send a time handled by the Dialog's own timer
                              // If a human doesn't close the dialog, it needs to close itself and return
                              // The desired result such as cancel.
                              }
                          }

                          Comment

                          • EntryTeam
                            New Member
                            • Aug 2009
                            • 55

                            #14
                            Originally posted by tlhintoq
                            Code:
                            CustomMsgBox.DialogResult = DialogResult.Cancel; 
                                    // cannot close CustomMsgBox form... what's wrong? 
                                    CustomMsgBox.Dispose();
                            You aren't going to tell it from the outside what it's dialog result is.

                            You need a method inside the form that closes and returns a given result.

                            You can then call that method from the outside
                            Nope, does not work:
                            Code:
                            public void ShowMessage(string msg, ref PlayerStatus ps, ref GUI gui)
                            {
                            
                            	MyMessageBox CustomMsgBox = new MyMessageBox(msg); 
                            
                            	CustomMsgBox.ShowDialog(); 
                            	if(ps.PlayerName=="CPU") {
                            	
                            		gui.Wait(2000); 
                            		
                            		CustomMsgBox.MyClose(); 
                            		CustomMsgBox.Dispose(); 
                            	}
                            }
                            Code:
                            public void MyClose()
                            {
                            	this.DialogResult = DialogResult.OK; 
                            	this.Close(); 
                            }

                            Comment

                            • Plater
                              Recognized Expert Expert
                              • Apr 2007
                              • 7872

                              #15
                              hehe.

                              Ok, if you're going to use ShowDialog() (which blocks), then your code for closing the box on a time interval has to be done either prior to opening it, or inside the class itself.

                              Otherwise, use the .Show() call and the program will continue to flow. Where you can use your wait() call then use the MyClose() function.

                              Comment

                              Working...