When I am building my typing tutor application the errors user made I want to show it in RED into his textbox. I created a LIST to have the index values stored but cannot figure it out how to retieve it and make them display as RED COLOR in UserTexbox. Anyways here is my code:
void ShowErrors()
{
try
{
List<int> lst = new List<int>();
string sample, user;
sample = TBox_Sample.Text; //Sample Text Given to the user
user = TBox_User.Text; //User input string
for (int i =开发者_高级运维 0; i < sample.Length; i++)
{
if (sample[i] != user[i])
{
lst.Add(i); //Made this list which contains indexes of errors positioned.
}
}
string user_new = TBox_User.Text.ToString();
for (int j = 0; j <= lst.Count; j++)
{
??? I WANT TO SHOW IN 'TBox_User' ALL ERRORS MARKED WITH RED WITH THE HELP OF MY LIST OBJECT: LST !!!
}
}
catch (IndexOutOfRangeException)
{
MessageBox.Show("There is no input from the user!");
//int ijj = 0;
}
catch (Exception)
{
MessageBox.Show("Unknown Error!");
}
You should be using a RichTextBox instead of a TextBox, that way you can either change the SelectionStart, SelectionLength, and SelectionColor, or modify the Rtf directly. Otherwise with a normal textbox you would be looking at overriding WndProc and doing some custom painting in the control which is quite a bit of work. Here is a quick example of using a RichTextBox to do this (all you would have to do is update your TextBox to RichTextBox and change your for-loop to the following:
foreach (int index in lst) {
richTextBox.SelectionStart = index;
richTextBox.SelectionLength = 1;
richTextBox.SelectionColor = Color.Red;
}
Let me know how it works for you.
精彩评论