form1.vb
来自「Visual.Basic.NET实用编程百例-47.6M.zip」· VB 代码 · 共 152 行
VB
152 行
Imports System.Windows.Forms.RichTextBoxFinds
Public Class Form1
Inherits System.Windows.Forms.Form
Private i As Integer
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.Button1 = New System.Windows.Forms.Button
Me.RichTextBox1 = New System.Windows.Forms.RichTextBox
Me.SuspendLayout()
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(144, 232)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(80, 24)
Me.Button2.TabIndex = 0
Me.Button2.Text = "查找"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(248, 232)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(88, 24)
Me.Button3.TabIndex = 1
Me.Button3.Text = "查找下一个"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(112, 16)
Me.Label1.TabIndex = 2
Me.Label1.Text = "输入要查找的字符"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(120, 0)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(96, 21)
Me.TextBox1.TabIndex = 3
Me.TextBox1.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(48, 232)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 5
Me.Button1.Text = "打开文件"
'
'RichTextBox1
'
Me.RichTextBox1.AutoWordSelection = True
Me.RichTextBox1.HideSelection = False
Me.RichTextBox1.Location = New System.Drawing.Point(8, 32)
Me.RichTextBox1.Name = "RichTextBox1"
Me.RichTextBox1.Size = New System.Drawing.Size(360, 192)
Me.RichTextBox1.TabIndex = 0
Me.RichTextBox1.Text = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(384, 273)
Me.Controls.Add(Me.RichTextBox1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Name = "Form1"
Me.Text = "实现查找功能"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
i = 0
i = RichTextBox1.Find(Me.TextBox1.Text)
If i >= 0 Then
Me.RichTextBox1.Select(i, Me.TextBox1.Text.Length)
i = i + TextBox1.Text.Length
End If
Me.Button2.Enabled = True
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim j As Integer
j = RichTextBox1.Find(TextBox1.Text, i, -1, None)
If j >= 0 Then
Me.RichTextBox1.Select(j, Me.TextBox1.Text.Length)
j = j + TextBox1.Text.Length
i = j
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.Filter = "RTF格式文件(*.rtf)|*.rtf"
If OpenFileDialog1.ShowDialog() = DialogResult.OK And _
OpenFileDialog1.FileName <> "" Then
RichTextBox1.LoadFile(OpenFileDialog1.FileName)
End If
End Sub
End Class
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?