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

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.

Related

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.

Winforms control LostFocus event not firing when you click the form background

I have a custom ComboBox which opens a separate control when you click on the arrow of the ComboBox. I would like to call the 'LostFocus' event handler to close the custom control when you lose focus of the ComboBox.
This works fine if you click onto another control such as a text box, however doesn't fire if you click on the background of the form.
I want to mimic the functionality of when you click off and close the dropdown of a normal combobox.
Take a bool variable at Form level and make it true during combobox enter(GotFocus) event. InLeave(LostFocus)event ofcombobox, make itfalse`.
Subscribe to Form MouseClick event and check bool variable in this event. if true , call combobox leave event here.

Creating an inputbox with radiobuttons 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.

Keep a Button control in the pressed state wp7?

How can I keep a Button control in the pressed state in a windows phone 7 app using C#? I want to have many different buttons on the screen, but when I press any of them I want it to stay pressed. Any ideas?
Also, once it is in its pressed state, is there a way to "depress" it manually?
Thanks
If the button properties are as follows:
foreground: white
background:black
Then in the pressing state its properties will be
foreground: black
background:white
If you change the properties of button in click event it can be seen as it is pressed. And making the IsEnabled property of button prevents it from firing the events on buttons.

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