2010-04-02 5 views
8

코드 강조 표시로 간단한 텍스트 및 스크립트 편집기를 만들고 있습니다. 그 때문에 나는 RichTextBox를 사용한다. 하지만 VS 나 메모장 ++ 같은 줄의 숫자를 왼쪽에 표시하는 방법을 모르겠습니다. 어떤 해결책이 있습니까?RichTextBox에서 줄 번호를 표시하는 방법 C#

+1

체크 아웃 코드 프로젝트에이 프로젝트를 - http://www.codeproject.com/KB/edit/numberedtextbox.aspx –

+0

은 아마 당신은 동일한 제어를해야 메모장 + +로. 이것은 Scintilla.Net입니다 : http://scintillanet.codeplex.com/ – Oliver

답변

16

.

그래서 줄 번호를 표시하는 다른 RichTextBoxEx를 만들었습니다.

줄 번호 매기기를 켜거나 끌 수 있습니다. 그것은 빠르다. 그것은 깔끔하게 스크롤됩니다. 숫자의 색, 그라디언트의 배경색, 테두리 두께, 글꼴, 선행 0 사용 여부를 선택할 수 있습니다. '표시된대로'줄 번호를 매기거나 RTB의 단단한 개행 문자에 따라 번호를 매길 수 있습니다.

예 : 그것은 한계가있다

alt text http://i39.tinypic.com/13zcoz6.jpg

alt text http://i43.tinypic.com/wml2z9.jpg

alt text http://i39.tinypic.com/25i4x3o.jpg

: 그것은 단지 왼쪽에 번호를 표시합니다. 당신이 돌보면 너무 많은 노력없이 그것을 바꿀 수 있습니다.

코드는 C# 프로젝트로 설계되었습니다. 이 도구는 더 큰 "솔루션"(XPath 시각화 도구)의 일부이지만 사용자 지정 RichTextBox는 분리 가능한 어셈블리로 패키지되어 새 프로젝트에서 사용할 준비가되었습니다. Visual Studio에서 DLL에 대한 참조를 추가하면 디자인 화면에 끌어다 놓을 수 있습니다. 더 큰 솔루션에서 다른 코드를 버릴 수 있습니다.

See the code

-1

사용자가 직접 컨트롤을 그릴 수 있습니다. 다음은 자신을 그리는 방법의 예입니다. link

0

richtextbox에 게시 할 메소드가있는 클래스에 각 행을 저장합니다. 이 방법에서는 클래스의 위치에 따라 줄 번호를 추가 할 수 있습니다. 예를 들어

(매우 약) : 나는 다른 곳에서 참조 CodeProject의 기사에서 코드를 다시 사용하지만, 내가 바라 보았다 모든 옵션이 조금 너무 미봉책 보였다 시도

class myText 
{ 
    public List<string> Lines; 

    public string GetList() 
    { 
     StringBuilder sb = new StringBuilder(); 
     int i = 0; 
     foreach (string s in Lines) 
     { 
      sb.AppendFormat("{0}: {1}", i, s).AppendLine(); 
      i++; 
     } 
     return sb.ToString(); 
    } 
} 
0

Scintilla.Net http://scintillanet.codeplex.com/는 사용자의 요구에 가장 실현 가능한 해결책이 될 수 있습니다. 그러나 내 프로젝트에서는 Cheeso (XPath 비주얼 라이저의 RichTextBoxEx)에서 제안한 솔루션을 사용했습니다. 매우 크지 않은 문서에 대해 간단하고 빠릅니다. 인터넷의 다른 모든 .net 구성 요소는 매우 느립니다.

-1

간단한 방법 :

Dim myArray = RichTextBox1.Text.Split() 

Dim cnt As Integer = 0 
RichTextBox1.Clear() 

Do While cnt < myArray.Count 
    RichTextBox1.AppendText(cnt & ":" & myArray(cnt) & vbNewLine) 
    cnt = cnt + 1 
Loop 
0
public int getWidth() 
    { 
     int w = 25; 
     // get total lines of richTextBox1 
     int line = richTextBox1.Lines.Length; 

     if (line <= 99) 
     { 
      w = 20 + (int)richTextBox1.Font.Size; 
     } 
     else if (line <= 999) 
     { 
      w = 30 + (int)richTextBox1.Font.Size; 
     } 
     else 
     { 
      w = 50 + (int)richTextBox1.Font.Size; 
     } 

     return w; 
    } 

    public void AddLineNumbers() 
    { 
     // create & set Point pt to (0,0) 
     Point pt = new Point(0, 0); 
     // get First Index & First Line from richTextBox1 
     int First_Index = richTextBox1.GetCharIndexFromPosition(pt); 
     int First_Line = richTextBox1.GetLineFromCharIndex(First_Index); 
     // set X & Y coordinates of Point pt to ClientRectangle Width & Height respectively 
     pt.X = ClientRectangle.Width; 
     pt.Y = ClientRectangle.Height; 
     // get Last Index & Last Line from richTextBox1 
     int Last_Index = richTextBox1.GetCharIndexFromPosition(pt); 
     int Last_Line = richTextBox1.GetLineFromCharIndex(Last_Index); 
     // set Center alignment to LineNumberTextBox 
     LineNumberTextBox.SelectionAlignment = HorizontalAlignment.Center; 
     // set LineNumberTextBox text to null & width to getWidth() function value 
     LineNumberTextBox.Text = ""; 
     LineNumberTextBox.Width = getWidth(); 
     // now add each line number to LineNumberTextBox upto last line 
     for (int i = First_Line; i <= Last_Line + 2; i++) 
     { 
      LineNumberTextBox.Text += i + 1 + "\n"; 
     } 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     LineNumberTextBox.Font = richTextBox1.Font; 
     richTextBox1.Select(); 
     AddLineNumbers(); 
    } 

    private void richTextBox1_SelectionChanged(object sender, EventArgs e) 
    { 
     Point pt = richTextBox1.GetPositionFromCharIndex(richTextBox1.SelectionStart); 
     if (pt.X == 1) 
     { 
      AddLineNumbers(); 
     } 
    } 

    private void richTextBox1_VScroll(object sender, EventArgs e) 
    { 
     LineNumberTextBox.Text = ""; 
     AddLineNumbers(); 
     LineNumberTextBox.Invalidate(); 
    } 

    private void richTextBox1_TextChanged(object sender, EventArgs e) 
    { 
     if (richTextBox1.Text == "") 
     { 
      AddLineNumbers(); 
     } 
    } 

    private void richTextBox1_FontChanged(object sender, EventArgs e) 
    { 
     LineNumberTextBox.Font = richTextBox1.Font; 
     richTextBox1.Select(); 
     AddLineNumbers(); 
    } 

    private void LineNumberTextBox_MouseDown(object sender, MouseEventArgs e) 
    { 
     richTextBox1.Select(); 
     LineNumberTextBox.DeselectAll(); 
    } 

    private void Form1_Resize(object sender, EventArgs e) 
    { 
     AddLineNumbers(); 
    } 
관련 문제