简体   繁体   English

当我选择它时,如何从 Richtextbox 复制一行?

[英]How can I copy a line from a Richtextbox when I select it?

I am trying to automatically highlight and copy a single line from a rich text box and store it in a variable when I click on any part of the line.当我单击行的任何部分时,我试图自动突出显示并复制富文本框中的一行并将其存储在变量中。

Is this possible?这可能吗?

The Click event and something like the following should help: Click 事件和类似下面的内容应该会有所帮助:

private void richTextBox1_Click(object sender, EventArgs e)
{
    int index = richTextBox1.SelectionStart;
    int line = richTextBox1.GetLineFromCharIndex(index);
    string lineText = (richTextBox1.Lines.Length > 0) ? richTextBox1.Lines[line] : "";

    //Debug output for my own testing purposes
    Debug.WriteLine(lineText);
}  

You may want to do something different when the RichTextBox is empty.RichTextBox为空时,您可能想要做一些不同的事情。 I just use an empty string.我只是使用一个空字符串。
Here's an example of the output from a simple app:下面是一个简单应用程序的输出示例: 示例输出
The text show in the output window reflects the order in which I clicked the lines after typing.输出窗口中显示的文本反映了我在键入后单击行的顺序。
However, one caveat to this is that you don't need to click exactly on the line for it to count.但是,对此的一个警告是,您无需精确地单击该行即可进行计数。 For example, clicking in the empty space below the last line registers as clicking the last line, because that's where the cursor ends up.例如,单击最后一行下方的空白区域会记录为单击最后一行,因为这是光标所在的位置。 It might not matter a whole lot, but it's something to be aware of.这可能无关紧要,但需要注意。

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

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