Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 在第二个文本框中突出显示错误(红色),如在C中键入导师应用程序_.net - Fatal编程技术网

.net 在第二个文本框中突出显示错误(红色),如在C中键入导师应用程序

.net 在第二个文本框中突出显示错误(红色),如在C中键入导师应用程序,.net,.net,当我构建打字导师应用程序时,用户犯了错误,我想在他的文本框中以红色显示它。我创建了一个列表来存储索引值,但无法确定如何重新设置它,并使它们在UserTexbox中显示为红色。无论如何,这是我的代码: void ShowErrors() { try { List<int> lst = new List<int>(); string sample, user; sample = TBox_Sample.Text;

当我构建打字导师应用程序时,用户犯了错误,我想在他的文本框中以红色显示它。我创建了一个列表来存储索引值,但无法确定如何重新设置它,并使它们在UserTexbox中显示为红色。无论如何,这是我的代码:

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!");
    }

您应该使用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;
}

让我知道它对您的作用。

是C,不是C