Best way "select all" on a *form*? [duplicate] - vb.net

I'm looking for a best way to implement common Windows keyboard shortcuts (for example Ctrl+F, Ctrl+N) in my Windows Forms application in C#.
The application has a main form which hosts many child forms (one at a time). When a user hits Ctrl+F, I'd like to show a custom search form. The search form would depend on the current open child form in the application.
I was thinking of using something like this in the ChildForm_KeyDown event:
if (e.KeyCode == Keys.F && Control.ModifierKeys == Keys.Control)
// Show search form
But this doesn't work. The event doesn't even fire when you press a key. What is the solution?

You probably forgot to set the form's KeyPreview property to True. Overriding the ProcessCmdKey() method is the generic solution:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == (Keys.Control | Keys.F)) {
MessageBox.Show("What the Ctrl+F?");
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}

On your Main form
Set KeyPreview to True
Add KeyDown event handler with the following code
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.N)
{
SearchForm searchForm = new SearchForm();
searchForm.Show();
}
}

The best way is to use menu mnemonics, i.e. to have menu entries in your main form that get assigned the keyboard shortcut you want. Then everything else is handled internally and all you have to do is to implement the appropriate action that gets executed in the Click event handler of that menu entry.

You can even try this example:
public class MDIParent : System.Windows.Forms.Form
{
public bool NextTab()
{
// some code
}
public bool PreviousTab()
{
// some code
}
protected override bool ProcessCmdKey(ref Message message, Keys keys)
{
switch (keys)
{
case Keys.Control | Keys.Tab:
{
NextTab();
return true;
}
case Keys.Control | Keys.Shift | Keys.Tab:
{
PreviousTab();
return true;
}
}
return base.ProcessCmdKey(ref message, keys);
}
}
public class mySecondForm : System.Windows.Forms.Form
{
// some code...
}

If you have a menu then changing ShortcutKeys property of the ToolStripMenuItem should do the trick.
If not, you could create one and set its visible property to false.

From the main Form, you have to:
Be sure you set KeyPreview to true( TRUE by default)
Add MainForm_KeyDown(..) - by which you can set here any shortcuts you want.
Additionally,I have found this on google and I wanted to share this to those who are still searching for answers. (for global)
I think you have to be using user32.dll
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x0312)
{
/* Note that the three lines below are not needed if you only want to register one hotkey.
* The below lines are useful in case you want to register multiple keys, which you can use a switch with the id as argument, or if you want to know which key/modifier was pressed for some particular reason. */
Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF); // The key of the hotkey that was pressed.
KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF); // The modifier of the hotkey that was pressed.
int id = m.WParam.ToInt32(); // The id of the hotkey that was pressed.
MessageBox.Show("Hotkey has been pressed!");
// do something
}
}
Further read this http://www.fluxbytes.com/csharp/how-to-register-a-global-hotkey-for-your-application-in-c/

Hans's answer could be made a little easier for someone new to this, so here is my version.
You do not need to fool with KeyPreview, leave it set to false. To use the code below, just paste it below your form1_load and run with F5 to see it work:
protected override void OnKeyPress(KeyPressEventArgs ex)
{
string xo = ex.KeyChar.ToString();
if (xo == "q") //You pressed "q" key on the keyboard
{
Form2 f2 = new Form2();
f2.Show();
}
}

In WinForm, we can always get the Control Key status by:
bool IsCtrlPressed = (Control.ModifierKeys & Keys.Control) != 0;

