hi i want to write a text in any line inside a rich text box how can i do that ?am now able to append to existing one or insert to the beginning of the rich text box but i want to write to any line inside the text box.

Edited by awaresefere: spelling

5 Years
Discussion Span
Last Post by lxXTaCoXxl

First fill the box with new lines:

// I'm just using 50 lines as an example.
public void FillBoxWithLines()
for (int i = 0; i < 50; i++)
richTextBox1.Text += Environment.NewLine;

Next you can use .SelectionStart to choose a line (you can specify lines using a numeric up down control or something of that nature) for my example I'll be using a numeric up down.

public int SelectedLine {get; private set;}

// numericUpDown.ValueChanged event
public void SelectLine(object sender, EventArgs e)
this.SelectedLine = Convert.ToInt32(numericUpDown1.Value);

Now that you've selected a line with numeric up down or other control, use your .SelectionStart value for the first run.

public void TypeToLine()
richTextBox1.SelectionStart = SelectedLine;

Next you can use an array that will store which lines have been written to. to ensure your place in the text.

public bool[] WrittenLines = new bool[65535];

There's a little more to it, but this should help you out. Every line after the first requires math to determine the difference in text length so that you know which lines are empty and which are not. The selection start places you at the index specified within the text length. So I'd write a method using the TextChanged event to do the math on how many characters have been added to the textbox after the lines were added, and to which line they were added.

Edited by lxXTaCoXxl: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.