简体   繁体   English

在Richtextbox中追加文本时不显示C#光标/插入符

[英]C# Cursor/ Caret Not display when Append text in Richtextbox

I am new to Programming, i am making C# Windows Form Application were, on selecting Tree node, it append the text in Richtextbox: 我是编程新手,正在制作C#Windows窗体应用程序,在选择“树”节点时,它将文本附加到Richtextbox中:

Qs1: For me Caret is not displayed after selecting the tree Node. Qs1:对我来说,选择树节点后不显示插入符号。 Qs2: Make display like editor, where if word start with // ( Comment) should be in green color. Qs2:使显示像编辑器一样,如果以//(注释)开头的单词应为绿色。

Thanks 谢谢

 if (treeView1.SelectedNode.Name == "Node1")
        {  
          this.richTextBox1.SelectedText += "  my text for Node1" + Environment.NewLine
          richTextBox1.Focus();
        }
        else if (treeView1.SelectedNode.Name == "Node2")
        {
         this.richTextBox1.SelectedText += "  my text for Node2" + Environment.NewLine
          richTextBox1.Focus();
        }

You're asking two questions related to RichTextBox . 您在问两个与RichTextBox有关的问题。 The preferred form on StackOverflow is one question per question . StackOverflow的首选形式是每个问题一个问题 You'll probably get more responses with more focused questions. 对于更集中的问题,您可能会得到更多答复。

That being said: 话虽如此:

  1. According to the documentation for the Select method: 根据有关Select方法的文档:

    The text box must have focus in order for the caret to be moved. 文本框必须具有焦点才能移动插入符号。

    So you need to do that first. 因此,您首先需要这样做。

    In addition, as a general rule, you should never modify the pre-existing Text or SelectedText with += because this will clear away any and all RTF formatting on that text. 此外,作为一般规则,您永远不要使用+=修改先前存在的TextSelectedText ,因为这将清除该文本上的所有RTF格式。 Instead, to insert text, you should set the selection to the desired location, with length zero, and insert there. 相反,要插入文本,应将所选内容设置为所需的长度为零的位置,然后在该位置插入。 Thus: 从而:

     public static void FocusAndAppendToSelectedText(this RichTextBox richTextBox, string text) { Action append = () => { richTextBox.Focus(); var start = richTextBox.SelectionStart; var length = richTextBox.SelectionLength; var insertAt = start + length; richTextBox.Select(insertAt, 0); richTextBox.SelectedText = text; }; if (richTextBox.InvokeRequired) richTextBox.BeginInvoke(append); else append(); } 

    Also, you should use \\n rather than Environment.Newline because the latter will get simplified into the former anyway. 另外,您应该使用\\n而不是Environment.Newline因为后者无论如何都会简化为前者

  2. A question like "[How to] Make display like editor, where if word start with // ( Comment) should be in green color" is very general. 诸如“ [如何使显示像编辑器一样,如果以//(注释)开头的单词应为绿色的地方”这样的问题非常笼统。 Try to break it down into discrete issues and ask questions for those you can't figure out yourself. 尝试将其分解为离散的问题,并针对无法解决的问题提出问题。 To get you started, see this question here: highlight the '#' until line end in richtextbox . 要开始使用,请在此处看到以下问题: 突出显示“#”,直到richtextbox中的行结束 However, you may want to set the SelectionBackColor not the SelectionColor , depending on your precise UI requirements. 但是,您可能需要根据您的精确UI要求设置SelectionBackColor而不是SelectionColor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM