Articles in this section
Category / Section

How to Indent and Outdent text in WinForms SyntaxEditor (EditControl) without selection?

2 mins read

Indent and outdent text

In EditControl, only the selected contents can be indented and outdented. By using the “CurrentPosition” property, even unselected contents can also be indented and outdented. The following code example is to demonstrate the same.

C#

Point savepoint = new Point();
void editControl1_MouseDown(object sender, MouseEventArgs e)
{
    //Need to set cursor position on mouse down.
    savepoint = this.editControl1.CurrentPosition;
}
void editControl1_KeyUp(object sender, KeyEventArgs e)
{
    //Need to set cursor position on up or down.
    savepoint = this.editControl1.CurrentPosition;
}
private void button1_Click(object sender, EventArgs e)
{
    //Indents selected text of the EditControl.
    this.editControl1.IndentSelection();
    //To set the EditControl on focus
    this.editControl1.Focus();
}
private void button2_Click(object sender, EventArgs e)
{
    //Outdents selected text of the EditControl.
    this.editControl1.OutdentSelection();
    if (!this.savepoint.IsEmpty)
        //Gets the current position of the EditControl.
        this.editControl1.CurrentPosition = savepoint;
    //Moves caret to the beginning of the line. First whitespaces are skipped.
    this.editControl1.MoveToLineStart();
    //To set the EditControl on focus
    this.editControl1.Focus();
    //Reset the selection.
    this.editControl1.ResetSelection();
}

VB

Private savepoint As New Point()
Private Sub editControl1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
    'Need to set cursor position on mouse down.
    savepoint = Me.editControl1.CurrentPosition
End Sub
Private Sub editControl1_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
    'Need to set cursor position on Keyup.
    savepoint = Me.editControl1.CurrentPosition
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button1.Click
    'Indents selected text of the EditControl.
    Me.editControl1.IndentSelection()
    'To set the EditControl on focus.
    Me.editControl1.Focus()
End Sub
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles button2.Click
    'Outdents selected text of the EditControl.
    Me.editControl1.OutdentSelection()
    If Not Me.savepoint.IsEmpty Then
       'Gets the current position of the EditControl.
       Me.editControl1.CurrentPosition = savepoint
    End If
    'Moves caret to the beginning of the line. First whitespaces are skipped.
    Me.editControl1.MoveToLineStart()
    'To set the EditControl on focus.
    Me.editControl1.Focus()
    'Reset the selection.
    Me.editControl1.ResetSelection()
End Sub

Samples:

C#: http://www.syncfusion.com/downloads/support/directtrac/139533/EditControl-338292462.zip

VB: http://www.syncfusion.com/downloads/support/directtrac/general/EditControl_VB-655172995.zip

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied