dumbed down auto indent/smart indent
-
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.)
-
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.)
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.
-
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.
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
-
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
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.
-
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.
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?
-
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?
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
-
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
I'm using Vista Home Premium, Visual Studio 2005 and .Net 3.0.
-
I'm using Vista Home Premium, Visual Studio 2005 and .Net 3.0.
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
-
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
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.
-
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.
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