C# keydown event storing in char variables - c#

I'm wondering how I could just store the pressed key in a char variable (C#).
Could anyone help me?
with kind regards,
dutchjelly
(is it possible to do this?)
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char c = e.KeyChar;
if(c = 'A')
{
do something
}
}

You could use something like this:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
char c = Convert.ToChar(e.KeyCode);
if (c == 69)
{
do something
}
}
where 69 represents an ASCII code (in this case E, you can search for other on google).

You can do the following:
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
Keys k = e.KeyCode; //This is your key code
}
The same way you could store the pressed keys to a List or some structure of your need:
List<Keys> keys = new List<Keys>();
keys.Add(k);
EDIT:
In the case you just want to get the char value of what the user has pressed you can do this:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char c = e.KeyChar;
}

Related

How to do Button_Click from keybord?

How to do my "Button_click" when I press some key on keyboard? My code:
private void Button_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
textBox1.Text = textBox1.Text + b.Text;
}
private void button1_KeyPress(object sender, KeyPressEventArgs e)
{
Char ch0 = '0';
if (e.KeyChar == ch0)
{
//MyButtonClick event
}
}
...
KeyPress event runs only when the button on form in focus, but I need to KeyPress worked just as I push the button on my keyboard. Please, give an exaple in you'r answer.
And don't be strict, I am only a beginner...
It is not completely clear that what are you asking, but I guess you want to do the same action on both keypress of your keyboard and when user clicks on the button.
look at this topic on MSDN for key_press:
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= 48 && e.KeyChar <= 57)
{
MessageBox.Show("Form.KeyPress: '" +
e.KeyChar.ToString() + "' pressed.");
switch (e.KeyChar)
{
case (char)49:
case (char)52:
case (char)55:
MessageBox.Show("Form.KeyPress: '" +
e.KeyChar.ToString() + "' consumed.");
e.Handled = true;
break;
}
}
}
To do the same action in 2 event, you can write another method and call them from both events.
public void MyAction(mypars)
{
....
}
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
MyAction(mypars);
}
private void Button_Click(object sender, EventArgs e)
{
MyAction(mypars);
}
It is also possible to call another event method, since they are like other methods, but I really do not suggest that! but it is like this:
private void button1_KeyPress(object sender, KeyPressEventArgs e)
{
Button_Click(sender,new EventArgs());
}
Update
You have to be better than "I want my button to be pressed". If you mean the visuals, you can set the focus of the control, like:
myButton.Focus();
Hope I could help you out :)
update 2
you have to use it like this
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
MyAction(mypars);
myButton.Focus();
}
if you need it to focus only on special chars, then check it before doing anythig like
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= 48 && e.KeyChar <= 57)
{
MyAction(mypars);
myButton.Focus();
}
}

How to determine if Pressed key is Underscore Or Minus? C#

The problem is that for both underscore and minus the keyvalue is 189, and the keycode is Keys.OemMinus. So I am unable to check whether pressed key is underscore or minus. Please Help.
private void Some_KeyDown(object sender, KeyEventArgs e)
{
if(Pressed key is minus/dash)
{
MessageBox.Show("minus");
}
if(pressed key is underscore)
{
MessageBox.Show("underscore");
}
}
If this is a WinForms project, use the KeyPress event instead of the KeyDown event:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == '-')
{
MessageBox.Show("Minus");
}
if (e.KeyChar == '_')
{
MessageBox.Show("Underscore");
}
}

Make code for multiple textbox when press Enter send command Tab better

