MessageBox & Dialogs on XNA (C#)

Inbali picture Inbali · Aug 5, 2012 · Viewed 8.4k times · Source

I'm having the following code as part of my XNA game:

    private void gameOver()
    {
        if (!m_IsGameOver)
        {
            string message = String.Format("GAME OVER!!!{0}Your score is: {1}",
                        Environment.NewLine, GameScore);
            if (MessageBox.Show(message, "GameOver", MessageBoxButtons.OK) == DialogResult.OK)
            {
                this.Exit();
            }

            m_IsGameOver = true;
        }
    }

    private void gameWon()
    {
        string message = String.Format("You Won!!!{0}Your score is: {1}",
                    Environment.NewLine, GameScore);
        if (MessageBox.Show(message, "You Won!", MessageBoxButtons.OK) == DialogResult.OK)
        {
            this.Exit();
        }
    }       

For some reason,I received the following errors:

"The name 'MessageBox' does not exist in the current context"  
"The name 'MessageBoxButtons' does not exist in the current context"    
"The name 'DialogResult' does not exist in the current context"  

I'm tring to add "System.Windows..." but it seems like that "System" does not have "windows" in it...

How can I solve this?

Answer

O. R. Mapper picture O. R. Mapper · Aug 5, 2012

It seems that you are trying to use WinForms classes in XNA. However, according to the docs, WinForms is not included in XNA: As can be seen in the MessageBox docs, none of the MessageBox methods has the XNA logo in the first column, which means that none of them is supported in XNA. (See, for contrast, the docs on System.Linq.Enumerable, where all methods have the X-shaped XNA logo next to them).

For in-game GUIs, various solutions such as this one exist; more links are included in this, this and this SO question and this MSDN forum posting contains another list of links.