当我构建我的打字导师应用程序时,用户所犯的错误我希望将其显示在RED中的文本框中。我创建了一个LIST来存储索引值,但无法弄清楚如何将其解压缩并使它们在UserTexbox中显示为RED COLOR。无论如何,这是我的代码:
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!");
}
答案 0 :(得分:0)
您应该使用RichTextBox而不是TextBox,这样您可以更改SelectionStart,SelectionLength和SelectionColor,或直接修改Rtf。否则使用普通文本框,您将看到覆盖WndProc并在控件中进行一些自定义绘制,这是相当多的工作。下面是使用RichTextBox执行此操作的快速示例(您只需将TextBox更新为RichTextBox并将for循环更改为以下内容:
foreach (int index in lst) {
richTextBox.SelectionStart = index;
richTextBox.SelectionLength = 1;
richTextBox.SelectionColor = Color.Red;
}
让我知道它对你有用。