Thursday, April 7, 2016

How to implement ScintillaNET column edit mode

Leave a Comment

I need a text editor control for column edit in my application.

Like notepad++ alt+mouse drag to select a text block , or pull down a long vertical cursor and press a key to insert a char to every lines over cursor.

I tried ScintillaNET but it not suppor the column-modet for insert, just can remove selected text block

I need below effect(notepad++), But ScintillaNET got:

notepad++ ScintillaNET

1 Answers

Answers 1

I found a way to solve. Still using ScintillaNET.

But coding is too complex, any body can give some help?

class SelWrap {     public int Begin { get; set; }     public int Length { get; set; } }  //...  editor.KeyDown += (s, e) => {     // filter alt we will hold down alt to make vertical selection     if (e.Alt) return;      var tb = editor;     if (tb.Selections.Count < 2) return; // no in column mode      e.SuppressKeyPress = true; //block input, handle by below code      //refered from post #5825820     var input = Utility.GetCharFromKey(e.KeyCode).ToString();     if (input == "\0")     {         //SystemSounds.Beep.Play();         return;     }      var array = tb.Selections         .OrderBy(p => p.Start)         .Select(p => new SelWrap{Begin=p.Start, Length=p.End - p.Start })         .ToArray();      //do process every caret(or selection)     for (var i = 0; i < array.Length; i++)     {         var item = array[i];          if (item.Length > 0)          {             //if has selected text, just clean             tb.DeleteRange(item.Begin, item.Length);              for (var j = i + 1; j < array.Length; j++)             {                 array[j].Begin -= item.Length;             }         }          if (input == "\b") //backspace          {             if (item.Length != 0) continue;              //delete a char before caret             tb.DeleteRange(item.Begin - 1, 1);              for (var j = i; j < array.Length; j++)             {                 array[j].Begin--;             }         }         else //just insert that         {             tb.InsertText(item.Begin, input);              for (var j = i; j < array.Length; j++)             {                 array[j].Begin++;             }         }      }      //restore caret status to keep column mode     tb.ClearSelections();     tb.SetSelection(array[0].Begin, array[0].Begin);     for (var i = 1; i < array.Length; i++)     {         var item = array[i];         tb.AddSelection(item.Begin, item.Begin);     } };  //... 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment