How to navigate tabcontrol with arrow keys? - c#

I have a tabcontrol with a series of tabs that contain textboxes and other input controls. If I click on the tab headers I can navigate through the tabs using the left and right arrow keys, but if I'm currently in a textbox or other control then I can't, assuming this is as the textbox takes all of the key events. I have tried attaching event handlers to keydown and previewkeydown but they don't get fired.
Is there a way to get the key events through the tabcontrol even when a child control has focus?

You can use the TabControl's KeyDown event to listen for arrow keys.
But this will likely conflict with other controls:
e.g. Moving the cursor left/right in a TextBox.
If all the controls are read-only, then this won't be much of an issue.
But if people are allowed to alter the data, then intercepting the arrow keys will interfere with standard navigation.
private void tabControl1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Left) {
if (tabControl1.SelectedIndex > 0) {
tabControl1.SelectedIndex--;
}
};
if (e.KeyCode == Keys.Right) {
if (tabControl1.SelectedIndex < tabControl1.TabCount - 1) {
tabControl1.SelectedIndex++;
}
};
}
Note: Having done a bit more testing, it works fine with TabSheets containing TextBoxes, but doesn't work if RadioButtons have focus in a TabSheet.

Related

Assign a Keyboard key to a button click WINFORM

I am developing a small application with some buttons and textbox. What I am having problem is assigning a keyboard key (e.g. F3) to a button click.
For example if the user click the button Cash the code I wanted it's executed fine, but I want to make more easier instead clicking the button with mouse, I want the user be able to press the key on keyboard. I used the keydown event, also keypress event of that button, but still nothing.
I tried this keydown event
if (e.KeyCode == Keys.Enter)
{
btncash.PerformClick()
}
But still nothing
Do not use F3 function button it's used by OS for activating search. Enter key is fairs click event on focused control so do not use this also. Implement as suggested below.
In your Main form
Set KeyPreview to True in form load event.
Add KeyDown event handler with the following code
private void Form1_KeyDown(object sender,
KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.H)
{
btncash.PerformClick();
//btncash_Click(null, null);
}
}

Activate a button Process via custom KeyPress

Basically in a C# Windows Form Application I am working on I have 2 buttons I press button1 and button2.
How do I make it when I press 2 custom keys simultaneously(eg. CTRL+L) the program does the steps coded for button1? Keeping in mind that the window might not be active.
I have looked at this: Keypress To Simulate A Button Click in C# but I don't think this would work if the window isn't active, and its also only one button pressed not two.
In Form KeyUp Event and the Properties is KeyPressPreview = true you can achieve this task.
private void Form_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.L && e.Control)
{
yourButton.PerformClick();
e.Handled = true;
}
}
or
if you want in Keypress Event
private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.Control && e.KeyCode == Keys.L)
{
yourButton.PerformClick();
e.Handled = true;
}
}
EDIT:
I realized that the OP want to the keypress Event from his statement "but I don't think this would work if the window isn't active"
You probably must check this
How to set a Windows hook in Visual C# .NET
If you precede any character in the button text with &, and keep UseMnemonic to true,. the alt+character will serve as shortcut key.
For example, in your case change the text of the buttons to button&1 and button&2 respectively. Keep UserMnemonic = true. Now if you press alt+1 then button1 will be pressed and if you press alt+2, button2 will be pressed.
Also 1 and 2 respectively will be underlined.
Hope that helps.

C# prevent default action on a listbox control when the right or left arrow keys are pressed

I have a listbox control in a windows app and I want to disable the default right and left arrow keydown event triggers. Currently when you press right or left arrows the selected item travels up and down the listbox. I want to add my own actions.
Try adding an event handler to the ListBox.KeyDown event. If the key pressed is an arrow key, set the Handled flag of the KeyPressEventArgs to true to prevent further processing.
A code example, based on an MSDN Forum post
private void listBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
If (e.KeyCode == Keys.Right || e.KeyCode == Keys.Left)
e.Handled = true;
}
You have to override the ProcessCmdKey method in the listbox control. Create a new class, derive it from listbox, then override the ProcessCmdKey.

How can you create Alt shortcuts in a Windows Forms application?

I'd like to create keyboard shortcuts for some controls in my Windows Forms application.
Example:
Notice the underlined, F E V P B.
I have a label and a textbox control. I'd like to associate that Alt keyboard shortcut to the label and the textbox. So if someone presses Alt + B, focus is given to the associated textbox. Is there a way to create this association?
When the label receives focus from pressing its accelerator key (set using the &), it forwards the focus to the next control in the tab order, since labels are not editable. You need the textbox to be next control in the tab order.
To view and correct the tab order of your form, use the View + Tab Order command in the IDE. Using TabPages or other containers adds a level of nesting to the tab order (e.g., 1.1, 1.2 instead of just 1 and 2), but if the label and textbox are within the same container it shouldn't be too hard to set properly.
Type &File or &Edit and you will get underline. That will automatically bind underlined letters with Alt keyword for shortcut.
EDIT.
You question has modified so I'd like to keep up with my answer. You would like to catch some keys combination (Alt + F) and set a focus to the text box.
You may try this solution using KeyDown event of the main form.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.F)
{
this.textBox1.Focus();
}
}
To achieve this, you have to additionally set KeyPreview property of the form to true.
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.W)
{
btnShowConstructionCdFun();
}
}

How to use keyboard shortcuts to select a portion of waveform instead of mouse drag and drop?

I am making a project where i am drawing waveform of an audio file in C#. Currently I am using mouse drag and drop for a part of waveform selection. But now i want that the waveform should also be selected by using following:
click at a point
press shift and click again on another point.
I dont have much knowledge about keyboard events. So need help in this.
Use the Control.ModifierKeys property to detect whether the Shift key is pressed. Sample code:
private void panel1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
if (Control.ModifierKeys == Keys.Shift) SetSelectionEnd(e.X);
else SetSelectionStart(e.X);
}
}
Implementing the SetSelectionStart/End is up to you.
The KeyUp event won't work for this, because although the KeyEventArgs argument for this event includes a Shift property that indicates whether or not the shift key is currently down, the event is not triggered when you only press the shift key (and not any other key). The KeyPress event is also not triggered just by the shift key.
Fortunately, the PreviewKeyDown event is exactly what you need for this. The PreviewKeyDownEventArgs argument includes a Shift property (also Control for the ctrl key), and the event is fired when you just click the shift button.
Also, PreviewKeyDown is triggered on a Form regardless of whether that form's KeyPreview is set to true or not, which is handy.

Categories

Resources