winforms 是否可以写入RichTextBox中的给定行、列?

x759pob2  于 2023-02-19  发布在  其他
关注(0)|答案(3)|浏览(190)

我有一个带有RichTextBox的WinForm。我试图在给定的行和列写入RichTextBox:下面是代码:公共窗体1(){

InitializeComponent();

        richTextBox1 = new RichTextBoxWithMouseSelectionFixed();
        richTextBox1.Location = new System.Drawing.Point(62, 46);
        richTextBox1.Name = "richTextBox1";
        richTextBox1.Size = new System.Drawing.Size(461, 391);
        richTextBox1.TabIndex = 0;
        richTextBox1.Text = "";
        richTextBox1.HideSelection = false;
        Controls.Add(richTextBox1);

        int line = 3; 
        int column = 5;
        GoToLineAndColumn(richTextBox1, line, column);
    }

    private RichTextBoxWithMouseSelectionFixed richTextBox1;

    private void GoToLineAndColumn(RichTextBox richTextBox1, int line, int column)
    {
        int offset = 0;
        for (int i = 0; i < line - 1 && i < richTextBox1.Lines.Length; i++)
        {
            offset += richTextBox1.Lines[i].Length + 1;
        }
        richTextBox1.Focus();
        richTextBox1.Select(offset + column, 0);
    }

我试着写:
添加文本("当前单词");
以及:
富文本框1.文本="当前单词";
但是在两种情况下,currentWord都被写入第一行的第0列。

6ie5vjzr

6ie5vjzr1#

public Form1()
        {
            InitializeComponent();
            int line = 6;
            int column = 30;

            RichTextBox richTextBox1 = new RichTextBox();
            richTextBox1.Location = new System.Drawing.Point(62, 46);
            richTextBox1.Name = "richTextBox1";
            richTextBox1.Size = new System.Drawing.Size(461, 391);
            richTextBox1.TabIndex = 0;
            richTextBox1.Text = "";
            richTextBox1.HideSelection = false;
            Controls.Add(richTextBox1);

            for (int i = 0; i < line - 1; i++)
                richTextBox1.Text += "\n";
            for (int i = 0; i < column - 1; i++)
                richTextBox1.Text += "  ";
            richTextBox1.Text += "Just Text";
        }
xhv8bpkk

xhv8bpkk2#

RichTextBox类具有可用于将文本写入其中特定索引的属性。首先,定义要写入的行和列的从零开始的索引,然后获取该行中第一个字符的索引,如下所示

// define the index of the line and the column of the line you want to write to in the rich text box
int lineIndex = 2;
 // zero-based index of the line
int columnIndex = 10;
// Get the index of the first character in that line you specified above
int index = richTextBox.GetFirstCharIndexFromLine(lineIndex) + columnIndex;
//declare the string you wish to paste
string mytext = "Write to this section only";
//Use the SelectionStart and SelectionLength properties to write the text
richTextBox.SelectionStart = index;
richTextBox.SelectionLength = mytext.Length; 
// clear any previous selection
richTextBox.SelectedText = mytext;
11dmarpk

11dmarpk3#

我找到的唯一解决办法是在文本前添加空行和空白字符:

private void WriteAtLineAndColumn(RichTextBox richTextBox1, string text, int line, int column)
    {
        // Insert blank lines at the beginning of the RichTextBox.
        for (int i = 0; i < line - 1; i++)
        {
            richTextBox1.AppendText(Environment.NewLine);
        }
        int index = richTextBox1.GetFirstCharIndexFromLine(line - 1);
        richTextBox1.Select(index, 0);
        string spaces = new string(' ', column);
        richTextBox1.SelectedText = spaces + "This is a sample text";

        // Insert the text at the desired position.
        richTextBox1.Select(richTextBox1.GetFirstCharIndexFromLine(line - 1) + column, 0);
        richTextBox1.SelectedText = text;
    }

相关问题