Click here to Skip to main content
15,917,642 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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.

Rob
Posted

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!
 
Share this answer
 
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:

C#
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()
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900