Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 4-Apr-10 23:39pm
Rate this: bad
good
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!
  Permalink  
Rate this: bad
good
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()
  Permalink  

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 275
1 Maciej Los 214
2 Sergey Alexandrovich Kryukov 200
3 King Fisher 185
4 OriginalGriff 163
0 OriginalGriff 6,223
1 DamithSL 4,724
2 Maciej Los 4,196
3 Kornfeld Eliyahu Peter 3,894
4 Sergey Alexandrovich Kryukov 3,496


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 5 Apr 2010
Copyright © CodeProject, 1999-2014
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