2009-03-18 12 views
11

Bir satır numarası ekleme özelliğini entegre etmek istediğim çok satırlı bir richtextbox denetimi var. i birçok yaklaşımı Zengin metin kutusuna satır numarasını görüntüleme C#

  1. etiket ekleme ve satır sayısı olarak satır numaralarını güncellenmesi
  2. üzerine dize çizmek için birlikte Picturebox ekle değiştirir

    kabul var.
  3. Başka bir metin kutusu ile birlikte ve üzerindeki çizgi numaralarını ekleyin
  4. Liste kutusu boyunca liste satırını ekleyin ve satır numaralarını görüntüleyin.

İki şüphem var.

  1. Kullanmakta olduğum richtextbox bir özel denetimdir ve RichTextBox sınıfından türetilmiştir. Buna nasıl birden çok kontrol ekleyebilirim?
  2. C#
+0

Sen görmek için bu makalelerde bakmak olabilir: [RichTextBox için LineNumbers] (http://www.codeproject.com/KB/cpp/linenumbers_for_rtb.aspx) [ .NET 2.0'da RichTextBox'ın numaralandırma satırları] (http://www.codeproject.com/KB/edit/numberedtextbox.aspx) – Stormenet

+0

İşte C# kullanarak RichTextBox için Satır Numaraları Oluşturma Blogu - [C# içindeki RichTextBox için Çizgi Numaraları Oluşturma] (http://www.c-sharpcorner.com/blogs/creating-line-numbers-for-richtextbox-in-c-sharp) –

cevap

3

Kendi örnek içinde satırlı metin için satır numaralarını göstermek için en iyi yaklaşım nedir. Tüm iyi, ama wordwrap devre dışı bırakılmalıdır :(

RTB benim richtextbox ve tB sonraki RTB

için metin kutusu olan
int maxLC = 1; //maxLineCount - should be public 
    private void rTB_KeyUp(object sender, KeyEventArgs e) 
    { 
     int linecount = rTB.GetLineFromCharIndex(rTB.TextLength) + 1; 
     if (linecount != maxLC) 
     { 
      tB_line.Clear(); 
      for (int i = 1; i < linecount+1; i++) 
      { 
       tB_line.AppendText(Convert.ToString(i) + "\n"); 
      } 
      maxLC = linecount; 
     } 
    } 

bu kod bana dönüştürmek için gerekli, teşekkür ederim yardımcı

2

JT jr temel görsel ama olabilir:

Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp 
    Dim maxlc As Integer = 1 
    Dim linecount As Integer = TextBox1.GetLineFromCharIndex(TextBox1.Height) + 1 

    If linecount <> maxlc Then 
     TextBox2.Clear() 
     For i = 0 To linecount - 1 Step 1 
      TextBox2.AppendText(Convert.ToString(i) + vbNewLine) 
     Next i 
     maxlc = linecount 
    End If 
End Sub 
1

100% ÇALIŞIYOR !!! ama ListB gibi diğer forma değiştirmek istiyorsanız, satır numaraları için richTextBox2 eklemem gerekiyor öküz, yine de bana iyi hizmet etti. bunu hayata nasıl

private void richTextBox1_keyDown(object sender, KeyEventArgs e) 
    { 

     for (int i = 0; i <= richTextBox1.Lines.Count(); i++) 
     { 
      if (!(e.KeyCode == Keys.Back)) 
      { 
       if (!richTextBox2.Text.Contains(i.ToString())) 
       { 
        richTextBox2.Text += i.ToString() + "\n"; 
       } 
      } 
      else 
      { 
       richTextBox2.Clear(); 
      } 
     }  
    } 
1
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(); 
    }