)
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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: EditControl |
Published Date: April 6, 2015 |
Last Revised Date: December 11, 2019

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

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
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
Live Chat Icon