i already have code when user press key Enter on keyboard, it return tab and "jump" to next field, its working great, its possible make it for 2 or 3 textbox, problem when need make it on multiple textbox like 20 textbox for each form, its just not work.
See code:
// Detect if Enter key is pressed on each text box, mute sound enter "ding" sound and replace Enter for tab (problem that have make it for each textbox)
private void txtAltura_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
e.Handled = true; //Silenciar Enter
SendKeys.Send("{TAB}");
}
}
private void txtLargura_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
e.Handled = true; //Silenciar Enter
SendKeys.Send("{TAB}");
}
}
private void txtProfundidade_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
e.Handled = true; //Silenciar Enter
SendKeys.Send("{TAB}");
}
}
//execute keypress command when enter is typed on textbox
private void txtProfundidade_TextChanged(object sender, EventArgs e)
{
if (txtProfundidade.Text != "") { foreach (char c in txtProfundidade.Text.ToCharArray()) txtProfundidade_KeyPress(sender, new KeyPressEventArgs(c)); }
}
private void txtLargura_TextChanged(object sender, EventArgs e)
{
if (txtLargura.Text != "") { foreach (char c in txtLargura.Text.ToCharArray()) txtLargura_KeyPress(sender, new KeyPressEventArgs(c)); }
}
private void txtAltura_TextChanged(object sender, EventArgs e)
{
if (txtAltura.Text != ""){foreach (char c in txtAltura.Text.ToCharArray()) txtAltura_KeyPress(sender, new KeyPressEventArgs(c));}
}
Hope make it better.
Thanks in advance..
if its windows form app can use this, this will replace tab key press with Enter key
protected override bool ProcessKeyPreview(ref Message m)
{
if (m.Msg == 0x0100 && (int)m.WParam == 13)
{
this.ProcessTabKey(true);
}
return base.ProcessKeyPreview(ref m);
}
From what I understand, you're trying to figure out a way to assign event handlers to multiple textbox controls and don't want to write a handler for each one. If that's the case, try this:
private void Form1_Load(object sender, EventArgs e)
{
foreach (TextBox textBox in this.Controls.OfType<TextBox>())
{
textBox.KeyDown += new KeyEventHandler(textBox_KeyDown);
}
}
void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
SendKeys.Send("{TAB}");
}
}
This will assign a handler to each textbox control on the form.
That worked perfectly well.
I also have to repeat this on each textbox to check if all codes was filled and update statusbar:
private void txtAltura_TextChanged(object sender, EventArgs e)
{
testePreenchidoecalculo();
}
private void txtLargura_TextChanged(object sender, EventArgs e)
{
testePreenchidoecalculo();
}
private void txtProfundidade_TextChanged(object sender, EventArgs e)
{
testePreenchidoecalculo();
}
Its possible to make it better ?

Allow backspace button to work in validated TextBox

I have the following code to only allow letters in the text box:
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
Char pressedKey = e.KeyChar;
if (Char.IsLetter(pressedKey))
{
// Allow input.
e.Handled = false
}
else
e.Handled = true;
}
}
How can I allow the backspace key to work because, it doesnt let me to delete characters after typed
You can check if the key pressed is a Control character using Char.IsControl(...), like this:
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsLetter(e.KeyChar) && !Char.IsControl(e.KeyChar))
e.Handled = true;
}
If you specifically need to check only chars + Delete, use this:
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsLetter(e.KeyChar) && e.KeyChar != (char)Keys.Back)
e.Handled = true;
}
This is for those using VB.net. There's a weird conversion I'd never come across that took me a while to figure out.
This allows only numbers, letters, backspace and space.
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
e.Handled = e.KeyChar <> ChrW(Keys.Back) And Not Char.IsLetter(e.KeyChar) And Not Char.IsDigit(e.KeyChar) And Not Char.IsSeparator(e.KeyChar)
End Sub

c# windows forms capital letters

My user can enter some text into the combobox but I wish that this text is automatically shown in capital letters (as if user had capslock on). Any ideas how to do this?
You will want to handle the KeyPress event.
private void ComboBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= 'a' && e.KeyChar <= 'z')
e.KeyChar -= (char)32;
}
32 is just the difference in ASCII values between lowercase and uppercase letters.
Here is how I handled it, it gives a much smoother change than simply replacing the whole text.
private void ComboBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (Char.IsLetter(e.KeyChar))
{
int p = this.SelectionStart;
this.Text = this.Text.Insert(this.SelectionStart, Char.ToUpper(e.KeyChar).ToString());
this.SelectionStart = p + 1;
}
}
another example
private void TextBox_Validated(object sender, EventArgs e)
{
this.TextBox.Text = this.TextBox.Text.ToUpper();
}
Regards
You can register to the TextChanged event and convert the text to capitals.
private void combobox_TextChanged(object sender, EventArgs e)
{
string upper = combobox.Text.ToUpper();
if(upper != combobox.Text)
combobox.Text = upper;
}

Categories

Resources