本文介绍了Windows窗体的文本框,有行号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个免费的WinForms组件我写的应用程序。我basicly需要的是包含行号在侧栏中的文本框。如果能够制表在它的数据将是一个重大利好了。

I'm looking for a free winforms component for an application I'm writing. I basicly need a textbox that contains line numbers in a side column. Being able to tabulate data within it would be a major plus too.

有谁知道premade组件,可以做到这一点?

Does anyone know of a premade component that could do this?

推荐答案

引用Wayne's帖子,这里是有关code。它使用GDI绘制行号旁边的文本框。

Referencing Wayne's post, here is the relevant code. It is using GDI to draw line numbers next to the text box.

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的是像这样覆盖:

The RichTextBox is overridden like this:

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

(code。通过divil在xtremedotnettalk.com论坛。)

(Code by divil on the xtremedotnettalk.com forum.)

这篇关于Windows窗体的文本框,有行号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:27