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;
}
Related
I need the following code : When I press shift key then I write small letters in my TextBox in other case I write big letters something like a “reverse” or all time pressed Caps Lock Key.
So This code and other similar is helpless because there are only one kind of letter size :
textBox1.CharacterCasing = CharacterCasing.Upper;
textBox1.CharacterCasing = CharacterCasing.Lower;
Thanks for help !
The easiest option is to change the text in the TextChanged event method. After entering a character, change its layout. But you must remember that text can be pasted / cut.
If you ignore this problem, you can use this simple example:
public partial class Form1 : Form
{
int last_len = 0;
bool char_to_lower = false;
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
// save last cursor position
var select_index = textBox1.SelectionStart;
// if text not delete - change char casing
if (textBox1.Text.Length > last_len && select_index > 0)
{
StringBuilder sb = new StringBuilder();
sb.Append(textBox1.Text.Take(select_index - 1).ToArray());
// check SHIFT and CAPS
if (char_to_lower || Control.IsKeyLocked(Keys.CapsLock))
sb.Append(textBox1.Text[select_index - 1].ToString().ToLower());
else
sb.Append(textBox1.Text[select_index - 1].ToString().ToUpper());
sb.Append(textBox1.Text.Skip(select_index).ToArray());
// insert new text in textBox
textBox1.Text = sb.ToString();
// return cursor position
textBox1.SelectionStart = select_index;
}
// save last length
last_len = textBox1.Text.Length;
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.Shift) char_to_lower = true;
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.Shift) char_to_lower = false;
}
}
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;
}
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();
}
}
I want to get a TextBox input key.
I was generate "KeyDown" event and
using the next
string inputKey = new KeysConverter().ConvertToString(e.KeyCode)
bun it is only assign upper case.
I want both upper case and lower case.
What should I do?
If you insist on using KeyDown Event This is what you need.
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
string inputKey;
bool capsLock = IsKeyLocked(Keys.CapsLock); // Check for capslock is on
bool shift = e.Shift; // Check for Shift button was pressed
if ((shift && !capsLock) || (!shift && capsLock))
{
inputKey = new KeysConverter().ConvertToString(e.KeyCode);
}
else if ((shift && capsLock) || (!shift && !capsLock))
{
inputKey = new KeysConverter().ConvertToString(e.KeyCode).ToLower();
}
}
You should use the KeyPress event and then get the character with e.KeyChar.
For example:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show(e.KeyChar.ToString());
}
I have a textbox and I need the user to enter only Cyrillic letters. User can't enter numbers and special characters (except space) and latin characters! The Value of variable "l" I will set by myself.
How can I make the KeyDown event to do this?
In WindowsForms I do it like this:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char l = e.KeyChar;
if ((l < 'А' || l > 'я') && l != '\b' )
{
e.Handled = true;
}
}
The most simple way I discovered is to leverage the OnPreviewTextInput event:
Markup:
<TextBox PreviewTextInput="UIElement_OnPreviewTextInput" />
Handler:
private void UIElement_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
bool isCyrillic = Regex.IsMatch(e.Text, #"\p{IsCyrillic}");
e.Handled = !isCyrillic;
}