Click here to Skip to main content
13,139,254 members (89,643 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 28 Aug 2010

XNA Menus

, 28 Aug 2010
Rate this:
Please Sign up or sign in to vote.
How to do menus easily using XNA. Easy to use classes that display menus.


This code allows you to display a menu with unlimited options and results very easily.

Using the Code

There are two classes and an XML file that are key to each new menu you want. The two classes are Menu.cs and MenuItem.cs. To use the classes, you must first create a menu XML file. To create a menu, you must create a folder in the Content part of your game project called Menus. Within that, create an XML file with the following structure:

<?xml version="1.0" encoding="utf-8" ?>

	<MenuName>Main Menu</MenuName>
		<MenuItemText>Option 1</MenuItemText>
		<EventParams>Option 1</EventParams>

This structure requires MenuName as the name you want your menu to be identified by, PositionX and PositionY are the top left x and y coordinates of your menu when it is displayed. You can have as many MenuItem sets as you like. Within a MenuItem must be MenuItemText, the text that is displayed to the user, MenuItemEvent, the name of the method to call when the user selects that item and optionally you can have EventParams, used as extra parameters if you have a method that needs them.

To use the menu, you need to do the above and 5 more things to your code:

  1. Load a menu
  2. Add an EventHandler
  3. Optionally set what your current menu is
  4. Get input from the menu
  5. Draw the menu

To load a menu, you have to get a list of XML files that are in your Menus folder, then create a new menu and load it using the selected file name. My code works on the basis that you can have multiple menus so loads all available in LoadContent() and has a method for selecting one of them. The code for loading looks like the following:

//Get a list of the xml files
string[] Files = Directory.GetFiles(Content.RootDirectory + "\\Menus\\");
//Loop through each one
foreach (string FileName in Files)
	//Create a new menu
	Menu AMenu = new Menu();
	//Tell it load the file
	//Add it to the list
	//If it is the Main Menu set it as the current menu
	if (AMenu.MenuTitle == "Main Menu")
		CurrentMenu = AMenu;
	foreach (MenuItem AnItem in AMenu.MenuItems)
		AnItem.OnConfirmedEvent += new EventHandler(AnItem_OnConfirmedEvent);

My code gets a list of files, loops through them creating a new menu and loading it. It also checks if the menu it has just loaded is the Main Menu and then sets it as the CurrentMenu being displayed. To use a menu, you must also add an event handler to all the menu items' OnConfirmedEvents. Whilst adding them, you can distinguish between what methods you set the events to call, but I chose to set them all to the same method. This method is as follows:

private void AnItem_OnConfirmedEvent(object sender, EventArgs e)
	MenuItem TheItem = (MenuItem)sender;
        switch (TheItem.EventName)
            case "ShowMessageBox":
            case "Quit":

It gets what MenuItem called it, then looks at the method name and if it's equal to ShowMessageBox, then it shows a message box with the first event parameter in it. My menu also has GetInput() and Draw() methods. GetInput() is used in update when you wish to allow the user to select a menu option.

That's it! Not much to it really but it can be surprisingly difficult to do for yourself. It took me a while to get this working.

Points of Interest

I had fun working out a sensible XML file standard that would encompass everything I wanted to do and thus be flexible, it's surprisingly difficult!


  • 28th August, 2010: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ed Nutting
United Kingdom United Kingdom

I am Edward Nutting, born March 1996 and I am currently a Computer Science and Electronic Engineering undergraduate at the University of Bristol, UK. I've been self-teaching programming since I was eight years old and have dabbled in many different areas from web development (where I had my own advertising startup) to low level operating system stuff (my current project Fling OS).

The articles and code on this profile are some of my early work (from when I was aged about 13 to 16) but despite my young age, it has been well received by CodeProject users. My more recent work has been for companies or on projects hosted on GitHub, CodePlex or BitBucket.

I keep an eye on many things techy and am open to any and all ideas, so please feel free to contact me if you think I can help! Smile | :)

Ed Nutting

You may also be interested in...


Comments and Discussions

GeneralXNA Menus Pin
Member 766814413-Feb-11 8:23
memberMember 766814413-Feb-11 8:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 28 Aug 2010
Article Copyright 2010 by Ed Nutting
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid