cannot set cancel button to forms? - c#

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.

Related

How to remove focus from a button

I have a panel on which there is a button.But there is a problem. I need to use KeyEventHandler to catch the Enter press, the button is pressing, and the event is not processed. I tried artificially giving focus to Form, but it didn't help.If I don't add buttons, everything works fine. Can you tell me how to solve this?
I created a new project and tried: with the button, the Event is not called, and the button is pressed, without the button, the event is called.
you need to enable KeyPreview property of that form, to catch a KeyEventHandler. Also check that your AcceptButton property has correct value

How to change button click event into enter event?

I'm new on C#, I made a button which makes appear another button and I want to remove the click event of this new one and change it into an enter(key) event.
Code1
Code2
When the button appears it only works when I click it but no when I press Enter.
As to your question, https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.enter?view=net-5.0
The enter event is for when keyboard or mouse enter the control (button in this case).
From the sounds of your question, you are wanting to click the button when the 'Enter' key is pressed.
If this is WinForms, then you might actually want a property on the parent form called 'AcceptButton' (https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.acceptbutton?view=net-5.0). This will make it so anytime the Enter key is hit (with the exception of controls such as a RichTextBox that accepts the return key) that button will be clicked (course you still need the button click event for the button).
Although you seem to be doing something weird in your example code image, so I'm not sure that's the correct solution for it. Instead, you need to look at maybe the Keyboard events, such as KeyPress (https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.keypress?view=net-5.0). There are several: KeyDown, KeyPress, KeyUp, and PreviewKeyDown.
Note these events require Keyboard focus on the control in question. So if add them to the button you are creating in your example code, then that button will need keyboard focus to receive the keyboard events.
Also, just FYI, if a button has keyboard focus, by default the SpaceBar will click that button for you, while the Enter key is generally reserved for clicking the Form's AcceptButton.

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.

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.

How to change Event properties & create new events in C# .NET

I'm using NotifyIcon (System tray icon) in my WinForms application. I also have a ContextMenuStrip assigned to it. When user right clicks on NotifyIcon this ContextMenuStrip pops up.
These are the only events that NotifyIcon has.
Click
DoubleClick
MouseClick
MouseDoubleClick
MouseDown
MouseMove
MouseUp
This contextMenuStrip items (ToolStripMenuItem) are dynamically generated. I mean there are few default items like 'About','Exit','Help' etc.. but other items are dynamically generated and inserted into this menu when user right clicks on it. I'm generating items and inserting into contextMenuStrip in Click event handler
Now, I've two problems:
Problem is for an instant its showing the default menustrip and then my Click event handler executes and new update menu pops up. How can I avoid this? I don't want to see the default menu at all. In other words I need to override the default behavior.
Other problem is since I'm handling the Click event (because I didn't find RightClick event) the left button click also is handled by the same handler. I want to do different things (like show application windows) on left click and show dynamically generated contextMenuStrip on right click. How to acheive this?
Why are there two different events like Click & MouseClick? What else would we click with? Aren't these two interdependent. I mean when ever there is a MouseClick there is also a Click.
If you can point me to some examples. That would be great!
Item #1) The ContextMenuStrip has events that allows you to handle any dynamic creation of menu items before the menu is displayed. See the Opening and Opened events.
Item #2) Use the MouseEventArgs parameter to inspect the mouse-state when event was raised.
Item #3) Depending on the control, Click and MouseClick can be different. Take buttons for instance. When a button has focus, the "Click" event is raised when the user presses the Space or Enter key. In most cases, a MouseClick generates a Click event.
I'm the OP. I guess I've achieved it.
notifyIcon.MouseDown += new MouseEventHandler(notifyIcon_MouseDown);
and
static void notifyIcon_MouseDown(object sender, MouseEventArgs e)
{
NotifyIcon notifyIcon = sender as NotifyIcon;
if (e.Button == MouseButtons.Left)
{
MessageBox.Show("Left Button Clicked"); // & do what ever you want
}
else
{
updateMenuItems(notifyIcon.ContextMenuStrip);
}
}

Categories

Resources