📄 文本框的行统计和读指定行.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "计算TextBox的行数(不使用TextBox的消息)"
ClientHeight = 3120
ClientLeft = 60
ClientTop = 345
ClientWidth = 5895
LinkTopic = "Form1"
ScaleHeight = 3120
ScaleWidth = 5895
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton Command2
Caption = "读取第21行"
Height = 495
Left = 4320
TabIndex = 2
Top = 1080
Width = 1335
End
Begin VB.CommandButton Command1
Caption = "计算行数"
Height = 495
Left = 4320
TabIndex = 1
Top = 120
Width = 1335
End
Begin VB.TextBox Text1
BeginProperty Font
Name = "MS Serif"
Size = 12
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 2775
Left = 240
MultiLine = -1 'True
ScrollBars = 3 'Both
TabIndex = 0
Text = "文本框的行统计和读指定行.frx":0000
Top = 120
Width = 3975
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Function TextBoxLineCount(txt As TextBox) As Long
Dim S As String, pos As Long
TextBoxLineCount = 0
pos = 1
S = txt.Text
Do
TextBoxLineCount = TextBoxLineCount + 1
pos = InStr(pos, S, vbCrLf)
If pos > 0 Then pos = pos + 2
Loop Until pos = 0
End Function
Function TextBoxGetLine(txt As TextBox, ByVal N As Long) As Variant
Dim S As String, pos As Long, pos2 As Long, LineIndex As Long
TextBoxGetLine = Null
LineIndex = 0
pos = 1
S = txt.Text
Do
If LineIndex = N Then
pos2 = InStr(pos, S, vbCrLf)
If pos2 > 0 Then
TextBoxGetLine = Mid(S, pos, pos2 - pos)
Else
TextBoxGetLine = Mid(S, pos)
End If
Else
pos = InStr(pos, S, vbCrLf)
If pos > 0 Then pos = pos + 2
End If
LineIndex = LineIndex + 1
Loop Until pos = 0
End Function
Private Sub Command1_Click()
MsgBox "行数=" & TextBoxLineCount(Text1)
End Sub
Private Sub Command2_Click()
MsgBox TextBoxGetLine(Text1, 21), , "第21行"
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -