Click to See Complete Forum and Search --> : auto indentation feature


kumaru_san
April 21st, 2004, 02:20 AM
Hello

Can anybody help me how to add
auto indentation feature.

I mena when i type the code in the Richtextbox the code
should be typed in the following format.

if()
{
MessageBox.Show("test");

while()
{
i++;
}

}

else
{
....
}


I guess KeyDown event ..
and SelectIndent property...its not working according to my expectation.

I need the logic only.

Please

:confused:

kasracer
April 21st, 2004, 02:24 AM
I implimented this in my syntax editor

Basically, get the current line the user is on when they press enter. Substract 1 and count the white space until you come accross something that isn't white space (so if someone makes 2 tabs, a word and another tab, you'll only count 2).

Then simply insert 2 tabs on the line the user is currently on

SendKeys works well with sending white space

kumaru_san
April 21st, 2004, 04:05 AM
hello
can you send me the code snippet?
i am also developing a syntax editor.
but i am new to C#.
Can u help me ..please?

kasracer
April 21st, 2004, 02:20 PM
Use the KeyPress event and check for the enter key. if it's pressed, convert the previous line into a string
temp = Convert.ToString(this.Lines.GetValue(this.GetLineFromCharIndex(this.SelectionStart - 1)));


Use a for loop to go until the index equals temp.Length and go through each character of the string until you reach something other than a tab (and/or space depending on how you want to do indenting) and break the loop. Remember, in the loop, count how many tabs it encountered.

Then use System.Windows.Forms.SendKeys.Send() and send in as many tabs (or spaces) that you encountered.

I didn't want to just you the entire piece of source because I think it's best when a person can figure something out on their own. I basically gave you everything you need to do this.

kumaru_san
April 22nd, 2004, 12:28 AM
thanks.
i will do it further.