English 中文(简体)
从RichTextBox控件检索准确的字符串数组
原标题:Retrieving an accurate string array from a RichTextBox control

我的表单上有一个RichTextBox控件。该控件的设置方式是,在输入32行文本后,它将换行到下一行。我遇到的问题是,我希望能够检索表示我控制的行的字符串数组。我知道RichTextBox附带了一个Lines属性,但我遇到了两个问题:

1) 我只想要一个字符串数组,显示仅在屏幕上可见的行。现在Lines数组返回RichTextBox中的每一行。我只想返回屏幕上可见的行。

2) The Lines property is not giving me a true representation of my lines. It counts a "line" as a line of text ended by a carriage return or . So in other words, if I type 64 characters and none of them are a carriage return, then it should return 2 lines (because there are 32 characters per line). Instead, it doesn t return any lines until I hit enter. Even then, it only returns 1 line, not 2. Its acting more like a Paragraph property, if there was such a thing.

有人知道解决这两个问题的方法吗?我正在使用C#btw

最佳回答

你必须做一些技巧来实现这一点,这些技巧与根据字符索引查询实际行的位置有关。下面的程序显示了一种方法。你可能需要稍微加强一下,但它应该让你开始:

public partial class Form1 : Form
{        
    public Form1()
    {
        InitializeComponent();                        
    }        

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
        if (richTextBox1.Text == "")
            return; 

        // Lines corresponding to the first and last characters:
        int firstLine = richTextBox1.GetLineFromCharIndex(0); 
        int lastLine = richTextBox1.GetLineFromCharIndex(richTextBox1.Text.Length);

        // Get array of lines:
        List<string> lines = new List<string>();
        for (int i = firstLine; i <= lastLine; i++)
        {
            int firstIndexFromLine = richTextBox1.GetFirstCharIndexFromLine(i);
            int firstIndexFromNextLine = richTextBox1.GetFirstCharIndexFromLine(i + 1);

            if (firstIndexFromNextLine == -1)
            {
                // Get character index of last character in this line:
                Point pt = new Point(richTextBox1.ClientRectangle.Width, richTextBox1.GetPositionFromCharIndex(firstIndexFromLine).Y);
                firstIndexFromNextLine = richTextBox1.GetCharIndexFromPosition(pt);
                firstIndexFromNextLine += 1;
            }

            lines.Add(richTextBox1.Text.Substring(firstIndexFromLine, firstIndexFromNextLine - firstIndexFromLine));
        }

        // Print to richTextBox2 while debugging:
        richTextBox2.Text = "";
        foreach (string line in lines)
        {
            richTextBox2.AppendText(">> " + line + Environment.NewLine);
        }                        
    }
}
问题回答

暂无回答




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签