Christian Schiedermeier

I am putting some WinForm controls on the Game's window, like this:

protected override void Initialize()

{

// TODO: Add your initialization logic here

TextBox t = new TextBox();

Form f = (Form)Form.FromHandle(this.Window.Handle);

f.Controls.Add(t);

base.Initialize();

}

This works fine for Buttons etc., but entered text does not show up in a TextBox. Not using Keyboard.GetState() does not help. I suppose that the game window consumes all keystrokes and does not pass them to the controls.

Any help



Re: XNA Framework No text input in WinForm controls

Kyle_W

This seems like a really novel approach. I certainly hadn't thought of it before. It would be great if you can get this to work but I don't know if you will be able to or not. One thing that looks like a potential deal-breaker is that the docs for the Form.FromHandle() method say that it returns the control associated with the specified handle, and I haven't seen anything in the docs to indicate that the Game class inherits from the Control class.

However, you say that you got it to work with buttons. By that do you mean that you got the buttons to show up, or did you actually get the buttons working such that you were able to successfully fire an event handler from their click events

When you click on the text box does it appear to receive focus (a blinking cursor shows up)

 





Re: XNA Framework No text input in WinForm controls

Leaf.

You are correct the game form consumes all keystrokes - I tried something similar to get typed character input but it didn't work. I have also used a similar technique to create new forms. I didn't have any text input on those forms but you might well find that you can get text input on those forms. It's not as neat as embedding the controls but you can link the forms together so that if the game window is minimized the other forms are also minimized. I've used this to do simple editor like functionality.

Cheers,
Leaf.






Re: XNA Framework No text input in WinForm controls

Christian Schiedermeier

Thanks, Leaf. I tried as you suggested, and used a separate "floating" form for my GUI:

static void Main(string[] args)

{

using (Form1 form1 = new Form1())

{

form1.Show();

using (Game1 game = new Game1())

{

game.Run();

}

}

}

This works fine, and also text entry works. Interestingly, the game loop seems to dispatch messages for all forms.

Unfortunately, the GUI form cannot be added to the game form as a control (like f.Controls.Add(anotherForm)), so I will have more work to dock the GUI on top of the game form.

Any ideas how to do that I remember Win32 had assignable child windows, where you could put an existing window under the control of a parent window, but I could be wrong.

Btw, all this is for a university course on computer graphics where I need text boxes to enter transformation matrix elements, etc.

Christian





Re: XNA Framework No text input in WinForm controls

Kyle_W

I wonder if you could use the normal XNA keyboard input APIs to capture the key strokes and then possibly use the SendKeys class from the Windows Forms namespace to send the strokes to the textbox



Re: XNA Framework No text input in WinForm controls

Leaf.

You could use the Form.Show(IWin32Window) override. Pass in a reference to the game form so that your new form is owned by the game form. This wont dock the forms but will enable your form to be minimized/restored and closed with the game form.

Personally I quite liked having a separate form, it was more flexible, especially when using two monitors. Also it means that I could make editor components that can be easily added to other games with minimal interference.

If you wanted to you could probably add some code to stick your form to the side of the game form and make it move/resize with it. You'd just have to monitor those events in the game form.

Cheers,
Leaf.





Re: XNA Framework No text input in WinForm controls

Kyle_W

Still it would be cool if there was some way to embed controls onto the game window. If you had a multiplayer online game and wanted to have a text box for chatting with other players, you wouldn't want to put that on a separate form.



Re: XNA Framework No text input in WinForm controls

Christian Schiedermeier

I didn't know the Show(IWin32Window) override. Nice.

So I gave up on delegating keyboard events and on docking and settled for:

static void Main(string[] args)

{

using (Game game = new Game())

{

using (Form1 form1 = new Form1(game))

{

form1.Show((Form)Form.FromHandle(game.Window.Handle));

game.Run();

}

}

}

Passing the game reference to the form makes it easy to apply GUI changes.

As you hinted, I will program the forms against interfaces offered by the games.

Thanks, I'm happy now.

Christian





Re: XNA Framework No text input in WinForm controls

Kyle_W

Well, one last thing I wanted to add with regard to your question about docking. It is possible to host the game window inside of a Windows Form, but it comes with certain trade-offs. Check out this thread:

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=682993&SiteID=1





Re: XNA Framework No text input in WinForm controls

Christian Schiedermeier

Yes, I have seen this. Actually, I have written a MDX-PictureBox some time ago, and wanted to avoid its state management (or event handling) with XNA. So I started with the Game class (not GraphicsDevice) as an application window, and made a splitter for GUI and graphics output this way: I put a panel on it for the GUI part, and an invisible PictureBox that controlled the viewport on the remaining part. This led to 3 issues, however:

1. Designer support for the panel. Got this working.

2. Keyboard input in text boxes. This issue started this thread, unsolved.

3. During interactive resizing, the viewport is reset to the full window by XNA, and can only be readjusted once resizing stops. Looks ugly.

I understand that the recommended way for GUIs is to use GraphicsDevice directly, not using Game. However, as I am writing my programs to teach CG, I want to avoid non-CG code as much as I can. The two-forms approach is preferable because it is minimally invasive: Just adding a form, and an interface to the game.

Christian