Jump to content
  • Advertisement
Sign in to follow this  
cmasupra

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

This topic is 2533 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

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;
}
}

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
Advertisement
[color="#000000"]For question #1, try putting "base.OnExiting(sender, args[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?

Share this post


Link to post
Share on other sites
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
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. :)

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;
}
}


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

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


Have you managed to solve this yet?
If not I'd recommend looking into the Form Closing Event.

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.)


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;
}
}
}

Share this post


Link to post
Share on other sites
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
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 SessionEnding 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
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:
Form f = Form.FromHandle(Window.Handle) as Form;
if (f != null)
{
f.FormClosing += f_FormClosing;
}


2) Create the function f_FormClosing():
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;
}
}
}


I got this solution from here: http://forums.create...306.aspx#268306

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!