# Strange anomalies when using WinForms with XNA

I haven't seen any questions related to my issue, and this leads me to believe I'm missing something minor, however;

The Problem

I'm creating a game that is effectively launched from a windows form. Consider the following code.

static void Main(string[] args) {
main = new TestGame(); // The Game class.
menu = new Menu(main); // The form.

while (true) {
if (menu.ShowDialog() == DialogResult.OK) {
if (menu.ShouldStartGame) {
main.RunGame( /* Some params */ );
} else {
ExitGame();
break;
}
}
}
}


This loop works fine. My issue lies in the form itself. Firstly, DialogResult does not trigger at all. When the result from menu.ShowDialog() is printed, its result is "Cancel" which is not the option set for the button (OK.) As well as this, picture boxes, the form background colour and other such controls do not work properly. And the form appears to have a UI style from Windows versions before or during XP. Is this a known issue or am I missing something?

• What does menu.ShowDialog() do? Shouldn't this logic be in some kind of OnButtonClick in the Form? Aug 14, 2015 at 9:29
• @AlexH It shows the form as a dialog box, or in other words it blocks the thread until the form is closed. This is so we can assign DialogResult values to buttons in order to tell a conditional what we want to do with the data we've entered. Aug 14, 2015 at 9:53

## 1 Answer

The return value of ShowDialog is set by altering the value of Form.DialogResult. To ensure that the correct result is set, override the form's OnClosing invocator or hook its Closing event and set it there.

• I have done something similar to this with a boolean that's set to true if the Start Game button was clicked. I'm just wondering why it is winforms don't work very well in XNA. P.S. Just tried this private void btnStartGame_Click(object sender, EventArgs e) { this.DialogResult = System.Windows.Forms.DialogResult.OK; } This does not work as far as I can tell. Aug 14, 2015 at 9:56
• Correct, that probably won't work. If you then call Close to close the form, it may be set to Cancel anyway. Also, if you press the "X" button on the form's header or press Alt+F4 it will likely be set to Cancel. You may need to use another variable to indicate to your overridden OnClosing to indicate what it should set DialogResult to.
– jzx
Aug 14, 2015 at 9:59
• Ahh that makes sense, so really the only way would be to use a flag system that checks and sets the dialogresult on a closing override? And can you tell me why the winform UI style is ancient when used in XNA? Just because its an outdated system or? Aug 14, 2015 at 10:01
• WinForms is being abandoned in favor of WPF. XNA itself is no longer supported by Microsoft either, only remnants being compatibility notes in MSDN, but has been partially taken over by the MonoGame community.
– jzx
Aug 14, 2015 at 10:03
• Hang on final question; WinForms as a whole is being abandoned!? Aug 14, 2015 at 10:04