hi guys:)if i have three lines saying "line1","line2"and "line3" and if i want to modify the first line to "line1 ok", the second to "line2 ok " and replace in there original location so i will have "line1 ok",line2 ok" ,"line3" for each line after modification.what should i do to do this.thanks in advance.

Dani AI

Generated

Good pointers from and . If you want to touch specific lines without rebuilding the whole document (and to avoid flicker or losing per-line formatting), target the line by character index and edit in place. For your case of appending " ok" to a line, this approach leaves the existing formatting intact and matches the inserted text to the last character of that line.

// Append text to a specific line in a WinForms RichTextBox.
void AppendToLine(RichTextBox rtb, int lineIndex, string suffix)
{
    if (lineIndex < 0 || lineIndex >= rtb.Lines.Length) return;

    int start = rtb.GetFirstCharIndexFromLine(lineIndex);
    string line = rtb.Lines[lineIndex];
    int endOfLine = start + line.Length;

    // Copy the last character's formatting so the inserted text blends in.
    Font lastFont = null;
    Color lastColor = Color.Empty;
    if (line.Length > 0)
    {
        rtb.Select(endOfLine - 1, 1);
        lastFont = rtb.SelectionFont;
        lastColor = rtb.SelectionColor;
    }

    rtb.Select(endOfLine, 0);
    if (lastFont != null) { rtb.SelectionFont = lastFont; rtb.SelectionColor = lastColor; }
    rtb.SelectedText = suffix;
}

// Usage: make line 1 and 2 read "... ok"
AppendToLine(richTextBox1, 0, " ok");
AppendToLine(richTextBox1, 1, " ok");

Notes:

  • Line indexes are zero-based. If a UI control shows 1-based values, subtract 1 before calling.
  • GetFirstCharIndexFromLine handles \r\n vs \n for you, so you do not need to count newline pairs manually.
  • Lines may omit a trailing empty line; guard with the bounds check above.

If you actually meant Python, the same idea applies. Tkinter example:

def append_ok_to_line(text, line_idx):
    start = f"{line_idx+1}.0"
    end = f"{line_idx+1}.0 lineend"
    text.insert(end, " ok")

Recommended Answers

All 2 Replies

You should load the entire contents of the Rich Text Box into an array or list of strings separated by CR/LF, modify them based on your criteria, then overwrite and append all of the text.

The below source will work perfectly. It uses a string array and the richTextBox.Lines property to change each line. It's also using a numeric up down control to choose the line to change. The numeric up down control has a min value of 1 and a max of 65535 in my test app, so that the value will look more realistic to the untrained eye.

// Array holding our information.
        private string[] Lines = new string[65535];

        // The selected line.
        private int SelectedLine = 0;
        
        /// <summary>
        /// Numeric Up Down - Value Changed Event
        /// I'm using this event to select the line I want in my Rich Text Box.
        /// </summary>
        private void ChooseLine(object sender, EventArgs e)
        {
            SelectedLine = Convert.ToInt32(numericUpDown1.Value) - 1;
        }

        /// <summary>
        /// Button - Click Event
        /// I'm using this to change the line I've selected.
        /// </summary>
        private void ChangeLineText(object sender, EventArgs e)
        {
            if (SelectedLine > richTextBox1.Lines.Length)
                MessageBox.Show("Invalid line...");

            else
            {
                Lines = richTextBox1.Lines;
                Lines[SelectedLine] = textBox1.Text;
                richTextBox1.Lines = Lines;
            }
        }

Hope that's what you were wanting. If it answered your post don't forget to mark the thread as solved. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.