Sign in to follow this  
cmasupra

[XNA] Override the Close Button, and Only Accept Certain Characters in Text Box

Recommended Posts

cmasupra    145
I've been trying to figure out how to do these 2 things in XNA for my Level Editor, but I can't figure them out.

1) How can I make it so that when the user clicks the red X at the top-right corner of the window, the level editor asks the user if they are sure they want to exit without saving? I have the following code, but it doesn't work. It closes the level editor before showing the dialog box.

[code]protected override void OnExiting(object sender, EventArgs args)
{
if (GUI.HUD.saved)
{
base.OnExiting(sender, args);
}
else // map is not saved
{
bool result = warnMapNotSaved();
if (result == true)
{
base.OnExiting(sender, args);
}
else
{
}
}
}

private bool warnMapNotSaved()
{
state = State.FREEZE;
DialogResult result = System.Windows.Forms.MessageBox.Show("There are unsaved changes to the map. Are you sure you wish to exit?", "Map Not Saved", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
return true;
}
else
{
state = State.PLAY;
return false;
}
}[/code]
I have also tried overriding the EndRun() method, but it ends with the same result.

2) How can I only accept letters (A-Z, a-z), numbers (0-9), and spaces in a text box in a Windows Form? It's a place where users can type in a map name (not file name), but I don't want them typing in bad names that could be hard to deal with in code (such as \r\n).

Share this post


Link to post
Share on other sites
EJH    315
[color="#000000"]For question #1, try putting "base.OnExiting(sender, args[/color][color="#000000"]);" before the "if-else" block. That is probably where the game threads are halted so you can do stuff before Exit(). Or try removing it altogether.

For #2, how are you getting the text?
[/color]

Share this post


Link to post
Share on other sites
cmasupra    145
1) Putting "base.OnExiting(sender, args);" before the if-else didn't fix it. I'm not sure what actually makes the window close when the X is clicked, but I think figuring that out will give the solution.

2) The user is typing the text into a text box in a Windows Form. I am pretty sure that I have to use a MaskedTextBox, but I don't know what to put in the filter for it.

Share this post


Link to post
Share on other sites
EJH    315
Ehhh not sure on #1 then.

For the alpha numeric textbox, just use a standard textbox, select it, go to Events, click KeyPress event, and use this code:

[url="http://www.dotnetspider.com/resources/23851-Textbox-validation-Allows-Only-Alpha-Numeric-s.aspx"]http://www.dotnetspider.com/resources/23851-Textbox-validation-Allows-Only-Alpha-Numeric-s.aspx[/url]

Share this post


Link to post
Share on other sites
cmasupra    145
Awesome! That worked for the text box. It now only accepts letters, numbers, and spaces. It also prevents them from pasting any text into the box. I had to modify it some to get it to work the way I wanted, but that's to be expected. :)

[code]private void mapNameTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if ( !(Char.IsLetter(e.KeyChar) || Char.IsDigit(e.KeyChar) || Char.IsWhiteSpace(e.KeyChar) || (e.KeyChar == (char)Keys.Back)) )
{
e.Handled = true;
}
}[/code]

Now if someone could help me solve #1, overriding the close button, that would be awesome as well!

Share this post


Link to post
Share on other sites
diablos_blade    257
[quote name='cmasupra' timestamp='1311894692' post='4841903']
Now if someone could help me solve #1, overriding the close button, that would be awesome as well!
[/quote]

Have you managed to solve this yet?
If not I'd recommend looking into the [url="http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx"]Form Closing Event[/url].

It's been a while since I used it myself, I haven't set up many Forms recently, but it should be something like this:
(Disclaimer: Mostly done from memory, may need a bit of rejigging.)

[code]
protected override void OnFormClosing(object sender, FormClosingEventArgs args)
{
if ( !saved )
{
switch( MessageBox.Show( "Alert", "Save first?", MessageBoxButtons.YesNoCancel ) )
{
case DialogueResult.Yes:
Save( );
break;

case DialogueResult.No:
//Do nothing
break;

case DialogueResult.Cancel:
args.Cancel = true;
break;
}
}
}
[/code]

Share this post


Link to post
Share on other sites
cmasupra    145
Does that work for just forms, or also for the main window? I am wanting to override the main window's close button.

I can't test it right now because I am on a cruise and don't have my computer with me, but if it works on the main window, I'll try it when I get back home.

Share this post


Link to post
Share on other sites
diablos_blade    257
Ah, yes I believe it is just for forms. I never had to use the Application class when I was making my own tools.

However, why is your Application having Shutdown called? Which is why the Exit event is raised.
If you are calling it explicitly yourself, then either consider asking for the user to save before calling Application.Shutdown (probably the easier option), or instead set Application.Shutdown mode OnMainWindowClose and call Form.Close on your main form if you have one.

If Shutdown is being called because the system is shutting down or logging off then you should respond to the [url="http://msdn.microsoft.com/en-us/library/system.windows.application.sessionending.aspx"]SessionEnding[/url] event instead, which can be canceled and delayed while you ask the user a question.

Share this post


Link to post
Share on other sites
cmasupra    145
Thank you, diablos_blade! I finally figured it out because of you. Your mentioning of an Exit event lead me to a Google search that gave me a solution. I think it is very similar to your solution, but this one shows how to make the main window a form, which I didn't think was possible.

Here is what I did:

1) Add this to the LoadContent() function:
[code]Form f = Form.FromHandle(Window.Handle) as Form;
if (f != null)
{
f.FormClosing += f_FormClosing;
}[/code]

2) Create the function f_FormClosing():
[code]private void f_FormClosing(object sender, FormClosingEventArgs e)
{
if (!GUI.HUD.saved)
{
if (System.Windows.Forms.MessageBox.Show("There are unsaved changes to the map. Are you sure you wish to exit?", "Map Not Saved", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Warning) == DialogResult.No)
{
e.Cancel = true;
}
}
}[/code]

I got this solution from here: [url="http://forums.create.msdn.com/forums/p/44962/268306.aspx#268306"]http://forums.create...306.aspx#268306[/url]

So now both questions are answered, thanks to EJH and diablos_blade!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this