Answer the question
In order to leave comments, you need to log in
How to compare entered character with character from text or Keyboard trainer in C#?
Tell me how this site works.
I want to write the same simulator in C #, but I can’t understand how it works.
Let's say there is text, we break it into an array of characters. And now what should I do with this array?
It can be compared with the pressed key, but how? Tried like this:
char[] znak;
public MainWindow()
{
InitializeComponent();
znak = textBlock1.Text.ToCharArray();
}
int rate = 0;
private void textBox1_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (e.Text == Convert.ToString(znak[rate]))
{
textBox1.Text += e.Text;
rate++;
}
else
{
e.Handled = true;
}
}
Answer the question
In order to leave comments, you need to log in
textBox1.Text += e.Text;
This line is superfluous here (you add the letter you pressed once again)
Also, do not forget about the space, it does not have "text", so you will also have to separately add a condition for it
private void textBox1_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space && char.IsWhiteSpace(znak[rate]))
{
rate++;
}
else if (e.Key != Key.Space)
{
}
else
{
e.Handled = true;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question