Switching Focus Between Winform Wizard Panels After Pressing Tab - c#

I wizard that contains two panels. One panel contains a form, and that form contains a textbox. The other panel contains a button. I want to press tab from the textbox, and want to see the button highlighted (or selected) afterward. This is intended to allow the user to press enter to press the button, instead of manually clicking it.
My current process does not allow the button to be selected after pressing tab from the textbox. I have also included a picture.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Tab)
{
if (FORM3.textbox().Focused)
{
//activate parent form
this.Activate();
WizardButton.Focus();
WizardButton.Select();
}
}
return base.ProcessCmdKey(ref msg, keyData);
}

Related

Excluding autocompleting textbox from form accept property?

I have a form in which I've linked the accept property to a button called submit on the form so that whenever enter key is pressed on any control in the form it fires the submit button's click event. I also have an auto completing textbox (to/from) on the form with auto complete mode set to SuggestAppend. I want to move to the next control(textbox amount) when the user presses the enter key. I tried the textbox's keyup event like this:
private void tb_to_from_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Return)
{
tb_amount.Focus();
}
}
This moves the control to the other textbox, however, the submit button is also pressed. How do I exclude tb_to_from from the accept property of the form so that it just moves to the next control?

Awkward Behavior of ComboBox by Tab Button in Metro Framework

Problem: My problem is that when my entry form loads, all the combo boxes work fine but when I click the TAB key to jump from one control to another, e.g. moving control from 'Name' text box to 'Father Name' text box, then my combo boxes show all the items in blue color like they are all selected but they actually are not selected.
Combo boxes work fine but seems awkward and it only happens whenever the TAB key is pressed.
Required solution: I want a solution for how to get rid of it. I mean that my TAB key does not affect the combo box.
I'm using the Metro framework (MetroModernUI 1.4.0) and .NET Framework 4.8 in Visual Studio 2015.
if you want TAB key does not affect the combo box ,use this :
metroComboBox1.TabStop = false;
Thanks to Navcore
Override the ProcessCmdKey method to detect tab press in the form and emulate alt key press programatically.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Tab)
{
SendKeys.Send("%");
}
return base.ProcessCmdKey(ref msg, keyData);
}

MessageBox without a default button

I have a form in my application where users can enter a list of serial numbers, each to be subjected to a quick check and then added to a list (this is for a stock take module). So the users typically use barcode scanners to scan the serial numbers from a pile of stock items.
I'm handling the KeyPress event of the TextBox that has focus while the users are scanning items and look for e.KeyChar == 13 (the enter key). Whenever enter is pressed, I know that I have a complete serial number which I can then validate before adding it to the list.
Here's where my problem occurs; Under certain conditions, I have to prompt the user at this point on whether he really wants the stock item added to the list or not. I'm using a MessageBox for that, like so:
if (MessageBox.Show("This is a special stock item.\r\nDo you want to add it to the list?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
// Add item to list
else
// Do not add item to list
But because this is something that only happens occasionally, the users often don't even see the MessageBox and simply scans the next serial number, which gets lost of course but which ends with an enter key, which fires the default button on the MessageBox and without having intended to do so, and sometimes not even aware that it's happened, the user adds an item to the list and misses a subsequent item.
Is there a way I can prevent the MessageBox from firing any buttons if enter is pressed? I don't mind if the user goes on scanning barcode after barcode and losing them all, as long as the MessageBox remains on screen until he realises his attention is required and deliberately select one of the two options.
Is there a way I can prevent the MessageBox from firing any buttons if
enter is pressed?
No.
I don't mind if the user goes on scanning barcode after barcode and
losing them all, as long as the MessageBox remains on screen until he
realises his attention is required and deliberately select one of the
two options.
Use the YesNoCancel option, and set the default to Button3, which will be the Cancel button. Now keep looping while the result is Cancel. When the loop drops out, the user will have selected either Yes or No:
DialogResult result;
do
{
result = MessageBox.Show("This is a special stock item.\r\nDo you want to add it to the list?", "Confirmation", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3);
} while (result == DialogResult.Cancel);
if (result == DialogResult.Yes)
{
// yes
}
else
{
// no
}
---------- Edit ----------
I'm not overly fond of the idea of having a button on the dialog that
means nothing other than for some obscure application logic (from the
user's perspective).
Agreed...having a "noop" button isn't optimal. The solution above is a quick and dirty "fix".
When you get around to implementing your own custom MessageBox Form, here's an easy way to make it ignore the Enter key whenever a Button is currently focused:
public partial class frmVerify : Form
{
public frmVerify()
{
InitializeComponent();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Enter && this.ActiveControl is Button)
{
return true; // suppress the keystroke
}
return base.ProcessCmdKey(ref msg, keyData);
}
// ... more code ...
}

How to Add shortcut key to checkbox without text

I want to add shortcut key to checkbox. Checkbox do not have text. I have label and then Checkbox. Label have shortcut key for ex. &Visible. So, Label have V as shortcut key. If someone press Alt+V then chechbox should change from selected to not selected state and same in opposite manner.
You can check it like this refer the following code part.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// look for the expected key
if (keyData == Keys.Alt && keyData == Keys.V)
{
checkBox1.Checked = true;
return true;
}
else
{
checkBox1.Checked = false;
return false;
}
}
Label controls are special with respect to keyboard mnemonics. Since labels can't ever get the focus, whenever you attempt to set the focus to a label using its keyboard mnemonic, the label sets the focus to the very next control in the tab order.
This is intended for use with textboxes and comboboxes, which don't have any built-in facility for displaying a label (in contrast to the check box and option button controls). To set up a mnemonic for these controls, you position a label next to them, set a mnemonic for it, and ensure that it comes right before the textbox in the tab order. That way, when the user activates the keyboard mnemonic for the label, it automatically sets focus to the textbox control. You've seen this all over the place in Windows:
Well, you can do exactly the same thing with a checkbox control if you must (though I'm really not sure why you'd want to). Set the mnemonic for the label (&Visible), and then position the label next to the checkbox that you want it to work with. Use the TabIndex configuration options in the Visual Studio IDE to ensure that if the label has tab index n, the checkbox control has tab index n+1.
There's no need to override ProcessCmdKey or anything else difficult.

WinForms - Capture key up before control

I've got a form and am subscribing to the KeyUp event and handling the ALT key, this is working great but only when my form has the focus, if a control on the form has focus then this no longer works; what can I use to capture the keys when controls have focus? I'd rather not have to subscribe to every KeyUp event on every on the form controls...
Thanks.
Will setting the form's KeyPreview property to true work for you?
Try overriding ProcessCmdKey from your form. This will be raised regardless of which controls have focus, so long as the form is active. You will have to do a little more work checking the keydata. Ex:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((keyData & Keys.Alt) == Keys.Alt)
{
Debug.WriteLine("ALT");
}
return base.ProcessCmdKey(ref msg, keyData);
}

Categories

Resources