Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1017731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:44:57+00:00 2026-05-16T10:44:57+00:00

this is how i implemented Shift-Tab or decrease indent… the result on screenr if

  • 0

this is how i implemented Shift-Tab or decrease indent… the result on screenr

if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift && e.Key == Key.Tab)
{
    // Shift+Tab
    int selStart = txtEditor.SelectionStart;
    int selLength = txtEditor.SelectionLength;
    string selText = txtEditor.SelectedText;
    string text = txtEditor.Text;

    // find new lines that are followed by 1 or more spaces
    Regex regex = new Regex(Environment.NewLine + @"(\s+)");
    Match m = regex.Match(selText);
    string spaces;
    while (m.Success)
    {
        GroupCollection grps = m.Groups;
        spaces = grps[1].Value;
        int i = 0;
        // remove 1 space on each loop to a max of 4 spaces
        while (i < 4 && spaces.Length > 0)
        {
            spaces = spaces.Remove(0, 1);
            i++;
        }
        // update spaces in selText
        selText = selText.Remove(grps[1].Index, grps[1].Length).Insert(grps[1].Index, spaces);

        m = regex.Match(selText, grps[1].Index + spaces.Length);
    }

    // commit changes to selText to text 
    text = text.Remove(selStart, selLength).Insert(selStart, selText);

    // decrease indent of 1st line
    // - find 1st character of selection
    regex = new Regex(@"\w");
    m = regex.Match(text, selStart);
    int start = selStart;
    if (m.Success) {
        start = m.Index;
    }
    // - start search for spaces 
    regex = new Regex(Environment.NewLine + @"(\s+)", RegexOptions.RightToLeft);
    m = regex.Match(text, start);
    if (m.Success) {
        spaces = m.Groups[1].Value;
        int i = 0;
        while (i < 4 && spaces.Length > 0) {
            spaces = spaces.Remove(0, 1); // remove 1 space
            i++;
        }
        text = text.Remove(m.Groups[1].Index, m.Groups[1].Length).Insert(m.Groups[1].Index, spaces);
        selStart = m.Groups[1].Index;
    }

    txtEditor.Text = text;
    txtEditor.SelectionStart = selStart;
    txtEditor.SelectionLength = selText.Length;
    e.Handled = true;
}

the code looks messy and i wonder if theres a better way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:44:58+00:00Added an answer on May 16, 2026 at 10:44 am

    Personally, I wouldn’t use Regex for this.

    Untested, probably needs modification:

    public static class StringExtensions
    {
     // Removes leading white-spaces in a string up to a maximum
     // of 'level' characters
     public static string ReduceIndent(this string line, int level)
     { 
       // Produces an IEnumerable<char> with the characters 
       // of the string verbatim, other than leading white-spaces
       var unindentedChars = line.SkipWhile((c, index) => char.IsWhiteSpace(c) && index < level);
    
       return new string(unindentedChars.ToArray());
     }
    
    
     // Applies a transformation to each line of a string and returns the
     // transformed string
     public static string LineTransform(this string text, Func<string,string> transform)
     {
       //Splits the string into an array of lines
       var lines = text.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
    
       //Applies the transformation to each line
       var transformedLines = lines.Select(transform);
    
       //Joins the transformed lines into a new string
       return string.Join(Environment.NewLine, transformedLines.ToArray());
     } 
    }
     ... 
    
    if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift && e.Key == Key.Tab)
    {                             
      // Reduces the indent level of the selected text by applying the
      // 'ReduceIndent' transformation to each line of the text.
      string replacement = txtEditor.SelectedText
                                    .LineTransform(line => line.ReduceIndent(4));
    
      int selStart = txtEditor.SelectionStart;
      int selLength = txtEditor.SelectionLength;
    
      txtEditor.Text = txtEditor.Text
                                .Remove(selStart, selLength)
                                .Insert(selStart, replacement);
    
      txtEditor.SelectionStart = selStart;
      txtEditor.SelectionLength = replacement.Length;
      e.Handled = true;
    }   
    

    EDIT:

    Added comments to the code as per the request of the OP.

    For more info:

    • Extension Methods
    • Func<T, TResult> delegate
    • Enumerable.SkipWhile extension method
    • Lambda Expressions
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 513k
  • Answers 513k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You shouldn't do that. There is not a thing called… May 16, 2026 at 5:58 pm
  • Editorial Team
    Editorial Team added an answer According to the documentation, boolValue "returns YES on encountering one… May 16, 2026 at 5:58 pm
  • Editorial Team
    Editorial Team added an answer For the first string you can use this regular expression:… May 16, 2026 at 5:58 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

With help from the community last week, I successfully implemented this code here Now
I have implemented a marquee text widget using Qt4. I painted the text content
From this question here , I was writing an enum wrapper to have some
I was reading this document: http://www.fadden.com/techmisc/hdc/lesson11.htm In it he stated: Problem is, we don't
How can the XOR operation (on two 32 bit ints) be implemented using only
Is there ever reason to think the >> (signed) and >>> (unsigned) right bit-shift
I'm not asking for anyone to do this homework for me, but I bring
I have a UserControl that consists of three TextBoxes. On a form I can
i tried the code below to convert it to c#. But i am getting
I am seeing an intriguing situation rounding Currency in C# (VS 2008 SP1). Below

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.