Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
SetStyle(ControlStyles.UserPaint, True)
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
SetStyle(ControlStyles.DoubleBuffer, True)
SetStyle(ControlStyles.ResizeRedraw, True)
End Sub
Private Sub RichTextBox1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.SelectionChanged
FindLine()
Invalidate()
End Sub
Private Sub FindLine()
Dim intChar As Integer
intChar = RichTextBox1.GetCharIndexFromPosition(New Point(0, 0))
intLine = RichTextBox1.GetLineFromCharIndex(intChar)
End Sub
Private Sub DrawLines(ByVal g As Graphics, ByVal intLine As Integer)
Dim intCounter As Integer, intY As Integer
g.Clear(Color.Black)
intCounter = intLine + 1
intY = 2
Do
g.DrawString(intCounter.ToString(), Font, Brushes.White, 3, intY)
intCounter += 1
intY += Font.Height + 1
If intY > ClientRectangle.Height - 15 Then Exit Do
Loop
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
DrawLines(e.Graphics, intLine)
End Sub
Private Sub RichTextBox1_VScroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.VScroll
FindLine()
Invalidate()
End Sub
Private Sub RichTextBox1_UserScroll() Handles RichTextBox1.UserScroll
FindLine()
Invalidate()
End Sub
RichTextBox被如下重写:
Public Class UserControl1
Inherits System.Windows.Forms.RichTextBox
Public Event UserScroll()
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = &H115 Then
RaiseEvent UserScroll()
End If
MyBase.WndProc(m)
End Sub
End Class
6条答案
按热度按时间zf2sa74q1#
参考韦恩的帖子,下面是相关代码。它使用GDI在文本框旁边绘制行号。
RichTextBox被如下重写:
(Code由divil在xtremedotnettalk.com论坛上发布。)
yptwkmov2#
看一下SharpDevelop C#编译器/IDE源代码。它们有一个复杂的文本框,上面有行号。您可以查看源代码,弄清楚它们在做什么,然后自己实现它。
下面是我所提到的一个例子:
(来源:sharpdevelop.net)
huus2vyu3#
http://www.xtremedotnettalk.com/showthread.php?s=&threadid=49661&highlight=RichTextBox上有一个项目,其代码可用。
您可以使用user/pass登录站点下载zip文件:布格梅诺特/布格梅诺特
u4dcyp6a4#
CodePlex for .net中有一个源代码编辑组件http://www.codeplex.com/ScintillaNET
3phpmpom5#
这里有一些C#代码可以做到这一点。它基于韦恩引用的www.example.com的代码xtremedotnettalk.com。我做了一些修改,使它实际上显示编辑器文本,而原始代码没有这样做。但是,公平地说,原始代码作者确实提到它需要改进。
下面是代码(编号文本框. cs)
下面是Visual Studio设计器代码(NumberedTextBox.Designer.cs)
ckx4rj1h6#
我猜这取决于字体大小,我使用了“Courier New,9pt,style=bold”
编号文本框.cs
编号文本框.设计器.cs