Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. dumbed down auto indent/smart indent

dumbed down auto indent/smart indent

Scheduled Pinned Locked Moved C#
help
10 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    simplicitylabs
    wrote on last edited by
    #1

    I have torn the Web apart looking for a solution. The only thing I have found that makes any sense, doesn't work for me. I want to hit the Enter key, read the previous line (the line the cursor just left) and determine how many spaces and/or tabs are before the first character. Then, place the cursor at the same position on the current/new line by adding the correct number of spaces and/or tabs before it including the tabs and/or spaces that may or may not already be there. I know there are controls/classes for sale out there that offer all the cool stuff for smart indenting and syntax highlighting, but I'm not a wealthy man.

        private void richTextBox1\_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                e.Handled = true;
                int pos = richTextBox1.SelectionStart;
                int lineNumber = richTextBox1.GetLineFromCharIndex(pos);
                String currentLineStr = richTextBox1.Lines\[lineNumber\];
    
                int firstChar = 0;
                while (firstChar != currentLineStr.Length)
                {
                    if (!Char.IsWhiteSpace(currentLineStr\[firstChar\])) break;
                    firstChar++;
                }
                String indent = currentLineStr.Substring(0, firstChar);
                richTextBox1.SelectedText = indent;
            }
        }
    

    Thanks in advanced for any help... (I may be asking about syntax highlighting after I get this part worked out, if I can't figure it out.)

    D 1 Reply Last reply
    0
    • S simplicitylabs

      I have torn the Web apart looking for a solution. The only thing I have found that makes any sense, doesn't work for me. I want to hit the Enter key, read the previous line (the line the cursor just left) and determine how many spaces and/or tabs are before the first character. Then, place the cursor at the same position on the current/new line by adding the correct number of spaces and/or tabs before it including the tabs and/or spaces that may or may not already be there. I know there are controls/classes for sale out there that offer all the cool stuff for smart indenting and syntax highlighting, but I'm not a wealthy man.

          private void richTextBox1\_KeyPress(object sender, KeyPressEventArgs e)
          {
              if (e.KeyChar == (char)Keys.Enter)
              {
                  e.Handled = true;
                  int pos = richTextBox1.SelectionStart;
                  int lineNumber = richTextBox1.GetLineFromCharIndex(pos);
                  String currentLineStr = richTextBox1.Lines\[lineNumber\];
      
                  int firstChar = 0;
                  while (firstChar != currentLineStr.Length)
                  {
                      if (!Char.IsWhiteSpace(currentLineStr\[firstChar\])) break;
                      firstChar++;
                  }
                  String indent = currentLineStr.Substring(0, firstChar);
                  richTextBox1.SelectedText = indent;
              }
          }
      

      Thanks in advanced for any help... (I may be asking about syntax highlighting after I get this part worked out, if I can't figure it out.)

      D Offline
      D Offline
      Dave Herren
      wrote on last edited by
      #2

      Try: int lineNumber = richTextBox1.GetLineFromCharIndex(pos-1) ; richTextBox1.Lines[] is a zero index array, but in your example if I press enter on the first line GetLineFromCharIndex returns 1 not 0, so you loop was reading data from the wrong line. According to the documetation GetLineFromCharIndex returns on a zero index as well, so I knew that wasn't the problem. My next thought was that the enter key had been processed, but that didn't make sense because the code is in the keypress event handler. Here is what I finally figured out... You are getting the selection index which is at the end of the line. This puts it one past the last character. Because of this GetLineFromCharIndex seems to consider it on the next line. So if you do GetLineFromCharIndex(pos-1) which gets the character before that, then everything works out.

      topcoderjax - Remember, Google is your friend.

      S 1 Reply Last reply
      0
      • D Dave Herren

        Try: int lineNumber = richTextBox1.GetLineFromCharIndex(pos-1) ; richTextBox1.Lines[] is a zero index array, but in your example if I press enter on the first line GetLineFromCharIndex returns 1 not 0, so you loop was reading data from the wrong line. According to the documetation GetLineFromCharIndex returns on a zero index as well, so I knew that wasn't the problem. My next thought was that the enter key had been processed, but that didn't make sense because the code is in the keypress event handler. Here is what I finally figured out... You are getting the selection index which is at the end of the line. This puts it one past the last character. Because of this GetLineFromCharIndex seems to consider it on the next line. So if you do GetLineFromCharIndex(pos-1) which gets the character before that, then everything works out.

        topcoderjax - Remember, Google is your friend.

        S Offline
        S Offline
        simplicitylabs
        wrote on last edited by
        #3

        Judging from your concise and confident reply, I have to assume you tested your suggestion. That being said, if it's still not working for me after I made your suggested modifications, what am I doing wrong? Could the TextChanged event be interfering? Thank you for your help.

            private void RichTextBox1\_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == (char)Keys.Enter)
                {
                    e.Handled = true;
                    int pos = RichTextBox1.SelectionStart;
                    int lineNumber = RichTextBox1.GetLineFromCharIndex(pos - 1);
                    String currentLineStr = RichTextBox1.Lines\[lineNumber\];
        
                    int firstChar = 0;
                    while (firstChar != currentLineStr.Length)
                    {
                        if (!Char.IsWhiteSpace(currentLineStr\[firstChar\])) break;
                        firstChar++;
                    }
                    String indent = currentLineStr.Substring(0, firstChar);
                    RichTextBox1.SelectedText = indent;
                }
            }
        

        -- modified at 1:23 Saturday 26th May, 2007

        D 1 Reply Last reply
        0
        • S simplicitylabs

          Judging from your concise and confident reply, I have to assume you tested your suggestion. That being said, if it's still not working for me after I made your suggested modifications, what am I doing wrong? Could the TextChanged event be interfering? Thank you for your help.

              private void RichTextBox1\_KeyPress(object sender, KeyPressEventArgs e)
              {
                  if (e.KeyChar == (char)Keys.Enter)
                  {
                      e.Handled = true;
                      int pos = RichTextBox1.SelectionStart;
                      int lineNumber = RichTextBox1.GetLineFromCharIndex(pos - 1);
                      String currentLineStr = RichTextBox1.Lines\[lineNumber\];
          
                      int firstChar = 0;
                      while (firstChar != currentLineStr.Length)
                      {
                          if (!Char.IsWhiteSpace(currentLineStr\[firstChar\])) break;
                          firstChar++;
                      }
                      String indent = currentLineStr.Substring(0, firstChar);
                      RichTextBox1.SelectedText = indent;
                  }
              }
          

          -- modified at 1:23 Saturday 26th May, 2007

          D Offline
          D Offline
          Dave Herren
          wrote on last edited by
          #4

          First, how is it not working? Second, I would suggest that you put break points in the keypress event, change event and any other relevant code. Watch it execute line by line... which is what I did. If necessary comment out the change event code and any other relevant code and execute without it. I copied your code above and tried it again and it still works, so something else is going on.

          topcoderjax - Remember, Google is your friend.

          S 1 Reply Last reply
          0
          • D Dave Herren

            First, how is it not working? Second, I would suggest that you put break points in the keypress event, change event and any other relevant code. Watch it execute line by line... which is what I did. If necessary comment out the change event code and any other relevant code and execute without it. I copied your code above and tried it again and it still works, so something else is going on.

            topcoderjax - Remember, Google is your friend.

            S Offline
            S Offline
            simplicitylabs
            wrote on last edited by
            #5

            I placed the code into a new project with nothing but the form and the richTextBox. It still didn't work. There are no errors, however, when I run the app nothing happens when I hit Enter -- I.E. no spaces and/or tabs are added before the cursor on the next line. Is there another way to do this?

            D 1 Reply Last reply
            0
            • S simplicitylabs

              I placed the code into a new project with nothing but the form and the richTextBox. It still didn't work. There are no errors, however, when I run the app nothing happens when I hit Enter -- I.E. no spaces and/or tabs are added before the cursor on the next line. Is there another way to do this?

              D Offline
              D Offline
              Dave Herren
              wrote on last edited by
              #6

              I enabled tabs ont the control. Ran the app and tabbed over once and typed text. Pressing enter took me to a new line and inserted the tab. Pressing enter in the middle of my text moved the remaining text after my position to the next line with tabs. I'm in 1.1 framework, don't know if it matters. Did not try pressing enter without any text or pressing enter at the very beginning of a line. Will look at the project again tonight when I get the chance and see if anything else comes to mind and if not may post the project code.

              topcoderjax - Remember, Google is your friend. Try this Custom Google Code Search

              S 1 Reply Last reply
              0
              • D Dave Herren

                I enabled tabs ont the control. Ran the app and tabbed over once and typed text. Pressing enter took me to a new line and inserted the tab. Pressing enter in the middle of my text moved the remaining text after my position to the next line with tabs. I'm in 1.1 framework, don't know if it matters. Did not try pressing enter without any text or pressing enter at the very beginning of a line. Will look at the project again tonight when I get the chance and see if anything else comes to mind and if not may post the project code.

                topcoderjax - Remember, Google is your friend. Try this Custom Google Code Search

                S Offline
                S Offline
                simplicitylabs
                wrote on last edited by
                #7

                I'm using Vista Home Premium, Visual Studio 2005 and .Net 3.0.

                D 1 Reply Last reply
                0
                • S simplicitylabs

                  I'm using Vista Home Premium, Visual Studio 2005 and .Net 3.0.

                  D Offline
                  D Offline
                  Dave Herren
                  wrote on last edited by
                  #8

                  Ok I tried it in 2.0 XP. Code still worked. Best of luck.

                  topcoderjax - Remember, Google is your friend. Try this Custom Google Code Search

                  S 1 Reply Last reply
                  0
                  • D Dave Herren

                    Ok I tried it in 2.0 XP. Code still worked. Best of luck.

                    topcoderjax - Remember, Google is your friend. Try this Custom Google Code Search

                    S Offline
                    S Offline
                    simplicitylabs
                    wrote on last edited by
                    #9

                    One for the "duh" files. I figured out why it wasn't working for me. I hadn't added the KeyPress event to my richTexBox control. Thanks for your patience and help.

                    D 1 Reply Last reply
                    0
                    • S simplicitylabs

                      One for the "duh" files. I figured out why it wasn't working for me. I hadn't added the KeyPress event to my richTexBox control. Thanks for your patience and help.

                      D Offline
                      D Offline
                      Dave Herren
                      wrote on last edited by
                      #10

                      No worries... happens to everyone from time to time. Glad I could help.

                      topcoderjax - Remember, Google is your friend. Try this Custom Google Code Search

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups