XNA and WinForms

A lot of people don’t think it is possible to have winforms items (like buttons and dropdowns) in an XNA application, but I am here to prove them wrong. There is a ridiculously simple method of adding controls to your XNA application INSIDE the XNA window without too much hassle – as long as you are ok with coding up the items by hand.

One downfall is that this won’t work on the Xbox 360 since System.Windows.Forms is not included in the things XNA can render, but these types of UIs aren’t really the kind that would work well on an Xbox anyway.

Here is my method of adding the controls and a screenshot of the app running:

 protected override void Initialize() {
    var list = new ListBox();
    list.Items.Add("Here");
    list.Items.Add("Are");
    list.Items.Add("Some");
    list.Items.Add("Items");

    Control.FromHandle(Window.Handle).Controls.Add(list);
}

ListBox in an XNA window

I hope this method solves a lot of the issues people seem to be having with using these Controls together with XNA.

2 Responses to “XNA and WinForms”

  1. SniperED007 says:

    Very cool, but what about keyboard support? I cant enter any text into a texbox etc..

  2. mikeschuld says:

    This seems to be an issue with how XNA / DirectX handles the input from DirectInput devices. I haven’t gotten a workaround for this one yet and the #xna folks don’t have much to say on it either since it is such a corner case use scenario.

    Seems we only get to use things like radio buttons and dropdowns for now at least.

Leave a Reply

You must be logged in to post a comment.

Do NOT follow this link or you will be banned from the site!