具有行号的Windows窗体文本框?

时间:2020-03-06 14:38:17  来源:igfitidea点击:

我正在为我正在编写的应用程序寻找免费的winforms组件。我基本上需要一个在侧栏中包含行号的文本框。能够将其中的数据制成表格也是一大优点。

有人知道可以做到这一点的预制组件吗?

解决方案

看一下SharpDevelop Ccompiler / IDE源代码。他们有一个复杂的带有行号的文本框。我们可以查看源代码,弄清楚他们在做什么,然后自己实现。

这是我所引用的示例:
替代文字http://community.sharpdevelop.net/photos/mattward/images/original/FeatureTourQuickClassBrowser.aspx

在http://www.xtremedotnettalk.com/showthread.php?s=&threadid=49661&highlight=RichTextBox上有一个包含代码的项目。

我们可以使用以下用户/密码登录该站点以下载zip文件:bugmenot / bugmenot

参考Wayne的文章,这里是相关代码。它使用GDI在文本框旁边绘制行号。

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

(在xtremedotnettalk.com论坛上由divil编写的代码。)

.NET的CodePlex中有一个源代码编辑组件,
http://www.codeplex.com/ScintillaNET