We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to insert selected text within XML tag in WinForms SyntaxEditor (EditControl)?

Platform: WinForms |
Control: EditControl

Add or remove the lines

In EditControl, you can load the selected text inside XML tag by using “SelectedText”, “Selection” properties and “InsertText”, “DeleteText” functions.

The following code example illustrates the same.


//Gets the selected text
string selectedText = editControl1.SelectedText;
//Delete the selected text
editControl1.DeleteText(editControl1.Selection.Top, editControl1.Selection.Bottom);
//Gets the cursor position
IParsePoint iparse = editControl1.GetRealCursorPosition();
int cursorposition = iparse.Position;
if (flag)
    cursorposition = cursorposition - 1;
//Inserting before
editControl1.InsertText(iparse.Line, cursorposition, "#region\n");
iparse = editControl1.GetRealCursorPosition();
if (flag)
    //Inserting selected text
    editControl1.InsertText(iparse.Line, cursorposition - 1, selectedText + "\n");
    flag = false;
    //Inserting selected text
    editControl1.InsertText(iparse.Line, cursorposition, selectedText + "\n");
iparse = editControl1.GetRealCursorPosition();
//Inserting text at end of the selection
editControl1.InsertText(iparse.Line, cursorposition, "#endregion");


'Gets the selected text
Dim selectedText As String = editControl1.SelectedText
'Delete the selected text
editControl1.DeleteText(editControl1.Selection.Top, editControl1.Selection.Bottom)
'Gets the cursor position
Dim iparse As IParsePoint = editControl1.GetRealCursorPosition()
Dim cursorposition As Integer = iparse.Position
If flag Then
 cursorposition = cursorposition - 1
End If
'Inserting before
editControl1.InsertText(iparse.Line, cursorposition, "#region" & Constants.vbLf)
iparse = editControl1.GetRealCursorPosition()
If flag Then
 'Inserting selected text
 editControl1.InsertText(iparse.Line, cursorposition - 1, selectedText & Constants.vbLf)
 flag = False
 'Inserting selected text
 editControl1.InsertText(iparse.Line, cursorposition, selectedText & Constants.vbLf)
End If
iparse = editControl1.GetRealCursorPosition()
'Inserting text at end of the selection
editControl1.InsertText(iparse.Line, cursorposition, "#endregion"



2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile