Public Class NumberedTextBox ' "行番号付きテキストボックス" ' by yamagata21, 2007.09- Private pls As Integer = -1, num As Integer = -1 ', lp As Integer = -1 Private img As Bitmap = Nothing Private Sub NumberedTextBox_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load '単一フォントに設定 RichTextBox1.LanguageOption = RichTextBoxLanguageOptions.UIFonts End Sub Private Sub constCalc() '表示行数や行間などを取得 pls = RichTextBox1.Font.Height - 1 num = (RichTextBox1.ClientRectangle.Height) / pls img = New Bitmap(PictureBox1.Width, PictureBox1.Height) End Sub Private Sub drawNumber() '行番号の描画 With RichTextBox1 Dim fp As Integer = .GetCharIndexFromPosition(New Point(0, 0)) Dim fl As Integer = .GetLineFromCharIndex(fp) Dim p As New Point(-5, .GetPositionFromCharIndex(.GetFirstCharIndexFromLine(fl)).Y) 'If lp = p.Y Then Exit Sub 'lp = p.Y Using g As System.Drawing.Graphics = Graphics.FromImage(img) g.Clear(Color.White) For l As Integer = fl To fl + num 'Dim p As System.Drawing.Point = .GetPositionFromCharIndex(.GetFirstCharIndexFromLine(l)) g.DrawString(String.Format("{0,7}", l + 1), .Font, Brushes.Black, p) p.Y += pls Next PictureBox1.Image = img End Using End With End Sub Public Shadows Event KeyPress As EventHandler Private Sub RichTextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles RichTextBox1.KeyPress 'KeyPressイベント RaiseEvent KeyPress(sender, e) End Sub Public Shadows Event KeyUp As EventHandler Private Sub RichTextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyUp 'KeyUpイベント RaiseEvent KeyUp(sender, e) End Sub Public Shadows Event TextChanged As EventHandler Private Sub RichTextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged 'TextChangedイベント Call drawNumber() RaiseEvent TextChanged(sender, e) End Sub Public Shadows Event KeyDown As EventHandler Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown 'KeyDownイベント If e.Control AndAlso e.KeyCode = Keys.V Then '「貼り付け」はテキスト形式で取得 RichTextBox1.SelectedText = Clipboard.GetText e.Handled = True End If RaiseEvent KeyDown(sender, e) End Sub Private Sub ChangeEvents1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _ RichTextBox1.Resize, RichTextBox1.FontChanged '行間などの再計算+行番号の描画呼び出し Call constCalc() Call drawNumber() End Sub Private Sub RichTextBox1_VScroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.VScroll '行番号の描画呼び出し Call drawNumber() End Sub Public Overrides Property Text() As String 'Textプロパティ Get Return RichTextBox1.Text End Get Set(ByVal value As String) RichTextBox1.Text = value End Set End Property Public Property SelectedText() As String 'SelectedTextプロパティ Get Return RichTextBox1.SelectedText End Get Set(ByVal value As String) RichTextBox1.SelectedText = value End Set End Property Public Property SelectionStart() As Integer 'SelectionStartプロパティ Get Return RichTextBox1.SelectionStart End Get Set(ByVal value As Integer) RichTextBox1.SelectionStart = value End Set End Property Public Property SelectionLength() As Integer 'SelectionLengthプロパティ Get Return RichTextBox1.SelectionLength End Get Set(ByVal value As Integer) RichTextBox1.SelectionLength = value End Set End Property Public Property SelectionColor() As Color 'SelectionColorプロパティ Get Return RichTextBox1.SelectionColor End Get Set(ByVal value As Color) RichTextBox1.SelectionColor = value End Set End Property Public Property SelectionBackColor() As Color 'SelectionBackColorプロパティ Get Return RichTextBox1.SelectionBackColor End Get Set(ByVal value As Color) RichTextBox1.SelectionBackColor = value End Set End Property Public Property [ReadOnly]() As Boolean 'ReadOnlyプロパティ Get Return RichTextBox1.ReadOnly End Get Set(ByVal value As Boolean) RichTextBox1.ReadOnly = value End Set End Property Public Sub SelectAll() 'SelectAllメソッド RichTextBox1.SelectAll() End Sub Private Class MyRichTextBox : Inherits RichTextBox Private Const WM_PASTE As Integer = &H302 Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) If m.Msg = WM_PASTE Then Me.SelectedText = Clipboard.GetText Return End If MyBase.WndProc(m) End Sub End Class End Class _ Partial Class NumberedTextBox Inherits System.Windows.Forms.UserControl _ Protected Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso components IsNot Nothing Then components.Dispose() End If MyBase.Dispose(disposing) End Sub Private components As System.ComponentModel.IContainer _ Private Sub InitializeComponent() Me.RichTextBox1 = New System.Windows.Forms.RichTextBox Me.PictureBox1 = New System.Windows.Forms.PictureBox CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit() Me.SuspendLayout() ' 'RichTextBox1 ' Me.RichTextBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.RichTextBox1.BorderStyle = System.Windows.Forms.BorderStyle.None Me.RichTextBox1.Font = New System.Drawing.Font("MS ゴシック", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte)) Me.RichTextBox1.HideSelection = False Me.RichTextBox1.Location = New System.Drawing.Point(50, 0) Me.RichTextBox1.MinimumSize = New System.Drawing.Size(10, 10) Me.RichTextBox1.Name = "RichTextBox1" Me.RichTextBox1.Size = New System.Drawing.Size(408, 278) Me.RichTextBox1.TabIndex = 1 Me.RichTextBox1.Text = "" Me.RichTextBox1.WordWrap = False ' 'PictureBox1 ' Me.PictureBox1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles) Me.PictureBox1.BackColor = System.Drawing.SystemColors.Window Me.PictureBox1.Location = New System.Drawing.Point(0, 0) Me.PictureBox1.MinimumSize = New System.Drawing.Size(10, 10) Me.PictureBox1.Name = "PictureBox1" Me.PictureBox1.Size = New System.Drawing.Size(49, 278) Me.PictureBox1.TabIndex = 2 Me.PictureBox1.TabStop = False ' 'NumberedTextBox ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.BackColor = System.Drawing.Color.Black Me.Controls.Add(Me.PictureBox1) Me.Controls.Add(Me.RichTextBox1) Me.Name = "NumberedTextBox" Me.Size = New System.Drawing.Size(458, 278) CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit() Me.ResumeLayout(False) End Sub Private WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox Private WithEvents PictureBox1 As System.Windows.Forms.PictureBox End Class