# Using Combobox/dropdown Box System In Xna

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

## Recommended Posts

Hello chums! So the editor aspect of our game we've been working on for the best part of four years is finally complete! This is a big day for us :D The next target is for us to throw in a few advanced features for the more tech-savvy players to be able to create levels that are more catered towards their needs. With this in mind, we've started working on some advanced features for the editor. Basically, if the user has created a new level, before they can get their hands on the canvas screen and start editing, they have to fill in a few basic fields about the level:

The user first enters the world name, level name, ID and author, with each field using a simple text input and converting the fields to strings.

Other basic things such as a ruler grid, etc, can be toggled on and off with single toggle buttons or sliders.

All of these fields are saved into a temporary get/set class which is used for the XML later if the map is saved, and the process is rather simple.

However, the user needs to be able to specify many other things about the maps in the level, such as the weather, type of background, gravity modifiers, soundtrack, ambience, etc. These last variables in design are basically concrete options that don't change, so having the user have to type "Grassland1" or "FilaBrazilliaTrack01" on every single map just seems silly, especially since it takes so long and also provides errors if there are typing mistakes, and that each level will consist of multiple interlinked maps.

My approach to this would be to create some kind of combobox or dropdown box from the ground up where the user can instead select from a number of predefined selections. So for background type, for example, they could choose from an enum of:

CityScape1,

CityScape2,

CityScape3,

Forest,

Canyon,

Seaworld,

etc...

I'd like to design some kind of combobox that essentially accounts for how many elements are present within that enum, so that if we are to add more options in the actual code, the boxes will account for these new options, and display more or less options depending on the size and type of enum. However, it seems that within C#/XNA you are unable to pass the generic type of enum through parameters, and would have to manually implement every available option into each dropdown box.

I'd like to create some kind of cyclic format that allows the user to cycle through all the available options, and accounts for different enums, so the music dropdown would use all of the elements of the music enum, the background box for the background enum elements, etc, without having to manually create some kind of complicated system which converts the selections into strings and back again each time these fields are modified.

It's also worth noting that I'm not using forms, so everything present in the editor is created in XNA (this is purely for aesthetic purposes, as the editor is animated with little jingles and particle effects to give it a sense of accessibility and fun), so I would be created some kind of object from scratch.

Thanks :D

##### Share on other sites
Enum.GetNames and Enum.Parse are your friends! You're going to have to convert to strings in order to render the enum names for the user anyway. You can either maintain just the enum names in the combobox, or have parallel arrays of names and values so that you don't have to Parse.

Unfortunately C# is pretty limited with using enums in generics, but you should be able to implement some combo boxes pretty easily.

You can use reflection to "bind" fields to particular combo boxes in a reusable manner (FieldInfo.GetValue/SetValue/FieldType or the corresponding PropertyInfo if you're using properties). Edited by Nypyren

##### Share on other sites

Thanks, Nypyren. I'll have a look into it!

EDIT:

So what I essentially have at the moment is this:

http://gph.is/2aoLpxg

It's a simple system. I have a Dropdown class which gives the dropdown its graphic and identifiers, then each dropdown contains a list of DropElements, each containing its index within the Dropdown and some string to signify exactly what it is. I have the selection, etc, all coded, and it works fine. However, when it comes to initialising each Dropdown box with its elements, I'm having to manually enter each string. What I'd like to do is create a loop which checks every value within an enumerated type. I'll show you what I mean as an example.

Here is the current hacked and ugly method for initialising:

        public void InitializeElements(ButtonType inType)
{
switch (buttonType)
{
case ButtonType.Gravity:
DropdownElement e = new DropdownElement(rectangle, "0.3f", 1);
DropdownElement e2 = new DropdownElement(rectangle, "0.4f", 2);
DropdownElement e3 = new DropdownElement(rectangle, "0.5f", 3);
DropdownElement e4 = new DropdownElement(rectangle, "0.6f", 4);

break;
case ButtonType.TileType:
DropdownElement e1 = new DropdownElement(rectangle, "Grassland", 1);
DropdownElement e12 = new DropdownElement(rectangle, "Desert", 2);
DropdownElement e13 = new DropdownElement(rectangle, "Beach", 3);
DropdownElement e14 = new DropdownElement(rectangle, "Jungle", 4);
break;
}
}


I instead want to put this into a loop format, which I understand how to do, etc, but I'd to extract the string names from each element in an enumerated type within a different class. So I also have a Map class, which contains many enums, an example being:

        public enum WorldBackgroundEffect
{
Nothing = 0,
LightRain = 1
}
public WorldBackgroundEffect worldBackgroundEffect = WorldBackgroundEffect.Nothing;

public enum WorldTrackPlaying
{
NoMusic = 0,
Fila1 = 1
}
public WorldTrackPlaying worldTrackPlaying = WorldTrackPlaying.NoMusic;

public enum WorldCameraType
{
AutoCamera = 0,
FixedOnCenter = 1,
FixedOnPlayer = 2,
SmashBrosWithoutZoom = 3,
SmashBrosWithZoom = 4
}
public WorldCameraType worldCameraType = WorldCameraType.AutoCamera;


So is it possible to be able to initialise the camera dropdown, for example, by doing something like:

        public void InitializeElements(ButtonType inType)
{
index = 0;
foreach (something in Map.WorldCameraType)
{
index++;
string inString = something.MakeThisAString();
DropdownElement e = new DropdownElement(rectangle, inString, index);
}
}


Help would be greatly appreciated!! :D

Edited by zuhane

##### Share on other sites

All sorted! Close up the thread if you want. In case anyone's curious the solution was this:

 case ButtonType.TileType:

var values = Enum.GetValues(typeof(Map.WorldTileType));
foreach (var enumString in values)
{
elementsList.Add(new DropdownElement(rectangle, enumString.ToString(), i + 1));
i++;
}

break;
case ButtonType.BGType:
var values2 = Enum.GetValues(typeof(Map.WorldBackgroundType));
foreach (var enumString in values2)
{
elementsList.Add(new DropdownElement(rectangle, enumString.ToString(), i + 1));
i++;
}
break;


1. 1
2. 2
Rutin
19
3. 3
khawk
19
4. 4
5. 5
A4L
11

• 9
• 12
• 16
• 26
• 10
• ### Forum Statistics

• Total Topics
633771
• Total Posts
3013761
×