Public Class Form1
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Integer, ByVal MSG As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Const EM_GETFIRSTVISIBLELINE As Short = &HCES
Private Sub RichTextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
SetLineNo()
End Sub
Private Sub RichTextBox1_VScroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.VScroll
SetLineNo()
End Sub
Private Sub SetLineNo()
'行数のリッチテキストに行番号をふる
Dim row As Integer
row = SendMessage(RichTextBox1.Handle, EM_GETFIRSTVISIBLELINE, 0, 0) + 1
Me.RichTextBox2.Text = row.ToString
Dim i As Integer
Dim lines As String = ""
Do Until i = 60 '一度に表示できる行数が60ぐらいとして設定 モニタによるので 120でも可
If lines = "" Then
lines = GetFormat(i + row)
Else
lines = lines & vbCrLf & GetFormat(i + row)
End If
i = i + 1
Loop
Me.RichTextBox2.Text = lines
End Sub
Private Function GetFormat(ByVal row As Integer) As String
'文字を5桁右寄せにする
Dim i As Integer
i = Len(row.ToString)
Select Case i
Case 1
Return " " & row.ToString
Case 2
Return " " & row.ToString
Case 3
Return " " & row.ToString
Case Else
Return " " & row.ToString
End Select
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.RichTextBox1.WordWrap = False '自動改行不可
Me.RichTextBox2.ReadOnly = True
Me.RichTextBox2.BackColor = Color.White
Me.StartPosition = FormStartPosition.CenterScreen
SetLineNo()
End Sub
End Class
|