public partial class Form1 : MetroFramework.Forms.MetroForm
public Form1()
{
InitializeComponent();
this.Controls.Add(richTextBox1);
richTextBox1.KeyPress += new KeyPressEventHandler(richTextBox1_KeyPress);
richTextBox1.Select();
//richTextBox1.BringToFront();
}
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.Text = loadText("coding.txt");
//this.ActiveControl = richTextBox1;
richTextBox1.Select();
}
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// e.Handled = false;
//richTextBox1.Focus();
char key = e.KeyChar;
char[] c = richTextBox1.Text.ToCharArray();
next++;
if (next == 0)
{
timer1.Start();
}
//here we check number and chars and considering this colour char and button
for (int i = 0; i < this.panel1.Controls.Count; i++)
{
if (this.panel1.Controls[i].Name.Substring(0, 4).ToLower().CompareTo("btn" + key.ToString().ToLower()) == 0 && this.panel1.Controls[i].Name.Length <= 4)
textAndButtonColour((Button)this.panel1.Controls[i], c, next, key);
}
//for checking symbols
if ((int)key == 33) textAndButtonColour(btn1, c, next, key);
if ((int)key == 64) textAndButtonColour(btn2, c, next, key);
if ((int)key == 35) textAndButtonColour(btn3, c, next, key);
if ((int)key == 36) textAndButtonColour(btn4, c, next, key);
if ((int)key == 37) textAndButtonColour(btn5, c, next, key);
if ((int)key == 94) textAndButtonColour(btn6, c, next, key);
if ((int)key == 38) textAndButtonColour(btn7, c, next, key);
if ((int)key == 42) textAndButtonColour(btn8, c, next, key);
if ((int)key == 40) textAndButtonColour(btn9, c, next, key);
if ((int)key == 41) textAndButtonColour(btn0, c, next, key);
if ((int)key == 123 || (int)key == 91) textAndButtonColour(btnopenbrace, c, next, key);
if ((int)key == 125 || (int)key == 93) textAndButtonColour(btnclosebrace, c, next, key);
if ((int)key == 58 || (int)key == 59) textAndButtonColour(btnsemi, c, next, key);
if ((int)key == 34 || (int)key == 39) textAndButtonColour(btndouble, c, next, key);
if ((int)key == 60 || (int)key == 44) textAndButtonColour(btncomma, c, next, key);
if ((int)key == 62 || (int)key == 46) textAndButtonColour(btndot, c, next, key);
if ((int)key == 63 || (int)key == 47) textAndButtonColour(btnbackslash, c, next, key);
if ((int)key == 124 || (int)key == 92) textAndButtonColour(btnforwardslash, c, next, key);
if ((int)key == 95 || (int)key == 45) textAndButtonColour(btnminus, c, next, key);
if ((int)key == 43 || (int)key == 61) textAndButtonColour(btnequals, c, next, key);
//check spacebar and enter keys
if ((int)key == 32) textAndButtonColour(btnspacebar, c, next, key);
if ((int)key == 13) textAndButtonColour(btnenter, c, next, key);
//e.Handled = true;
if (Char.IsControl(e.KeyChar))
{
e.Handled = true;
next--;
}
//stop timer when the number of keypesses equal to the number of letters displayed in the box
if (c.Length == next + 1)
{
timer1.Stop();
//richTextBox1.Text = loadText(fileName);
//richTextBox1.SelectionStart = 0;
//richTextBox1.SelectionLength = next;
//richTextBox1.SelectionColor = Color.Black;
//richTextBox1.SelectionBackColor = Color.White;
//charCorrect.Text = correct.ToString();
//charIncorrect.Text = incorrect.ToString();
}
e.Handled = true;
}
因为我没有扩展表单类而是使用Metroframework表单。 我需要在表单加载时关注richtextbox。我确保tabindex没问题,我使用焦点方法 - 不工作,选择方法正在工作但是然后keypresseventlistener contorl无法正常工作