Creating an inputbox with radiobuttons c#

2019-09-01 08:46发布

问题:

I'm currently making a simple C# game for a college assignment, wondering if it's possible to have radio or checkbox (or something similar) inputs upon loading, in an external window. I'm currently using this to get a username:

     public Form1()
    {
        InitializeComponent();
        Initialize();
        GlobalVar.Username = Microsoft.VisualBasic.Interaction.InputBox("Welcome to EasiGame, Please enter your username.", "Welcome", "Player1", -1, -1);
        label4.Text = GlobalVar.Username;            
    }

This references VB, and that works great, however could I include radio buttons or something in this box, or a separate box, to grab user input for a difficulty setting. Thanks in advance!

回答1:

You would have implement your own form. Assuming you're using a recent version of Visual Studio:

  1. Click Project > Add Windows Form.
  2. Set the FormBorderStyle property of the Form control to FixedDialog to prevent the user from resizing the form.
  3. Set the MinimizeBox and MaximizeBox properties to false.
  4. Set the StartPosition property to CenterScreen or CenterParent.
  5. Add a button to the form with the text OK and set the DialogResult property to OK.
  6. Add a button to the form with the text Cancel and set the DialogResult property to Cancel.
  7. Change the AcceptButton property of the form to reference the OK button.
  8. Change the CancelButton property of the form to reference the Cancel button.
  9. Add a TextBox and a RadioButton or CheckBox control to the form.
  10. Press the F7 key to open the code view for the form.
  11. Implement a property of type string that returns the Text property of the TextBox control.
  12. You'll also need to come up with a way of getting the difficulty from the form; I'll let you figure this part out for yourself :)
  13. To show the form, create an instance and call the ShowDialog() method. This will block the calling method until the user has clicked a button. The ShowDialog method will return a DialogResult, which can be used to determine which button was clicked.


标签: c# input