I was hoping that this code below would overwrite it, since I am assigning new stuff. But instead it executes both, selecting all and my message box
private void EventSetter_OnHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.A && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
{
System.Windows.MessageBox.Show("ctrl a");
}
}
please help thanks
If you handle the PreviewKeyDown event for the ListBox, you should be able to mark the event as handled, and the Ctrl+A should be ignored:
private void OnListBoxKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.A && (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
{
e.Handled = true;
}
}
Related
I have a textbox with a OnKeyPress event. In this textbox I wish to input only numbers, and for some specific letters like t or m, I would want to execute a code without that letter being typed in the textbox. Small sample of what I am trying to do:
//OnKeyPressed:
void TextBox1KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.T || e.KeyCode == Keys.M) Button1Click(this, EventArgs.Empty);
}
This unfortunately does not prevent the input of the letter..
Set the SuppressKeyPress property from KeyEventArgs to true, like below:
private void TextBox1KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.T || e.KeyCode == Keys.M)
{
e.SuppressKeyPress = true;
Button1Click(this, EventArgs.Empty);
}
}
You could always run the TryParse on the keyDown event so as to validate as the data gets entered. It saves the user an additional UI interaction.
private void TextBox1KeyDown(object sender, KeyEventArgs e)
{
int i;
string s = string.Empty;
s += (char)e.KeyValue;
if (!(int.TryParse(s, out i)))
{
e.SuppressKeyPress = true;
}
else if(e.KeyCode == Keys.T || e.KeyCode == Keys.M)
{
e.SuppressKeyPress = true;
Button1Click(this, EventArgs.Empty);
}
}
I have a simple XAML form within my WPF/C# app, containing several textboxes. I need to know the name of the control when either TAB or ENTER keys are pressed - but I don't know how to do this.
I have a function that listens for the Enter / Tab keys, but after that - I'm stumped:
public viewSearch()
{
InitializeComponent();
PreviewKeyDown += new KeyEventHandler(HandleEsc);
}
private void HandleEsc(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape) Close();
if (e.Key == Key.Enter) SearchAndDisplay();
if (e.Key == Key.Tab) SearchAndDisplay();
}
private void SearchAndDisplay()
{
MessageBox.Show("THE NAME OF THE CONTROL");
}
Thank you.
If you're looking for the control that triggers the event, you could try something as follows:
(pseudocode, since I currently have no access to Visual Studio and I can't directly check if this is all valid for WPF):
private void HandleEsc(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape) Close();
if (e.Key == Key.Enter ||
e.Key == Key.Tab) SearchAndDisplay(e.OriginalSource)
}
private void SearchAndDisplay(object sender)
{
if(sender is Control)
{
MessageBox.Show(((Control)sender).Name);
}
}
I have an issue with my textbox, i want to select everything from a textbox that is readonly. The text that appears in the textbox is a ouput of another function that checks if our databases has no corruptions. The ouput will appear in the textbox.
So what im trying to do is to select everything from a readonly textbox. So we can save to ouput of the database check.
i've tried this so far:
private void ContentTextBox_TextChanged(object sender, KeyEventArgs e)
{
if (e.Control)
{
MessageBox.Show("Control works");
}
}
But now i have to make the "a" key work and i have to make the combination select the textbox.text
Can someone help me with this?
Thanks in advance
You can add a KeyDown() method to your TextBox, which recognises a user hitting Ctrl + A and then selects all of the text, like:
private void ContentTextBox_KeyDown(object sender, KeyEventArgs e)
{
if(e.Control && e.KeyCode == Keys.A)
{
ContentTextBox.SelectAll();
}
}
You have to code the KeyDown method of your textbox. Something like:
private void ContentTextBox_KeyDown(object sender, KeyEventArgs e)
{
if(e.Control && e.KeyCode == Keys.A)
{
MessageBox.Show("Ctrl + a detected");
}
}
You can use this code:
if (e.Control && e.KeyCode == Keys.A)
{
textBox.Focus();
textBox.SelectionStart = 1; //start
textBox.SelectionLength = 2; //length
textBox.ScrollToCaret();
}
I am trying to use the KeyDown-Event (because I like to use KeyCode) to make a single action happen. For this purpose I am using a bool variable to stop continuous actions.
Can't figure out what's wrong with my code though, and haven't found a comparable problem/solution yet...
There are 2 tabs on my tabcontrol and i want to be able to switch between them using CTRL+TAB.
The switching should happen ONCE on keydown of tab.
bool tabSwitchPossible = true;
void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
tabSwitchPossible = true; //Reset boolean
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (tabSwitchPossible && e.KeyCode == Keys.Tab && e.Modifiers == Keys.Control)
{
tabSwitchPossible = false; //Set boolean to prevent further action
if (mainTabControl.SelectedIndex >= mainTabControl.TabCount - 1)
mainTabControl.SelectedIndex = 0;
else
mainTabControl.SelectedIndex++;
return;
}
}
Is there an automatic KeyUp event fired, even when i don't release the key?!
Thanks, in advance guys...
You don't need to handle Form1_KeyUp and tabSwitchPossible varible, remove it and just copy the following code:
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Modifiers == Keys.Control)
{
if (mainTabControl.SelectedIndex >= mainTabControl.TabCount - 1)
mainTabControl.SelectedIndex = 0;
else
mainTabControl.SelectedIndex++;
}
}
I want to call function when Ctrl+space pushed. I searched more but couldn't find what I want.
You need to add an event handler for KeyDown like: KeyDown="TextBox_KeyDown" on your TextBox.
And then in the event handler:
if (e.Key == Key.Space && e.KeyboardDevice.Modifiers == ModifierKeys.Control)
{
//Do Stuff
}
Use something like this:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space &&
(Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{
// Do what you need here
}
}
This should get you working -
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space && Keyboard.Modifiers == ModifierKeys.Control)
{
}
}
If you want to catch all the keys, whether you have the focus or not, in your class you just need to add in the constructor:
// To capture keyboard
EventManager.RegisterClassHandler(typeof(Window), Keyboard.KeyDownEvent, new System.Windows.Input.KeyEventHandler(keyDown), true);
And add the method: (it's an example, it's not for adapted for what you want)
private void keyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Space)
{
code;
}
else if ((Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) && Keyboard.IsKeyDown(Key.T))
{
code;
}
}