How can I add a shortcut to a certain button in windows forms application?
For an instance I want to assign the combination CTRL+R to my exit button, so when I press it, the code in that particular button executes.
Write an ampersand ("&") in the Text property of the button before a character to have Alt+that key as the shortcut.
E.g.:
myButton.Text = "Press &me";
would make Alt+M the shortcut.
Beware of duplicates and ensure you only assign a shortcut once per form. You can also assign shortcuts to e.g. Label control to make the control next to the label (in the tab order) get the focus, e.g. a text box control.
If you want to have Ctrl instead of Alt take a look at this SO question, as Plue commented. Be aware that Alt+some key is the usual way, so better have a good reason to change the default behavior.
Related
I am coding a C# forms application, and have a question in relation to the text property of a button where a messagebox is shown on the button's click event.
The button is a remove button that shows a messagebox where the user can press yes, no or cancel.
In the above situation, should the text of the button have the "..." characters following the text of the button?
With menuStrip items, and a form is displayed, the convention is to have the "..." characters after the text property of the menuStrip item. When following this convention, should the remove button have the same "..." characters.
One element of creating a good user interface is consistency. I assume that you want to create a user interface that is consistent with similar user interfaces on Windows.
Microsoft has published a user experience guide that contains the a guideline regarding using ellipsis with commands (buttons and menu items):
Indicate a command that needs additional information (including confirmation) by adding an ellipsis at the end of the button label.
So if your command opens a message box which requires further input or opens another dialog box where the user has to provide input you should use the ellipsis. But if the command doesn't open a secondary window, or the purpose of the command is to open a secondary window like an about box or a dialog box with options then no ellipsis should be used.
Microsoft provides a rationale for this design:
Proper use of ellipses is important to indicate that users can make further choices before performing the action, or even cancel the action entirely. The visual cue offered by an ellipsis allows users to explore your software without fear.
Your specific case where confirmation is required is included in the guideline that states that you should add ellipsis. Your users will know that they can invoke the remove button without fear.
I have 3 radio buttons on my WinForm.
I would like to give the user the option to navigate between them also by keyboard.
Is there a way to enable it?
I understand I have to use this code:
if (e.KeyChar==Convert.ToChar(Keys.Down))
But how do I know which single radio button I have to set as checked?
The simplest solution is to use keyboard shortcuts. This entails prefixing one character in each RadioButton's text with the & character.
For example if the text of your radio button is "&Big option" then the user can select this option by pressing the [ALT] and B keys at the same time.
Additionally, once one of the radio buttons has the focus you can navigate between them by using the up and down arrows. In general, the user can navigate between controls by using the [Tab] key. In VS 2010 the tab order may be modified by selecting the View->Tab Order menu item.
The same keyboard shortcut trick works for many other controls. For example if you have a TextBox control preceded by a label control you can prefix a letter in the Label control with &. Now since the Label (by default) can't take the focus, when the user uses the Label's keyboard shortcut, the focus will move to the next control in the Tab Order i.e. the TextBox.
If you want a control to be skipped when using the [Tab] key set its TabStop property to False.
I have a control with several textbox controls on it.
Now, when I press tab after I edit one of the textboxes, the focus is switched to the next textbox BUT I need to press an additional tab in order for it to enter the "editing" stage.
The first tab simply draws a dotted background on the textbox ... and the second one actually puts the cursor position inside the textbox. Is there a way when I press tab, to automatically set the cursor inside the textbox?
Thanks./
Are you sure you don't have any hidden controls or other controls that might be accepting focus before your textbox? Either way, you should be able to update the tabindex value of your controls so they follow a logical sequence when tab is pressed.
In a C# desktop application, I have a customer widget which contains a text box. I also have a menu item on a menu strip that has the Delete key as its short-cut key. The behaviour I'm finding is that pressing delete in the text box, which the user will expect to delete a character, is actually triggering the menu item and deleting the whole object they are working on.
Is there any way to let the text box have "first crack" at handling the key press rather than the menu item?
Thanks.
Are you handling the delete key at the form level? Either way, you could check for the widget having focus and then not handle the event. Even better would be to not use delete as a global shortcut, this seems along the lines of reassigning what ctrl+c, alt+f4, or tab do.
I would change the shortcut key of your 'Delete the whole thing' menu item to Shift-Del or Ctrl-Del...or something that makes it a little more work for the user to delete the whole object.
If that's not an option, you could check the form's ActiveControl in the MenuStrip's delete code and if it's your TextBox, check the Textbox.SelectionStart value to get the position of the cursor in the text and manually delete the character:
if (myTextBox.Text.Length() >0) {
int pos = myTextBox.SelectionStart;
string txt = myTextBox.Text;
if (pos < txt.Length()) {
myTextBox.Text = txt.Substring(0,txt.Substring(0, pos-1) + txt.substring(pos+1);
}
}
What we did
The solution we ended up using was to disable those menu items, thereby disabling their respective short-cut keys, when the control that those menu items were intended to act upon.
This solved the problem where clicking delete in an unrelated text box deletes the selected item in the main widget. However, it introduces the problem that the user has to click on the main widget in order to access those menu items. To counter this a little bit, I did make it such that the main widget regains focus when the panel with the hideable widgets is hidden.
I'm not advocating this solution, just including it for completeness.
What I would have liked
My ultimate solution would be to only perform the action only if:
the main widget has focus, OR
the even was triggered via clicking the menu item
but there doesn't seem to be a way to detect whether the even was triggered by a short-cut key within the framework.
Set KeyPreview to False on the Form.
The Customer Widget might be turning it on, so you may have to disable KeyPreview on the form when the text box gets focus (and re-enable it when it loses focus).
Otherwise, disabling it from the designer should work.
I have a UserControl that consists of three TextBoxes. On a form I can have one or more or my UserControl. I want to implement my own tab behavior so if the user presses Tab in the second TextBox I should only move to the third TextBox if the the second TextBox has anything entered. If nothing is entered in the second TextBox the next control of the form should get focus as per the normal tab behavior. If the user hasn't entered anything in the first or second TextBox and the presses tab there is this special case where a control on the form should be skipped.
By using the ProcessDialogKey I have managed to get it work kind of ok but I still have one problem. My question is if there is a way to detect how a WinForms control got focus since I would also like to know if the my UserControl got focus from a Tab or Shift-Tab and then do my weird stuff but if the user clicks the control I don't want to do anything special.
As a general rule, I would say overriding the standard behavior of the TAB key would be a bad idea. Maybe you can do something like disabling the 3rd text box until a valid entry is made in the 2nd text box.
Now, having said this, I've also broken this rule at the request of the customer. We made the enter key function like the tab key, where the enter key would save the value in a text field, and advance the cursor to the next field.
I don't think there's a built-in way that you could do it. All of the WinForms focus events (GotFocus,LostFocus,Enter,Leave) are called with empty EventArgs parameters, which will not give you any additional information.
Personally, I would disable the third textbox, as Rob Thomas said. If you're determined to do this, though, it wouldn't be difficult to set up a manual (read: hackish) solution. Once the tab key is pressed (if the focus is on the second textbox), set a variable inside your form. If the next object focused is then the third textbox, then you know exactly how it happened.
The reason for this odd tab behavior is all about speed in the input process. It was really good to get some input, I hadn't thought about disabling a textbox but that could actually work. But using the Enter key to accept the input hadn't even crossed my mind. That will work so much better. The user can enter the numbers and then press enter to accept the input and the next possible textbox will be the active one. It's like having the cake and eating it too, The speed factor is there since when using the enter key no unnecessary tabing must be done to get to the correct field and using the enter key next to the numeric keyboard makes it really smooth.
Thanks for the input!
I agree with DannySmurf. Messing with the tab order might give you hell later on if the requirements for the application change.
Another thing that you could do is to implement some kind of wizard for the user to go through.
Better than disabling controls, try monkeying around with TabStop - if this is false, the control will be simply skipped when tabbing.
I'd also suggest that the Changed event of the TextBox is the place to be updating TabStop on the other controls.
I've done something similar to this with a login control, where users could enter either a username or an email address (in separate fields), plus their password, and tabStop is what I used to get the job done.