1 Replies - 2426 Views - Last Post: 26 October 2012 - 07:57 AM Rate Topic: -----

#1 whyte_rhyno   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-October 12

Indent next line in RichTextBox based on previous line

Posted 26 October 2012 - 06:02 AM

Hello all,

Firstly, thank you for this great forum; I have often come across this site when Googlin' and it has always provided me with great solutions. However, after many hours of searching and playing around with code I have become stuck.

I am building a simplistic IDE for an arbitrary language which auto-indents (TABs) based on the previous line. For example,

if true then
     //this line auto-indents because we are in a selection statement
     if true then
          //this line also auto-indents
     endif
     //indenting goes back to here now, as we're still in first statement
endif
//indenting back to normal




For as long as I've been working on this, I have very little code to show for it I would be extremely grateful if you could point me in the right direction as to how to achieve this. I know people have written auto-indenting RichTextBoxes before, but I would like to code my own purely for educational purposes.


The code below is in a class which inherits a RichTextBox, hence why I'm calling, for example, Selectionstart instead of RichTextBox1.Selectionstart.

 protected override void onkeydown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                int index = Selectionstart;
                int curPos = GetLineFromCharIndex(index);
                String lineText = Lines[curPos];
                if (lineText.Contains("then")) //just an example, could point to an array/regex of keywords
                {

                    //tab the next line
                }
            }
            base.onkeydown(e);
        }




Thank you for your time.

Is This A Good Question/Topic? 0
  • +

Replies To: Indent next line in RichTextBox based on previous line

#2 sithius92   User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 164
  • Joined: 01-August 08

Re: Indent next line in RichTextBox based on previous line

Posted 26 October 2012 - 07:57 AM

I have never tried anything like this before, but here's my attempt:

Add a new string with a tab character (or spaces, depending on your requirements) at the beginning to the Lines[] array after the current index (this will represent the new line that is created when the return key is pressed).

This is crude, but maybe someone else can expand on it or provide a better solution.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1