Creating an XHTML Editor in Code > Performing Other Common Tasks > Selecting a Paragraph using the Select Method |
The Select method in the C1Editor is equivalent to the familiar Select method in the TextBox and RichTextBox controls. You pass the index of the first character in the selection and the selection length, and the current selection is updated.
The code below shows how you could use the Select method to select the sixth paragraph in the current document:
To write code in C#
C# |
Copy Code
|
---|---|
void selectParagraph_Click(object sender, EventArgs e) { // get text (notice new line handling) string txt = this.c1Editor1.Text; txt = txt.Replace("\r\n", "\n"); // find 6th paragraph int start = IndexOf(txt, '\n', 6) + 1; int len = IndexOf(txt, '\n', 7) - start; // select the paragraph c1Editor1.Select(start, len); } |
The code starts by retrieving the Text property and replacing cr/lf combinations with simple lf characters. This is necessary to get the selection indices to match the content of the document. Next, the code uses the IndexOf helper method to find the position of the sixth paragraph. Finally, it selects the paragraph using the Select method.
The implementation of the IndexOf method is as follows:
To write code in C#
C# |
Copy Code
|
---|---|
int IndexOf(string text, char chr, int count) { for (int index = 0; index < text.Length; index++) { if (text[index] == chr) { count--; if (count <= 0) { return index; } } } return text.Length; } |