howto emulate events?  | Member | | Join Date: Aug 2009
Posts: 49
| |
I'm writing A.I. for logic game, and I need to know how do I call methods like this: - 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.
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,148
| | | re: howto emulate events?
Its just a function, call it like any other function.
If you have: -
void mybutton_Click(object sender, EventArgs e)
-
{
-
//your code here
-
}
-
You can call that function manually with -
mybutton_Click(mybutton,new EventArgs());
-
From a coding perspective I like to do things something like this: -
void mybutton_Click(object sender, EventArgs e)
-
{
-
MyButtonClick();
-
}
-
-
void MyButtonClick()
-
{
-
//code here
-
}
-
Then I can just call MyButtonClick()
| | Familiar Sight | | Join Date: Jul 2009 Location: Calgary, Alberta, Canada
Posts: 211
| | | re: howto emulate events?
For the message box thing, I'd recommend making your own class to handle showing a message box. Something like... - 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 :) http://www.devarticles.com/c/a/C-Sha...ts-in-C-sharp/ |  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,743
| | | re: howto emulate events? Quote:
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.
|  | Member | | Join Date: Aug 2009
Posts: 49
| | | re: howto emulate events? Quote:
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: - 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?
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,148
| | | re: howto emulate events?
MessageBox.Show() is a blocking call
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,743
| | | re: howto emulate events? Quote:
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.
|  | Member | | Join Date: Aug 2009
Posts: 49
| | | re: howto emulate events? Quote:
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...
| | Familiar Sight | | Join Date: Jul 2009 Location: Calgary, Alberta, Canada
Posts: 211
| | | re: howto emulate events?
Use the ShowDialog method instead of the Show method when you make it appear.
|  | Member | | Join Date: Aug 2009
Posts: 49
| | | re: howto emulate events? Quote:
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? - 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();
-
}
-
}
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,743
| | | re: howto emulate events?
Internally this.close()
Externally call the method that has "this.close()"
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,743
| | | re: howto emulate events? - 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
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,743
| | | re: howto emulate events? Quote: - 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 - public void ShowMessage(string msg, ref PlayerStatus ps, ref GUI gui, int AutoTimeOut)
-
{
-
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.
-
}
-
}
|  | Member | | Join Date: Aug 2009
Posts: 49
| | | re: howto emulate events? Quote:
Originally Posted by tlhintoq - 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: - 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();
-
}
-
}
- public void MyClose()
-
{
-
this.DialogResult = DialogResult.OK;
-
this.Close();
-
}
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,148
| | | re: howto emulate events?
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.
|  | Member | | Join Date: Aug 2009
Posts: 49
| | | re: howto emulate events? Quote:
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...
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,148
| | | re: howto emulate events?
Well you are stuck putting a timer event in your custom messagebox code.
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,743
| | | re: howto emulate events? Quote:
Originally Posted by EntryTeam Nope, does not work: - 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();
-
}
-
}
- 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 CustomMessageBox.
Not inside the other class that calls the CustomMessageBox.
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 CustomMessageBox 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. Quote:
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 CustomMessageBox 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.
|  | Member | | Join Date: Aug 2009
Posts: 49
| | | re: howto emulate events? Quote:
Originally Posted by Plater Well you are stuck putting a timer event in your custom messagebox code. Quote:
Originally Posted by tlhintoq You will need to rework your code slightly so that you pass the string criteria as part of the CustomMessageBox parameters when you make the box. Correct me if I'm wrong: - public MyMessageBox(string msg, string name)
-
{
-
InitializeComponent();
-
richTextBox1.Text = msg;
-
-
this.ShowDialog();
-
-
if(name=="CPU") {
-
GUI.Wait(2000); // ms
-
this.Close();
-
}
-
}
- public void ShowMessage(string msg, ref PlayerStatus ps, ref GUI gui)
-
{
-
MyMessageBox CustomMsgBox = new MyMessageBox(msg, ps.PlayerName);
-
CustomMsgBox.Dispose();
-
}
No effect
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,148
| | | re: howto emulate events?
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.
|  | Member | | Join Date: Aug 2009
Posts: 49
| | | re: howto emulate events? Quote:
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) - 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;
-
}
- 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...
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,743
| | | re: howto emulate events? Quote:
// 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.ToLower() =="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?
|  | Member | | Join Date: Aug 2009
Posts: 49
| | | re: howto emulate events? Quote:
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.ToLower() =="cpu") Nice advice. Quote:
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 :(
|  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|