Click here to Skip to main content
11,790,385 members (54,353 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C#

I'm attempting to write code for a game using c# with a windows form interface. I'd like to be able to use a message box or something similar to pop up to ask for a users name. I'd like it to be a message box as it would only need to ask the question once in the game and I don't want it to clog up the rest of the form. Any other suggestions would be welcome and thanks in advance for any help.

Posted 4-Apr-10 22:39pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Unlike VB C# does not have an InputBox (just a MessageBox) so there are two ways you can do it:
1) Create a form which asks for the input you want, and has an OK and a Cancel button.
2) Use the VB one by adding a reference to 'Microsoft.VisualBasic.dll' and use the static method Microsoft.VisualBasic.Interaction.InputBox()

I would go with the first option, as it lets you have more control over what things look like - helpful for a game!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

The way I usually do these things is that I create a form, and add a property to it that would contain the result.

Now, I present the message box as follows:

DialogResult result = frmQuestion.ShowDialog()
if(result == DialogResult.OK) //you can use whichever one you need here; check them out
   //Do stuff
   //retrieve e.g. user name and password
   string uname = frmQuestion.UserName;
   string pwd = frmQuestion.Password;

Of course, since its a full fledged form, you can do some pretty nifty things with it.

If you don't want to lock up the application (non-modal form) then use Show() rather than ShowDialog()

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

  Print Answers RSS
0 OriginalGriff 1,114
1 Maciej Los 825
2 KrunalRohit 686
3 CPallini 611
4 ppolymorphe 610

Advertise | Privacy | Mobile
Web03 | 2.8.1509028.1 | Last Updated 5 Apr 2010
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100