Returning the name of a control - c#

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

Related

Allow only specific keys on TextBox

I have a question. Examples that i find is on a "KeyPress" and they are not longer working on WPF
Can you tell me, how to allow only specified keys from keybord to be write on WPF textbox? I know about keyUp and Down functions, but how to define letters that i want to by type into it?
I think it will be easier, if i will post my code and i tell u what i want to do. What to change here?
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//something here to only allow "A" key to be pressed and displeyed into textbox
if (e.Key == Key.A)
{
stoper.Start();
}
}
private void textBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.A)
{
//here i stop the stopwatch to count time of pressing the key
stoper.Stop();
string aS = stoper.ElapsedMilliseconds.ToString();
int aI = Convert.ToInt32(aS);
stoper.Reset();
}
}
You can use PreviewKeyDown and use e.Key to filter out what you need.
Or, in any place of your code you can use Keyboard class:
if (Keyboard.IsKeyDown(Key.E)) { /* your code */ }
UPDATE:
To forbid a key, you need to set event as handled:
if (e.Key == Key.E)
{
e.Handled = true;
MessageBox.Show($"{e.Key.ToString()} is forbidden");
}
That thing works pretty fine to me (thanks for #JohnyL):
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//something here to only allow "A" key to be pressed and displeyed into textbox
if (e.Key == Key.A)
{
stoper.Start();
}
else
e.Handled = true;
}
private void textBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.A)
{
//here i stop the stopwatch to count time of pressing the key
stoper.Stop();
string aS = stoper.ElapsedMilliseconds.ToString();
int aI = Convert.ToInt32(aS);
stoper.Reset();
}
}

Detecting Enter key press on a button c#

This piece of code detect most keys and puts it in a messageBox, but one of the keys it doesn't is the enter key. *Note it is a key_down method
MessageBox.Show(e.KeyData.ToString());
so far I have tried so many methods to fix and searched a lot for an answer
I have tried this
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("this is it");
e.IsInputKey = true;
}
}
it says 0 references which I know I'm not calling the method but where do I call it?
where ever I do call it, it gives an error.
you delete this line
e.IsInputKey = true;
private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("this is it");
//e.IsInputKey = true;
}
}
or
private void button1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("this is it");
//e.IsInputKey = true;
}
}
In your Form1 Design Properties:
AcceptButton Property set dropdown to button1 or the name of your Button
KeyUp Event of your Button
private void button1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter key has been pressed");
}
}
or
private void button1_KeyUp(object sender, KeyEventArgs e)
{
button1_KeyDown(sender, e);
}
private void button1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter key has been pressed");
}
}
Put this line soon after: InitializeComponents(); -->
button1.PreviewKeyDown +=new PreviewKeyDownEventHandler(button1_PreviewKeyDown);
.
The event you're trying to use will only fire when you focus on that button. The focus is passed in 2 different ways ( via user interaction ) one of which is an actual mouse click on that element and another is "tabbing" on to the control, and make sure you read about "Events" and "EventsHandlers"

How to overwrite ctrl + a select all from list box?

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

Detect Enter Key C#

I have the following code which does not show the MessageBox when enter/return is pressed.
For any other key(i.e. letters/numbers) the MessageBox shows False.
private void cbServer_TextChanged(object sender, EventArgs e)
{
if (enterPressed)
{
MessageBox.Show("Enter pressed");
}
else
MessageBox.Show("False");
}
private void cbServer_Keydown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
{
enterPressed = true;
MessageBox.Show("Enter presssed: " + enterPressed);
}
else
enterPressed = false;
}
Any ideas?
EDIT: Above code, I thought the issue was with the _Keydown even so I only posted that.
in your form designer class (formname.designer.cs) add this :
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Login_KeyPress);
and add this code to backbone code (formname.cs):
void Login_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
MessageBox.Show("ENTER has been pressed!");
else if (e.KeyChar == (char)27)
this.Close();
}
This is because when you press Enter TextChanged event won't fire.
private void textBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
MessageBox.Show("Enter key pressed");
}
else if (e.Key == Key.Space)
{
MessageBox.Show("Space key pressed");
}
}
Use PreviewKeyDown event to detect any key before shown in textbox or input
void cbServer_PreviewKeyDown(object sender, System.Windows.Forms.PreviewKeyDownEventArgs e)
{
// Check if User Presses Enter
if (e.KeyCode == System.Windows.Forms.Keys.Return)
{
// Here: user pressed ENTER key
}
else
{
// Here: user did not press ENTER key
}
}

How to know when ctrl+space pushed?

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

Categories