1
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ What does menu.ShowDialog() do? Shouldn't this logic be in some kind of OnButtonClick in the Form? \$\endgroup\$ – Alex H Aug 14 '15 at 9:29
  • \$\begingroup\$ @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. \$\endgroup\$ – Daaksin Aug 14 '15 at 9:53
0
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Daaksin Aug 14 '15 at 9:56
  • \$\begingroup\$ 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. \$\endgroup\$ – jzx Aug 14 '15 at 9:59
  • \$\begingroup\$ 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? \$\endgroup\$ – Daaksin Aug 14 '15 at 10:01
  • \$\begingroup\$ 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. \$\endgroup\$ – jzx Aug 14 '15 at 10:03
  • \$\begingroup\$ Hang on final question; WinForms as a whole is being abandoned!? \$\endgroup\$ – Daaksin Aug 14 '15 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.