Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Okay, this is may be very simple. I embedded my XNA project in WinForms through "this tutorial" and it works fine even with XNA 4. Now I have a 3D CoilSpring object and the number of loops for this coil needs to be determined by the user. I'm using a txtBox for this. However when I run the project it only gets the default value of txtBox and I want to update it when the user changes its value.

share|improve this question
Oh golly. Please use the official WinForms sample‌​. It's actually correct, and it really makes things a lot easier. The sample even demonstrates exactly what you are looking for (modifying a control and having the change appear in-game). –  Andrew Russell Jul 23 '12 at 12:27
Andrew Russell: With official sample, he would need to write his logic for updating gamecomponents (or dont use them) and it makes things actually more complex... –  Kikaimaru Jul 23 '12 at 12:39
add comment

3 Answers

You can use databinding. For instance if you have an object with a property NumberOfLoops which determines the number of loops being drawn:

class CoilSpring
    int NumberOfLoops { get; set; }

You can link that property to the value of the textbox doing something like this, for example on the form load event:

textbox.DataBindings.Add("Text", coil, "NumberOfLoops");

This way the value of NumberOfLoops updates automatically whenever you write in the textbox, but not the other way around. If you need changes to happen in both directions, you would need to implement the INotifyPropertyChanged interface on the CoilSpring class (example).

In general, I find this easier than making all the connections manually which requires a lot of boilerplate code to make it work both ways (especially as the number of properties increases), and have been using it all over my editor.

share|improve this answer
+1 that's neat, I didn't know WinForms had databindings, I thought that was just WPF. –  Roy T. Jul 23 '12 at 12:56
I don't want the other way at least for now. I've done this, I created a CoilSpring class with NumberOfLoops as its property and I create "coil" instance of this class, however when I call the number of loops of CoilSpring my game's update, it is zero and changes the value in textBox to zero too. I guess it's doing the wrong way that bounds the value of number of loops (initiates with zero) to the textBox, so the value of textBox gets zero! –  perrakdar Jul 23 '12 at 21:10
Yes the textbox gets the initial value at the time of the binding, but after that, whenever you change the value in the textbox, it should change the value in your object automatically. There's an extra parameter of type DataSourceUpdateMode on the Add method. Try calling it with DataSourceUpdateMode.OnPropertyChanged. –  David Gouveia Jul 23 '12 at 21:49
It still changes to zero. I used textbox.DataBindings.Add("Text", coil, "NumberOfLoops", true, DataSourceUpdateMode.OnPropertyChanged); –  perrakdar Jul 23 '12 at 22:12
add comment

You can do this the 'standard winforms way' by subscribing to the textbox's TextChanged event, this way you get a message every time the text is changed, see this MSDN article.

You can also do this by placing the following code in your game's update loop:

String prevText; //place this variable outside the update method in the class

       //You win!
        //Incorrect, try again
    prevText = textBox.Text;
share|improve this answer
But as far as I know we do not have access to the textBox from game class. BTW, I had used TextChanged event but it only changes the value in WinForm and when I call a function to read the value of the textBox again in game's update, it again assigns the default value for the textBox to the NumberOfLoops –  perrakdar Jul 23 '12 at 17:30
No but your WinForms app has access to the Game class so it can just pass the TextBox to the game class so that it can read from it. Or even better, just pass the string everytime the text of the textbox has changed. So in the event handler in the Form put myGame.MyString = textBox.Text;. –  Roy T. Jul 23 '12 at 21:59
add comment

Okay thank you for your helps. Just wanted to say I changed my method and instead of embedding the XNA in WinForm, I embedded WinForm controls in XNA and used the idea from "This LinK" and also created a picture box to show my XNA project window like what I defined from the tutorial mentioned in my question, and then everything works great. thank anyway!

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.