I am making a Windows Form Application.
I have two buttons (Accept and Cancel) and I want to call the click event for one when Enter is pressed, and the click event for the other when Escape is pressed.
This is a code I've tried (I found it here on a similar question) that didn't work.
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode.Equals(Keys.Enter)) {
this.btnOrder.PerformClick();
}
if (e.KeyCode.Equals(Keys.Escape))
{
this.btnCancel.PerformClick();
}
}
I have looked through all the properties and events of the buttons but I cannot find anything Accept or Cancel related. I know that this is probably way too easy but I've learnt of these buttons tonight and I just can't do them.
Can someone tell me what am I doing wrong?
It's a property of the Form:
AcceptButton
CancelButton
In the Form Designer: You add the buttons to the form, select the form, and select the buttons you want as accept and/or cancel button.
Related
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.
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; }
}
}
I am having a strange problem with the .NET TabControl in C# (Visual Studio 2010). Start a Windows Forms Application. Add a tab control and a button. Add two different labels to the two tab pages so you can differentiate them. The purpose of the button is just to act as a next button; subscribe to the its Click event with the code:
tabControl1.SelectTab(1);
Let's assume the user entered something wrong on the first tab, so when they try to go to the second tab we want to send them back, so subscribe to the tab control's SelectedIndexChanged event with the code:
if(tabControl1.SelectedIndex == 1)
{
tabControl1.SelectTab(0);
}
Now run the program and click the button. You will notice that as judged by the highlighted tab at the top, the first tab page is the one that appears to be selected, as you'd expect. However, as judged by the tab page that actually appears in the body of the tab control, it's still the second tab page that shows up! Calls to various controls' Focus(), Update(), and Refresh() functions don't seem to help. What is going on here?
I repro. This is a generic problem with event handlers, you can confuse the stuffing out the native Windows control by jerking the floor mat like that. TreeView is another control that's very prone to this kind of trouble.
There's an elegant and general solution for a problem like this, you can use Control.BeginInvoke() to delay the command. It will execute later after the native control is done with the event generation and all side-effects have been completed. Which solves this problem as well, like this:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) {
if (tabControl1.SelectedIndex == 1) {
this.BeginInvoke(new Action(() => tabControl1.SelectTab(0)));
}
}
I have a windows form with a tool strip on it containing a text box and some buttons (think browser url bar with a go button, back, forward)
I want pressing enter to activate the goButton just as clicking it would, which I believe is what TextBox.AcceptsReturn = false is for.
I don't see anything that seems to fit the bill for "tell me what button on the form is the one that we will activate".
What am I missing?
A Form has a default button, but a specific control does not (out of the box anyway).
In your scenario, I would probably handle invoking the goButton.Click event by monitoring the keys pressed waiting for the Enter key to be pressed.
The easiest way is to set the forms "Accept Button" to the button control you want. This can be done in the designer.
I know this is an Old Question, but for someone who might to to lazy or just a beginner ,
handler might look like too much work ( though it isn't really )
But there is an easier work around for this,
you can make a panel for each of them ( 1 Textbox and 1 Button for Example ) , and set the Defaultbutton for Each panel as you need.
I used this for my site, where I had several Ajax panel , and I Wanted Each to have their own search box on different subjects and work with Enter Button.
Looks like an old question, but I will provide my solution.
private void ChangeDefaultButton()
{
if (this.TextBox.Focused)
{
this.AcceptButton = button;
}
else
{
this.AcceptButton = button1;
}
}
And then add this method to the Focus Events of the text boxes. Like...
private void TextBox_Enter(object sender, EventArgs e)
{
ChangeDefaultButton();
}
And
private void TextBox_Leave(object sender, EventArgs e)
{
ChangeDefaultButton();
}
Is it possible to check for a right-click on a menu item in .NET?
It appears that the framework doesn't expose it as an Event, but I've seen other applications (like Chrome and Firefox) which allow you to bring up a right-click context menu for a menu item. Presumably with a little event-loop magic you can do the same thing in .NET, right?
EDIT: I'm talking about desktop application programming, not ASP.NET.
It is an unnatural act. Menus are designed to automatically pop down when they lose the focus. The context menu will take the focus, end of menu. MenuStrip will fight you tooth and nail, I haven't seen it done.
In Winforms I'm unsure, I don't think so, the Click event is a generic EventHandler
In WPF, You can, the OnClick Event passes in a System.Windows.Input.MouseEventArgs object, which has properties on it like .MiddleButton .RightButton
For ASP.NET You need to use javascript to catch the right-click and submit a secret form, initiate a post-back, or use AJAX.
This isn't exactly what was asked for but is a decent compromise and isn't too big a stretch from the UI standards viewpoint.
WinForms menus don't seem to even respond to right clicks so use the presence/absence of the shift key being pressed instead.
private void MenuClick(object sender, EventArgs args)
{
if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift) DoSpecialStuff();
else DoNormalStuff();
}
In Click event you can detect right mouse click with:
Control.MouseButtons == MouseButtons.Right;
Though you might want to check this also in Closing event so to prevent Close event from being raised.