C# Compare the Key Pressed on the Keyboard - c#

I have a textbox and a button.I'm saving the value(keyboard key) entered in the TextBox.I need to give a message when I press the right keyboard key.
private void btn_Click(object sender, EventArgs e)
{
Properties.Settings.Default.text1 = text1.Text;
Properties.Settings.Default.Save();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == text1.Text) //--------->> error
{
MessageBox.Show("success");
}
}
how can I provide this condition?

maybe easier it will be to use KeysConverter
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
KeysConverter convertor = new KeysConverter();
string keyPressed = convertor.ConvertToString(e.KeyValue);
if (keyPressed == text1.Text)
{
//do stuff
}
}

If you compare with one char text. You can try this.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (txt.Text.Length == 1 && e.KeyValue == (int)txt.Text[0]) //--------->> error
{
MessageBox.Show("success");
}
}

Related

How to run event implementation from other event condition

I'm wondering, if is it possible to run if (e.KeyCode == Keys.Enter) event process without pressing on enter, correctly to say without event condition itself.
The only way I know to get result, which can be useful if implemented, is to locate content of condition in function, but I'm asking if it is possible other way.
From textBox2_TextChanged event condition to get implementation of if (e.KeyCode == Keys.Enter) event of textBox1_KeyDown with entering of exist text and containing process implementation?
For example:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
/// code
if (e.KeyCode == Keys.Enter)
{
label1.Text = ("text inserted");
}
}
and desired result is to call (e.KeyCode == Keys.Enter) implementation with label1.Text = ("text inserted"); from textBox1_KeyDown in textBox2_TextChanged here:
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (x > 0)
{
/// here I want implement if (e.KeyCode == Keys.Enter) from code above with label1.Text = ("text inserted");
}
}
you can like this
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
label1.Text = ("text inserted");
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
KeyEventArgs ev = new KeyEventArgs(Keys.Enter);
textBox1_KeyDown(sender, ev);
}

Keypress event in cell of datagridview with key return

Code run when I use with key A.
But code not run when I use with key return.
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
TextBox txt = e.Control as TextBox;
txt.KeyPress += new KeyPressEventHandler(txt_KeyPress);
}
void txt_KeyPress(object sender, KeyPressEventArgs e)
{
/* Code run. */
if(e.KeyChar == (char)Keys.A)
{
MessageBox.Show("Hello");
}
/* Code not run! */
if (e.KeyChar == (char)Keys.Return)
{
MessageBox.Show("Hello");
}
}
But it run if I try with a text box.
Thank you TaW!
I solved it like this...
void TextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
}
}
void TextBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Return)
{
/* Your code here! */
}
}

How to detect if Enter was pressed in txtMessage_TextChanged() ?

I want to find out if Enter was pressed in
private void txtMessage_TextChanged(object sender, EventArgs e)
{
// do something
}
But I can't do this :
private void txtMessage_TextChanged(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar == (char)13)
{
// Then Enter key was pressed
}
}
Since txtMessage_TextChanged doesn't have this overload .
Any suggestions ? thanks !
You need to subscribe to an event that actually captures that information.
The KeyPress event fires before TextChanged, so you could use a bool to indicate whether the Enter key was pressed.
bool enterKeyPressed = false;
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
enterKeyPressed = (e.KeyChar == (char)Keys.Enter);
}
private void txtMessage_TextChanged(object sender, EventArgs e)
{
if (enterKeyPressed)
{
}
}
register to this:
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Login_KeyPress);
In that method set a boolean or something if keychar == 13
you can use keypress event to capture enter key event

Disable white space on key press in textbox

I am beginner in WinForms so i would like to learn that,
How can i disable white space keypress in textbox?
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// MyTextBox.Text ...?
}
Any help will be appreciated.
that's what you want, disallows all spaces
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = (e.KeyChar == (char)Keys.Space);
}
Use this code:
private void txt_keyPress(object sender, KeyPressEventArgs e) \
{
if ((sender as TextBox).SelectionStart == 0)
e.Handled = (e.KeyChar == (char)Keys.Space);
else
e.Handled = false;
}

Convert KeyDown keys to one string C#

I have magnetic card reader, It emulates Keyboard typing when user swipes card. I need to handle this keyboard typing to one string, when my WPF window is Focused. I can get this typed Key list, but I don't know how to convert them to one string.
private void Window_KeyDown(object sender, KeyEventArgs e)
{
list.Add(e.Key);
}
EDIT: Simple .ToString() method not helps. I've tried this already.
Rather than adding to a list why not build up the string:
private string input;
private bool shiftPressed;
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftShift || e.Key == Key.RightShift)
{
shiftPressed = true;
}
else
{
if (e.Key >= Key.D0 && e.Key <= Key.D9)
{
// Number keys pressed so need to so special processing
// also check if shift pressed
}
else
{
input += e.Key.ToString();
}
}
}
private void Window_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftShift || e.Key == Key.RightShift)
{
shiftPressed = false;
}
}
Obviously you need to reset input to string.Empty when you start the next transaction.
...or you can try this:
string stringResult = "";
list.ForEach(x=>stringResult+=x.ToString());
EDIT:
After good Timur comment I decided to suggest this:
you can use keyPress event to everything like this:
string stringResult = "";
private void Window_KeyPress(object sender, KeyPressEventArgs e)
{
stringResult += e.KeyChar;
}
Listen To PreviewTextInput Event Instead ...
the TextCompositionEventArgs has a property called "Text" which give you the text representation for key
for example Key.D2 will be just "2" ...i think it will
do the purpose
private void MainWindow_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
list.Add(e.Text);
}
You could have a member variable which is a StringBuilder.
something like
class A
{
StringBuilder _contents = new StringBuilder();
private void Window_KeyDown(object sender, KeyEventArgs e)
{
_contents.Append(e.Key.ToString());
}
}
You would have to create a new StringBuilder each time a new card was swiped and then to get the string you would use _contents.ToString();
String combined = String.Empty;
private void Window_KeyDown(object sender, KeyEventArgs e)
{
combined = combined + e.Key.ToString();
}

Categories