howto emulate events?

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

    #16
    Originally posted by Plater
    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.
    The problem is, that a criteria (string argument) should be passed after ShowDialog called. Otherwise, CustomForm will close before ShowDialog called -> runtime error.

    Show is not a good desicion, cause it allows performing manual actions on the main form while secondary is up, carrying message...

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #17
      Well you are stuck putting a timer event in your custom messagebox code.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #18
        Originally posted by EntryTeam
        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(); 
        }
        You can't say it didn't work, when you didn't do it as it was suggested.
        The timer has to be inside the CustomMessageBo x.
        Not inside the other class that calls the CustomMessageBo x.
        Go back and re-read the suggestion I make paying special attention where I said "NOTE the new parameter of int for the number of seconds before the CustomMessageBo x closes". You have to tell the box how long to live before auto closing. You cannot close it from the outside if you use ShowDialog and you have already stated that you HAVE to use ShowDialog so people can't play with the mainform while the dialog form is open.

        The problem is, that a criteria (string argument) should be passed after ShowDialog called.
        Not an option. Accept that. As Plater has pointed out ShowDialog is a blocking call. You will need to rework your code slightly so that you pass the string criteria as part of the CustomMessageBo x parameters when you make the box.

        Or you can make the box
        Pass the string
        THEN call its ShowDialog method.


        Stop.

        Breathe.

        Relax.

        You are over thinking it. you are making it harder than it really is.
        Just make the box.
        Give it all the values it needs.
        Then the last step is to ShowDialog.

        Comment

        • EntryTeam
          New Member
          • Aug 2009
          • 55

          #19
          Originally posted by Plater
          Well you are stuck putting a timer event in your custom messagebox code.
          Originally posted by tlhintoq
          You will need to rework your code slightly so that you pass the string criteria as part of the CustomMessageBo x parameters when you make the box.
          Correct me if I'm wrong:
          Code:
          public MyMessageBox(string msg, string name)
          {
          	InitializeComponent();
          	richTextBox1.Text = msg; 
          
          	this.ShowDialog(); 
          
          	if(name=="CPU") {
          		GUI.Wait(2000); // ms
          		this.Close(); 
          	}	
          }
          Code:
          public void ShowMessage(string msg, ref PlayerStatus ps, ref GUI gui)
          {
          	MyMessageBox CustomMsgBox = new MyMessageBox(msg, ps.PlayerName); 
          	CustomMsgBox.Dispose(); 
          }
          No effect

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #20
            Yup still wrong.
            The .ShowDialog() will block regardless of its location.
            Here's a way you could do it:
            INSIDE your custom class's constructor, attached an event handler to its own "Activated" event (or similar event) in that handler you would start the timer with the number of seconds from your constructor interval.
            This way, the timer starts when your form is shown.
            Use an actual timer, not just a wait function. When the timer elapses, close the custom dialog.

            Comment

            • EntryTeam
              New Member
              • Aug 2009
              • 55

              #21
              Originally posted by Plater
              Yup still wrong.
              The .ShowDialog() will block regardless of its location.
              Here's a way you could do it:
              INSIDE your custom class's constructor, attached an event handler to its own "Activated" event (or similar event) in that handler you would start the timer with the number of seconds from your constructor interval.
              This way, the timer starts when your form is shown.
              Use an actual timer, not just a wait function. When the timer elapses, close the custom dialog.
              Can I please have working code example? Here's mine, still not working properly:
              (MyMessageBox class)
              Code:
              private static System.Timers.Timer aTimer;
              private string arg_name; 
              		
              public MyMessageBox(string msg, string name)
              {
              	InitializeComponent();
              	richTextBox1.Text = msg; 
              	arg_name = name; 
              	
              	aTimer = new System.Timers.Timer(2000);
              	aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
              	aTimer.Interval = 2000;
              	// aTimer.Enabled = true;
              			
              	this.ShowDialog(); 
              }
              		
              void Button1Click(object sender, EventArgs e)
              {
              	this.Close(); 
              }
              
              private void OnTimedEvent(object source, ElapsedEventArgs e)
              {
              	this.Close();
              }
              		
              void MyMessageBoxActivated(object sender, EventArgs e)
              {
              	if(arg_name=="CPU") 
              		aTimer.Enabled = true;
              }
              		
              void MyMessageBoxLoad(object sender, EventArgs e)
              {
              	if(arg_name=="CPU")
              		aTimer.Enabled = true;
              }
              		
              void MyMessageBoxShown(object sender, EventArgs e)
              {
              	if(arg_name=="CPU")
              		aTimer.Enabled = true;
              }
              Code:
              public void ShowMessage(string msg, ref PlayerStatus ps, ref GUI gui)
              {
              
              	MyMessageBox CustomMsgBox = new MyMessageBox(msg, ps.PlayerName); 
              	CustomMsgBox.Dispose(); 
              }
              As you can see I've tryied all possible events...

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #22
                // aTimer.Enabled = true;
                If you don't start the timer, then its not going to autoclose

                Suggestion: Always do text comparrisons to lower so you can be sure to get a match regardless of what you you send it. If by chance you were sending a player name of "cpu" or "Cpu" then your comparrison could be failing.

                if(arg_name.ToL ower() =="cpu")

                If you run it just as you have it now but with line 13 working (not commented out) then your dialog should be closing after 2 seconds. Does it?

                Comment

                • EntryTeam
                  New Member
                  • Aug 2009
                  • 55

                  #23
                  Originally posted by tlhintoq
                  If you don't start the timer, then its not going to autoclose

                  Suggestion: Always do text comparrisons to lower so you can be sure to get a match regardless of what you you send it. If by chance you were sending a player name of "cpu" or "Cpu" then your comparrison could be failing.

                  if(arg_name.ToL ower() =="cpu")
                  Nice advice.

                  Originally posted by tlhintoq
                  If you run it just as you have it now but with line 13 working (not commented out) then your dialog should be closing after 2 seconds. Does it?
                  Still not working :(

                  Comment

                  Working...