The VB.NET version of Hans' answer.
(There's a ProcessCmdKey function template in Visual Studio.)
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If (keyData = (Keys.Control Or Keys.F)) Then
' call your sub here, like
SearchDialog()
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
End Class

Related

MRTK and Grip Press

I'm using MRTK 2.3.0 trying to catch a "Grip Press" event from the Mixed Reality motion controller.
I've setup the Input Action in the MRTK Toolkit in the Hierarchy of Unity. I've also assigned the action to the controller's grip button in the Controller Definitions. I'm using the following code and made sure the Grip variable is assigned to the Grip Press event. Nothing happens... I'm able to catch touchpad and joystick, menu button press, but not Grip? Why?
According to this documentation: https://microsoft.github.io/MixedRealityToolkit-Unity/Documentation/MixedRealityConfigurationGuide.html the Grip should be a "float" as it's designed as single axis (I wonder why, since it's a button and not a trigger...). However, I'm trying to catch the event where I can... not working...
Anyone understand what the heck I'm trying to say here? (sorry, the code below also includes other events that I can catch without a hickup).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Microsoft.MixedReality.Toolkit.Input;
using Microsoft.MixedReality.Toolkit;
public class TestInput : MonoBehaviour, IMixedRealityBaseInputHandler,
IMixedRealityInputHandler<Vector2>, IMixedRealityInputHandler
{
public MixedRealityInputAction Trigger, Grip, Scroll, Swipe, Joystick, DPad, TouchPadClicked, TouchPadTouched, MenuPressed;
public MixedRealityInputAction DPadUp, DPadDown, DPadLeft, DPadRight, JoystickUp, JoystickDown, JoystickLeft, JoystickRight;
private bool TouchpadPressed, MenuButtonPressed, GrabPressed, TouchDialogPadPressed, TouchpadClicked, Thouchpadtouched,
ThumbstickActive, ThumbstickPressed, VRManipulationStarted, ScrubStarted, PlayPause = false;
private void OnEnable()
{
IMixedRealityInputSystem inputSystem;
if (MixedRealityServiceRegistry.TryGetService<IMixedRealityInputSystem>(out inputSystem))
{
inputSystem?.RegisterHandler<IMixedRealityBaseInputHandler>(this);
inputSystem?.RegisterHandler<IMixedRealityInputHandler<Vector2>>(this);
inputSystem?.RegisterHandler<IMixedRealityInputHandler>(this);
inputSystem?.RegisterHandler<IMixedRealityInputHandler<float>>(this);
}
}
private void OnDisable()
{
IMixedRealityInputSystem inputSystem;
if (MixedRealityServiceRegistry.TryGetService<IMixedRealityInputSystem>(out inputSystem))
{
inputSystem?.UnregisterHandler<IMixedRealityBaseInputHandler>(this);
inputSystem?.UnregisterHandler<IMixedRealityInputHandler<Vector2>>(this);
inputSystem?.UnregisterHandler<IMixedRealityInputHandler>(this);
inputSystem?.UnregisterHandler<IMixedRealityInputHandler<float>>(this);
}
}
public void OnInputChanged(InputEventData<Vector2> ed)
{
Debug.Log("InputChanged");
if (ed.MixedRealityInputAction == DPad)
{
Debug.Log("Touched Touchpad at:" + ed.InputData.x.ToString() + "," + ed.InputData.y.ToString());
}
if (ed.MixedRealityInputAction == Joystick)
{
Debug.Log("Touched Joystick at:" + ed.InputData.x.ToString() + "," + ed.InputData.y.ToString());
}
}
public void OnInputChanged(InputEventData ed)
{
if (ed.MixedRealityInputAction == MenuPressed)
{
Debug.Log("Menu button pressed");
//ActionText.text = "Grab pressed";
}
}
public void OnInputChanged(InputEventData<float> ed)
{
Debug.Log("Float Changed");
if (ed.MixedRealityInputAction == Grip)
Debug.Log("Grab Pressed");
}
Ok... I got this..... It's a BUG in MRTK.
I have 18 actions defined (when you have DPad left, up, down etc it goes by fast!).
When I try to assign my MixedRealityInputAction to an InputAction in the Inspector, when I try to select GrabPressed (which is the 18th action), I get
IndexOutOfRangeException: Index was outside the bounds of the array.
Microsoft.MixedReality.Toolkit.Input.Editor.InputActionPropertyDrawer.OnGUI
Therefore the action does not work....
I tried to use another InputAction instead and assign it to the grab button (I used Select in this case) and it works like a charm....
Beware....
Try
Input.GetAxisRaw(ControllerMappingLibrary.AXIS_11) > 0 || Input.GetAxisRaw(ControllerMappingLibrary.AXIS_12) > 0;
AXIS_11 is grip press for the left controller while 12 is for the right controller

Handling VirtualKey in Windows 8 Store Apps with C#

I'm aware of how to handle key events, i.e.
private void Page_KeyUp(object sender, KeyRoutedEventArgs e)
{
switch (e.Key)
{
case Windows.System.VirtualKey.Enter:
// handler for enter key
break;
case Windows.System.VirtualKey.A:
// handler for A key
break;
default:
break;
}
}
But what if I need to discern between lower-case 'a' and upper-case 'A'? Also, what if I want to handle keys like the percent sign '%'?
Got an answer elsewhere. For those that are interested...
public Foo()
{
this.InitializeComponent();
Window.Current.CoreWindow.CharacterReceived += KeyPress;
}
void KeyPress(CoreWindow sender, CharacterReceivedEventArgs args)
{
args.Handled = true;
Debug.WriteLine("KeyPress " + Convert.ToChar(args.KeyCode));
return;
}
Even better, move the Window.Current.CoreWindow.CharacterReceived += KeyPress; into a GotFocus event handler, and add Window.Current.CoreWindow.CharacterReceived -= KeyPress; into a LostFocus event handler.
You can't easily get this information from KeyUp because KeyUp only knows which keys are being pressed, not which letters are being typed. You could check for the shift key being down and you could also try to track caps lock yourself. Better you use TextChanged event.

override DataGridView Shift+Space

In a DataGridView, pressing SHIFT and SPACE will by default select the entire row. The only solution I've found (referenced at vb.net DataGridView - Replace Shortcut Key with typed character) is to turn off the row select feature. While that works, it's not ideal, because I would still like to be able to select the whole row using the row selector (for example, to delete the row), and by changing the SelectionMode property to anything other than RowHeaderSelect I lose that ability. Is there a way to trap just the SHIFT+SPACE combination and replace it with a simple SPACE? It seems like none of the key events even recognize that keystroke when the control's MutiSelect property is set to True and the SelectionMode property is set to RowHeaderSelect, so I can't use those.
ETA: I thought maybe turning off MultiSelect and changing the selection mode to CellSelect, then adding an event handler for the RowHeaderMouseClick event would work...nope.
The best way I figured out how to accomplish this was to inherit from DataGridView and override the ProcessCmdKey method. Then you can intercept the Shift+Space and just send on Space. Just add this class to your project and switch all of your DataGridViews to MyDataGridViews. My solution draws inspiration from this DataGridView keydown event not working in C# SO question (that also explains why Zaggler's solution doesn't work) and this SendKeys in ProcessCmdKey: change Shift-Space to just a Space Bytes.com post. Sorry, but it's in C#.
class MyDataGridView : System.Windows.Forms.DataGridView
{
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
if (keyData == (System.Windows.Forms.Keys.Space | System.Windows.Forms.Keys.Shift))
{
// DataGridView is dumb and will select a row when the user types Shift+Space
// if you have the DGV set so that you can click a row header to select a row (for example, to delete the row)
// this method will intercept Shift+Space and just send on Space so that the DGV properly handles this.
// For example, if I type "ME TYPING IN ALL CAPS" it ends up looking like "METYPINGINALLCAPS".
// Or if I type "Note: I have some OS thing to talk about" it looks like "Note:Ihave some OSthing to talk about"
byte[] keyStates = new byte[255];
UnsafeNativeMethods.GetKeyboardState(keyStates);
byte shiftKeyState = keyStates[16];
keyStates[16] = 0; // turn off the shift key
UnsafeNativeMethods.SetKeyboardState(keyStates);
System.Windows.Forms.SendKeys.SendWait(" ");
keyStates[16] = shiftKeyState; // turn the shift key back on
UnsafeNativeMethods.SetKeyboardState(keyStates);
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
[System.Security.SuppressUnmanagedCodeSecurity]
internal static class UnsafeNativeMethods
{
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern int GetKeyboardState(byte[] keystate);
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
public static extern int SetKeyboardState(byte[] keystate);
}
}
This was my solution:
private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
dataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;
}
private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
dataGridView.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
}
Here this works fine for me....
Private Sub DataGridView1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
'Lets see what keys we have down shall we?'
If My.Computer.Keyboard.ShiftKeyDown And e.KeyCode = Keys.Space Then
DataGridView1.CurrentCell.Selected = False
End If
End Sub
Here's another way..
Private Sub DataGridView1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
'Lets see what keys we have down shall we?'
If My.Computer.Keyboard.ShiftKeyDown And e.KeyCode = Keys.Space Then
'SendKeys.Send(Keys.Space)
DataGridView1.CurrentCell.Selected = False
End If
End Sub
Just experiment with them and I hope something works out for you?

avoid list view selected index changed firing twice

Quite a simple question, when the selected index of a list view is changed, the event fires twice, once for deselection and a second time to select the next item.
I need to use the event when selecting or deselecting at different times however whan deselecting only to reselect a moment later it makes half my ui flash from enabled being on to off and back on again, it also causes a fair bit of code to run so I just need a way of avoiding the deselection firing if it was another item that was clicked and not blank space (for deselection)
Dave R said to use a 100ms timer here : Am I missing something with my ListView selection event handling
which sounds like it would work but seems quite untidy or generally a bad way of doing it.
My only other idea was to use the click event and then find the item at the location? but I'd rather not go to the hassle
thanks in advance!
-EDIT-
I've just thought that the click event would fire first so I could set a flag that skips selection index changed code if the click event happened on an item and then resets the flag after it's been used therefore skipping the deselection? I'll have a look now but again doesnt feel like a very efficient or easy way of doing something that sounds quite simple?
Use the ItemSelectionChanged event instead - the ListViewItemSelectionChangedEventArgs can tell you which item caused it to fire, and whether it's selected or not.
i just tried another solution which is potentially without any delay, it worked for me:
If ListView1.Items(ListView1.FocusedItem.Index).Selected = False Then
'This is the deselected value
MsgBox("Deselected: " & ListView1.Items(ListView1.FocusedItem.Index).SubItems(0).Text)
Else
'This is the new selected value
MsgBox("Selected: " & ListView1.Items(ListView1.FocusedItem.Index).SubItems(0).Text)
End If
The following solution works even with a delay of 1 ms. To be sure it works probably you can choose a higher delay, 10 ms for example, but a delay of 100 ms will make it a bit laggy on selecting "nothing". Here's the C#-Code:
public class FixedListView : ListView
{
private Timer _ItemSelectionChangedTimer = new Timer();
private Timer _SelectedIndexChangedTimer = new Timer();
private ListViewItemSelectionChangedEventArgs _ItemSelectionChangedEventArgs;
private EventArgs _SelectedIndexChangedEventArgs;
public FixedListView()
{
this._ItemSelectionChangedTimer.Interval = 1;
this._SelectedIndexChangedTimer.Interval = 1;
this._ItemSelectionChangedTimer.Tick += (sender, e) =>
{
this.OnItemSelectionChanged(this._ItemSelectionChangedEventArgs);
this._ItemSelectionChangedEventArgs = null;
};
this._SelectedIndexChangedTimer.Tick += (sender, e) =>
{
this.OnSelectedIndexChanged(this._SelectedIndexChangedEventArgs);
this._SelectedIndexChangedEventArgs = null;
};
}
protected override void OnItemSelectionChanged(ListViewItemSelectionChangedEventArgs e)
{
if (this._ItemSelectionChangedTimer.Enabled)
{
this._ItemSelectionChangedTimer.Stop();
base.OnItemSelectionChanged(e);
}
else
{
this._ItemSelectionChangedEventArgs = e;
this._ItemSelectionChangedTimer.Start();
}
}
protected override void OnSelectedIndexChanged(EventArgs e)
{
if (this._SelectedIndexChangedTimer.Enabled)
{
this._SelectedIndexChangedTimer.Stop();
base.OnSelectedIndexChanged(e);
}
else
{
this._SelectedIndexChangedEventArgs = e;
this._SelectedIndexChangedTimer.Start();
}
}
}
And here is the VB-Code:
Public Class FixedListBox
Inherits ListView
Public Sub New()
Me._ItemSelectionChangedTimer.Interval = 1
Me._SelectedIndexChangedTimer.Interval = 1
AddHandler Me._ItemSelectionChangedTimer.Tick, _
Sub(sender, e)
Me.OnItemSelectionChanged(Me._ItemSelectionChangedEventArgs)
Me._ItemSelectionChangedEventArgs = Nothing
End Sub
AddHandler Me._SelectedIndexChangedTimer.Tick, _
Sub(sender, e)
Me.OnSelectedIndexChanged(Me._SelectedIndexChangedEventArgs)
Me._SelectedIndexChangedEventArgs = Nothing
End Sub
End Sub
Private _ItemSelectionChangedTimer As New Timer()
Private _SelectedIndexChangedTimer As New Timer()
Private _ItemSelectionChangedEventArgs As ListViewItemSelectionChangedEventArgs
Private _SelectedIndexChangedEventArgs As EventArgs
Protected Overrides Sub OnItemSelectionChanged(e As ListViewItemSelectionChangedEventArgs)
If Me._ItemSelectionChangedTimer.Enabled Then
Me._ItemSelectionChangedTimer.Stop()
MyBase.OnItemSelectionChanged(e)
Else
Me._ItemSelectionChangedEventArgs = e
Me._ItemSelectionChangedTimer.Start()
End If
End Sub
Protected Overrides Sub OnSelectedIndexChanged(e As EventArgs)
If Me._SelectedIndexChangedTimer.Enabled Then
Me._SelectedIndexChangedTimer.Stop()
MyBase.OnSelectedIndexChanged(e)
Else
Me._SelectedIndexChangedEventArgs = e
Me._SelectedIndexChangedTimer.Start()
End If
End Sub
End Class
You can use the control like a normal ListView but SelectedIndexChanged and ItemSelectionChanged will fire only once.
Have fun...
Just check in the SelectedIndexChanged event whether the focused item is null and exit.
ListView^ item = listView1-> FocusedItem; //get selected item
if (item == nullptr){return;) // this line exits when deselection event fires
String^ data1 = Convert::ToString ( item-> SubItems [0] ); // get your data from columns like so
MessageBox::Show (data1); // display
note that you could grab data under several columns by changing index provided in SubItems
And using timers and delays will just incur overhead especially with large databases causing your application to slow Code in visual C++ .NET but the same theory applies for C# and others
Enjoy!!

How to execute a binding for a metro control

I want to write the contents of a per occasion active TextBox back to the bound property of the ViewModel when the user presses the key combination for save (Ctrl-S).
My Problem with it is, that I'm not able to trigger the execution of the binding so that the bound Text-Property reflects the contents of the TextBox.
-There seems to be no GetBinding-method. Therefore I can not get the Binding and execute it manualy.
-There is no Validate-method such as in WinForms which executes the Binding
-Giving focus to another control from within KeyDown seems not to work, the binding does not execute
How can I achieve this?
Take a look at Aaron's discussion about this in his WiredPrarie blog post : http://www.wiredprairie.us/blog/index.php/archives/1701
I think I understand your question better now. One way around this would be to use a sub-classed textbox with a new property like this from here:
public class BindableTextBox : TextBox
{
public string BindableText
{
get { return (string)GetValue(BindableTextProperty); }
set { SetValue(BindableTextProperty, value); }
}
// Using a DependencyProperty as the backing store for BindableText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BindableTextProperty =
DependencyProperty.Register("BindableText", typeof(string), typeof(BindableTextBox), new PropertyMetadata("", OnBindableTextChanged));
private static void OnBindableTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs)
{
((BindableTextBox)sender).OnBindableTextChanged((string)eventArgs.OldValue, (string)eventArgs.NewValue);
}
public BindableTextBox()
{
TextChanged += BindableTextBox_TextChanged;
}
private void OnBindableTextChanged(string oldValue, string newValue)
{
Text = newValue ? ? string.Empty; // null is not allowed as value!
}
private void BindableTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
BindableText = Text;
}
}
Then bind to the BindableText property.
Solution for command-instances
Here a solution I have found which is relatively leightweight, but also a bit "hackish":
btn.Focus(Windows.UI.Xaml.FocusState.Programmatic);
Dispatcher.ProcessEvent(CoreProcessEventsOption.ProcessAllIfPresent);
btn.Command.Execute(null);
First I give the focus to another control (In my case the button which has the bound command). Then I give the system time to execute the bindings and in the end I raise the command which is bound to the button.
Solution without bound commands
Give the Focus to another control and call the Dispatcher.ProcessEvent(...).
anotherControl.Focus(Windows.UI.Xaml.FocusState.Programmatic);
Dispatcher.ProcessEvent(CoreProcessEventsOption.ProcessAllIfPresent);
// Do your action here, the bound Text-property (or every other bound property) is now ready, binding has been executed
Please see also the solution of BStateham.
It's another way to solve the problem