Awkward Behavior of ComboBox by Tab Button in Metro Framework - c#

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);
}

Related

Can't escape from ToolStripTextBox in a particular case

It is hard to describe the issue. Let me illustrate it with a very simple example.
Start from a new solution of winform(.net framework 4.8).
Add a menustrip with a textbox, then a datagridview.
And let's handle KeyDown event of datagridview.
if (e.KeyCode == Keys.F3)
toolStripTextBox1.Focus();
Ok, now we start the program.
Click the datagridview to focus on it.
Click the textbox to change focus.
Press Esc on your keyboard.
You can see that the datagridview gets the focus as expected.
But if you make a little change in step two, the result will be confusing.​ Press F3 instead of clicking the textbox.​ When you press Esc this time, the focus is just lost.​ I tried to print the name and the handle of focused control. It turned out to be the textbox itself.​ Can somebody explain it?
Now I'm quite sure it is some kind of bug. After Jimi's mention of hwndThatLostFocus, I had traced this variable for several hours. Although I failed to locate the exact position where hwndThatLostFocus was changed, something unreasonable was found: when Focus() was called in different cases, the result lost consistency.
Under most circumstances, if Focus() of ToolStripTextBox is called, hwndThatLostFocus of ToolStripTextBox will be set to 0, just like the example in my question. But if you click the datagridview and click the ToolStripTextBox and then click the datagridview agian, this time you call Focus(), hwndThatLostFocus will remain a pointer to the datagridview. In addition, this could be reproduced in .net 6.
Later I will report this to Microsoft. For now, there are three ways to avoid this.
Simulate a mouse click by SetCursorPos and mouse_event in user32.dll.
Use reflection like Jimi's advice.
Override ProcessCmdKey in Form, and take care of Keys.Escape yourself:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Escape && msg.HWnd == toolStripTextBox1.TextBox.Handle)
{
dataGridView1.Focus();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
As I avoid using unnecessary reflect, and Dllimport is also some kind of ugly to me, I prefer the third one.

Switching Focus Between Winform Wizard Panels After Pressing Tab

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);
}

How do you create a CheckedListBox WinForm control that is completely unresponsive to keyboard input?

I tried creating a custom CheckedListBox and overriding the OnKeyUp, OnKeyPress, OnKeyDown, OnPreviewKeyDown methods but I couldn't accomplish what I wanted to accomplish.
Essentially what is happening now is that if the user presses a key while the CheckedListBox has focus, the selected item changes. For example, if the user presses the up or down key, the next/previous item in the CheckedListBox is selected. if the user presses 'A', then the first item that begins with the letter 'A' is selected.
I want the CheckedListBox to behave in such a way that it is completely unresponsive to key presses (but still responsive to mouse clicks).
Can someone please tell me how to accomplish this?
I think it is possible to achieve this by inheriting from CheckedListBox and overriding it's ProcessCmdKey() method to return true. That should ignore all commands.
I'll give it a try in a solution and report back :)
Update:
It works.
Code I used:
using System.Windows.Forms;
public class CheckedListBoxIgnoreKeyboard : CheckedListBox
{
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
return true;
}
}
A rebuild is required for the control to show up in Toolbox.
did you try to use the KeyDown event with this?
e.SuppressKeyPress = true;
seem, on my side to work

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.

c#/winforms: application wide keyboard shortcuts with exception on editable controls

in my c#/winforms application i would like to do something like application wide keyboardshortcuts, which should be triggered anywhere, except if the focus is in a control where the user can edit text, like a textbox.
currently i am overwriting this function to do this.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData);
how can i add an exception to this, that it is not triggered when the user is in an editable control?
thanks!
I see 2 solutions here.
1) in each editable control, handle all keyboard events and in the eventArgs object, set the Handled property to true;
e.Handled = true;
2) before executing the wide keyboard shortcut, look for the control which has the focus, and if it's a TextBox, ignore it. There's probably a method in each Form to tell what Control has the focus.
The second option is cleaner. I don't give code because I don't have Visual Studio open right now, but if you need more specific code you can ask.
PS: here, I did some googling for you: How to Get FOcused Control?

Categories

Resources