问题描述
您好
我有几个richTextBox。我为其中一个编写了这段代码:
I have several richTextBoxes. I wrote this code for one of them:
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern short GetAsyncKeyState(Keys vKey);
private void rtbFMR_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.ShiftKey)
{
if (Convert.ToBoolean(GetAsyncKeyState(Keys.LShiftKey)))
{
InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"));
rtbFMR.Select(rtbFMR.SelectionStart, 0);
rtbFMR.SelectionFont = new Font("Tahoma", 10, FontStyle.Regular);
}
else if (Convert.ToBoolean(GetAsyncKeyState(Keys.RShiftKey)))
{
InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.CreateSpecificCulture("fa-IR"));
rtbFMR.Select(rtbFMR.SelectionStart, 0);
rtbFMR.SelectionFont = new Font("Arial", 12, FontStyle.Regular);
}
}
}
如果我不使用"InputLanguage"。 CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.CreateSpecificCulture(" fa-IR"))"和"InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.CreateSpecificCulture(" en-US"))",
richTextBox的语言不会改变。
If I do not use "InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.CreateSpecificCulture("fa-IR"))" and "InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))", the language of the richTextBox is not changed.
AND
我无法用左右箭头键+ shift键选择richTextBox文本的一部分。
I cannot select a part of the text of the richTextBox with right and left arrow keys + shift key.
推荐答案
private void richTextBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if(char.IsLetter(e.KeyChar))
{
if ((e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z'))
{
//MessageBox.Show("Letter " + e.KeyChar.ToString());
richTextBox2.Select(richTextBox2.SelectionStart, 0);
richTextBox2.SelectionFont = new Font("Tahoma", 10, FontStyle.Italic);
}
else
{
//MessageBox.Show("Letter " + e.KeyChar.ToString());
richTextBox2.Select(richTextBox2.SelectionStart, 0);
richTextBox2.SelectionFont = new Font("Tahoma", 12, FontStyle.Bold);
}
}
else if (char.IsDigit(e.KeyChar))
{
//MessageBox.Show("Digit");
if (InputLanguage.CurrentInputLanguage.Culture.Name == "fa-IR")
{
if (e.KeyChar == '\u0030')
e.KeyChar = '\u0660';
else if (e.KeyChar == '\u0031')
e.KeyChar = '\u0661';
else if (e.KeyChar == '\u0032')
e.KeyChar = '\u0662';
else if (e.KeyChar == '\u0033')
e.KeyChar = '\u0663';
else if (e.KeyChar == '\u0034')
e.KeyChar = '\u0664';
else if (e.KeyChar == '\u0035')
e.KeyChar = '\u0665';
else if (e.KeyChar == '\u0036')
e.KeyChar = '\u0666';
else if (e.KeyChar == '\u0037')
e.KeyChar = '\u0667';
else if (e.KeyChar == '\u0038')
e.KeyChar = '\u0668';
else if (e.KeyChar == '\u0039')
e.KeyChar = '\u0669';
}
}
else
{
//MessageBox.Show("Sign");
}
}
已编辑:
我正在运行我的程序。它运行良好。为什么?为什么以前没有使用此代码运行?
I am running my program. It is running well. Why? Why was not it run with this code previously?
这篇关于C#:Ctrl +向右和向左Shift键和KeyUp以及更改richTextBox的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!