퇴근5분전

 이번 AsIs Function Connection 프로그램 구현 중... 

텍스트 입력기반으로 만들었기에... 편의기능을 추가했다. 

생각지도 않았던 Alt + Up, Alt + Down 으로 VS 편집기처럼 선택한 행들의 위치를 변경하는 것을 구현하게 되었다. 

인텔리센스를 구현하려니... 참 걸리는게 많네.. 대충 흉내만 냈다. 

 

Textbox_KeyDown Event에서!!

 // txtSource : TextBox 임 

 // !!! 주의 : 프로퍼티에 WrapWord = false; 로 작업해야 함. 

 // 이유 : textbox의 내용이 길때 textbox 크기가 작아지면서 박스안에 글이 구겨들어간다. 이때 커서의 현재 위치 정보를 제대로 읽을 수 없었다.

 

            if (e.Alt && (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down))
            { 
                int selStartIndex = txtSource.SelectionStart;
                int selLength = txtSource.SelectionLength;
                int selLineCnt = 1 + txtSource.SelectedText.Count(c => c == '\n');
                int currentlineIndex = txtSource.GetLineFromCharIndex(selStartIndex);
                List<string> lines = new List<string>(txtSource.Lines);

                if (e.KeyCode == Keys.Up && 0 <= (currentlineIndex -1 ))
                { 
                    string moveString = lines[currentlineIndex - 1]; 
                    lines.RemoveAt(currentlineIndex - 1);
                    lines.Insert(currentlineIndex + selLineCnt - 1, moveString);
                    int offset = moveString.Length;                    
                    txtSource.Text = string.Join(Environment.NewLine, lines);
                    lines.Clear();
                    txtSource.SelectionStart = selStartIndex - offset -2;
                    txtSource.SelectionLength = selLength;
                }
                else if (e.KeyCode == Keys.Down && (currentlineIndex+ selLineCnt) < lines.Count)
                {
                    string moveString = lines[currentlineIndex + selLineCnt];                    
                    lines.RemoveAt(currentlineIndex + selLineCnt);
                    lines.Insert(currentlineIndex, moveString);
                    int offset = moveString.Length;
                    txtSource.Text = string.Join(Environment.NewLine, lines);
                    lines.Clear();
                    txtSource.SelectionStart = selStartIndex + offset +2;
                    txtSource.SelectionLength = selLength;
                }
            }

 

 

구현 로직 설명