Creating an inputbox with radiobuttons c# - c#

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!

You would have implement your own form. Assuming you're using a recent version of Visual Studio:
Click Project > Add Windows Form.
Set the FormBorderStyle property of the Form control to FixedDialog to prevent
the user from resizing the form.
Set the MinimizeBox and MaximizeBox properties to false.
Set the StartPosition property to CenterScreen or CenterParent.
Add a button to the form with the text OK and set the DialogResult
property to OK.
Add a button to the form with the text Cancel and set the DialogResult
property to Cancel.
Change the AcceptButton property of the form to reference the OK button.
Change the CancelButton property of the form to reference the Cancel button.
Add a TextBox and a RadioButton or CheckBox control to the form.
Press the F7 key to open the code view for the form.
Implement a property of type string that returns the Text property of
the TextBox control.
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 :)
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.

Related

How do I make a form TopMost from a different existing form?

I have a navigation menu to select the settings for the main form. How would I go about making the whole application TopMost from a different existing form.
Picture 1 shows the settings form
How would I be able to change TopMost from the Menu Settings section
I am using panels to display the change between forms.
(Assuming this is WinForms) I think I'd do it by bind to a setting.
On the form you want to be topmost open the form in the designer, click the form background so the Props grid shows props for the form, then expand Application Settings and click the 3 dots next to Property Binding
Drop down next to TopMost and choose New
Call it IsTopMost ...
Then in some code (anywhere, in any form, anywhere.. Such as this button click handler..) change Properties.Settings.Default.IsTopMost
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.IsTopMost = !Properties.Settings.Default.IsTopMost;
}
If you run this, and then click that button on form2, form 1 will pop to top and be topmost. If you click it again, it will toggle off. If you have some settings form2, and you bind a checkbox to this value, by the same process, then when you click the checkbox on form2, form1's TopMostness will follow the check state of the checkbox on form2.
Regardless how you change that bool in Properties.Settings.Default.IsTopMost, it will influence Form1's TopMostness

cannot set cancel button to forms?

I'm learning on programming with C# windows forms. Then I discover cancelButton property. I try to set this property to my form so that when I hit ESC it would close my form. But when I double click cancelButton in my form's property, there nothing happen except VS marks my Form1.cs as unsaved. No method created after the double click. I tried to create private void cancelButon(object sender, EventArgs e){} but the dropdown box where I select method for cancelButton refuses to show my method. The same thing happens to AcceptButton. I have tried to create a brand new project, but it would not help.
Is that VS's bug, or am I missing something?
You've misunderstood what the form's CancelButton and AcceptButton properties are for...
To make the form's CancelButton property work you first have to add a button to the form. Then you select that button from the drop-down list next to the form's CancelButton property.
What this does is to automatically click the Cancel button when the user presses the Escape key.
Similar logic applies to the form's AcceptButton property, except that it will cause the associated button to be clicked when the user presses the Enter key.
Having done that, you STILL HAVE TO ADD A HANDLER FOR THE BUTTON CLICKS.
To do that, double-click on the button in the form (displayed in the designer) - that is what will automatically add the handler for you.
To summarise:
Form.CancelButton -> Determines which button will be clicked when user presses Escape.
Form.AcceptButton -> Determines which button will be clicked when user
presses Enter.
To add a handler for a button, double click the button in the designer.

On windows forms, set tab focus to a button

I have created an Outlook add in which at some point displays a windows form with four buttons present on it. I am trying to default the focus to the first button, however the visual "selected" border will not appear around the button whenever I default this button as the focused one on start.
Any ideas how I could achieve this?
You can use either of these options to set the focus on a control in Load event of the form:
this.ActiveControl = this.button1;
this.button1.Select();
this.Show(); this.button1.Focus();.
You can use the Control.Focus method in the Load event of the form to set the focus on a control only after the Visible property of the form is set to true.
After selection the button, the border of the button will be drawn in a way that shows it's the active control, but the focus cues will not be drawn.
As a quick and dirty fix, you can send a Tab, and a Shift + Tab to your form:
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("+{TAB}");
If you are interested to change the standard behavior of Button to see focus cues when you select button in code or using mouse, you can create your own button inheriting Button and override its ShowFocusCues to return Focused value. You can read more about it here:
public class MyCustomButton : Button
{
protected override bool ShowFocusCues
{
get { return this.Focused; }
}
}

Visual C# Designating a Windows Forms LABEL as the Accept Button

I try to Designate the Accept Button property from my form to the click event of a label. If I would have used a button this wouldn't have been a problem but I have chosen to use a label for UI reasons.
Unfortunaly the Accept Button property only accept a button, so I have already tried to place a button on my form, make this button the acceptbutton and call the label_Click method when the button click_event is fired.
This worked like a charm but when I made the visibility property of my button false or made my button Hide() after the InitializeComponent() the Accept Button didn't worked anymore.
How Can the Accept Button property accept a label OR how can I visually hide a button without changing its visibility property?
Edit: a visualisation of what I tried to acomplish.
When the enter is pressed on the form (Accept Button property), I want to invoke the same method when the Play label is clicked.
To make the button "disappear" without hiding it you can change the border to 0, change the background color to the same color as the form and for FlatStyle property choose Flat. You could also use an image for the button background that is the same color as your form.

How can I set up my winform button so that I can either click the button or press enter

I have some buttons on a winform that I would like to have the option of either clicking the button or press enter but I cannot figure out how to do it. Is it even possible?
Set the AcceptButton property on your form to the button you want.
If you want to make a button the default button on a form you should set the property AcceptButton.
In this way, if another control in your form has focus, pressing enter will close the form.
The same process is valid if you want a cancel button (a button connected to the Escape key). This time you set the CancelButton property on the form.

Categories

Resources