form1.vb

来自「vb 应用实例 简单应用 dddddddddddddddddddddddddd」· VB 代码 · 共 319 行

VB
319
字号
Imports System.IO

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form  "

    Public Sub New()
        MyBase.New()

        InitializeComponent()


    End Sub

    
    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

 
    Private components As System.ComponentModel.IContainer

    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
    Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents Button4 As System.Windows.Forms.Button
    Friend WithEvents Button5 As System.Windows.Forms.Button
    Friend WithEvents Button6 As System.Windows.Forms.Button
    Friend WithEvents Button7 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
        Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog()
        Me.TextBox2 = New System.Windows.Forms.TextBox()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.Button3 = New System.Windows.Forms.Button()
        Me.Button4 = New System.Windows.Forms.Button()
        Me.Button5 = New System.Windows.Forms.Button()
        Me.Button6 = New System.Windows.Forms.Button()
        Me.Button7 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(20, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(96, 24)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "文件"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(20, 112)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both
        Me.TextBox1.Size = New System.Drawing.Size(441, 136)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = ""
        '
        'SaveFileDialog1
        '
        Me.SaveFileDialog1.FileName = "doc1"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(133, 16)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(328, 21)
        Me.TextBox2.TabIndex = 2
        Me.TextBox2.Text = ""
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(20, 48)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(123, 24)
        Me.Button2.TabIndex = 3
        Me.Button2.Text = "打开(整个)"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(154, 48)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(153, 24)
        Me.Button3.TabIndex = 4
        Me.Button3.Text = "打开(一次一行)"
        '
        'Button4
        '
        Me.Button4.Location = New System.Drawing.Point(317, 48)
        Me.Button4.Name = "Button4"
        Me.Button4.Size = New System.Drawing.Size(144, 24)
        Me.Button4.TabIndex = 5
        Me.Button4.Text = "打开(一次一字)"
        '
        'Button5
        '
        Me.Button5.Location = New System.Drawing.Point(20, 80)
        Me.Button5.Name = "Button5"
        Me.Button5.Size = New System.Drawing.Size(123, 24)
        Me.Button5.TabIndex = 6
        Me.Button5.Text = "保存"
        '
        'Button6
        '
        Me.Button6.Location = New System.Drawing.Point(154, 80)
        Me.Button6.Name = "Button6"
        Me.Button6.Size = New System.Drawing.Size(153, 24)
        Me.Button6.TabIndex = 7
        Me.Button6.Text = "保存(SaveFileDialog)"
        '
        'Button7
        '
        Me.Button7.Location = New System.Drawing.Point(317, 80)
        Me.Button7.Name = "Button7"
        Me.Button7.Size = New System.Drawing.Size(144, 24)
        Me.Button7.TabIndex = 8
        Me.Button7.Text = "保存(加在后面)"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(481, 266)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button7, Me.Button6, Me.Button5, Me.Button4, Me.Button3, Me.Button2, Me.TextBox2, Me.TextBox1, Me.Button1})
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "文本文件读写"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Dim OpenFileDialog1 As New OpenFileDialog()

        OpenFileDialog1.CheckFileExists = True
        OpenFileDialog1.DefaultExt = "txt"
        OpenFileDialog1.InitialDirectory = "C:\"
        OpenFileDialog1.Multiselect = False

        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            TextBox2.Text = OpenFileDialog1.FileName
        End If


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'need to Imports System.IO
        Dim sr As StreamReader
        Try
            sr = File.OpenText(TextBox2.Text)
            TextBox1.Text = sr.ReadToEnd()
        Catch e1 As Exception
            MsgBox("无法打开或者读取文件。 " + e1.Message)
        Finally
            If Not sr Is Nothing Then
                sr.Close()
            End If
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim sr As StreamReader

        Try
            sr = File.OpenText(TextBox2.Text)
            TextBox1.Clear()

            Dim intLineCount As Integer = 0

            Dim strOneLine As String = sr.ReadLine()

            While Not strOneLine Is Nothing
                TextBox1.Text += "行 " + intLineCount.ToString() + ": " + strOneLine + vbCrLf
                intLineCount += 1
                strOneLine = sr.ReadLine()
            End While
        Catch e1 As Exception
            MsgBox("无法打开或者读取文件。 " + e1.Message)
        Finally
            If Not sr Is Nothing Then
                sr.Close()
            End If
        End Try

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim sr As StreamReader

        Try
            sr = File.OpenText(TextBox2.Text)
            TextBox1.Clear()

            Dim intNext As Integer = sr.Read()

            While intNext <> -1
                TextBox1.Text += ChrW(intNext)
                intNext = sr.Read()
                TextBox1.Refresh()
                System.Threading.Thread.CurrentThread.Sleep(100)
            End While
        Catch e1 As Exception
            MsgBox("无法打开或者读取文件。" + e1.Message)
        Finally
            If Not sr Is Nothing Then
                sr.Close()
            End If
        End Try
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

        If File.Exists(TextBox2.Text) Then
            If MsgBox("文档存在,是否要覆盖?", MsgBoxStyle.YesNo) <> MsgBoxResult.Yes Then

                Return
            End If
        End If

        Dim sr As StreamWriter

        Try
            sr = File.CreateText(TextBox2.Text)
            sr.Write(TextBox1.Text)
            sr.Flush()
            MsgBox("保存文件OK")

        Catch e1 As Exception
            MsgBox("无法保存文件,检查是否具有写入文件的权利。 " + e1.Message)
        Finally
            If Not sr Is Nothing Then
                sr.Close()
            End If
        End Try
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Dim sr As StreamWriter

        Try
            'Append
            sr = File.AppendText(TextBox2.Text)
            sr.Write(TextBox1.Text)
            sr.Flush()
            MsgBox("保存文件OK")
        Catch e1 As Exception
            MsgBox("无法保存文件,检查是否具有写入文件的权利。 " + e1.Message)
        Finally
            If Not sr Is Nothing Then
                sr.Close()
            End If
        End Try

    End Sub

    'See CommonDialogs.sln for details
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

        Dim FileName1 As String

        Dim sw1 As StreamWriter

        Try
            With SaveFileDialog1

                .AddExtension = True

                .CheckPathExists = True

                .CreatePrompt = False

                .OverwritePrompt = True

                .ValidateNames = True

                .ShowHelp = True

                .DefaultExt = "txt"

                .FileName = FileName1

                .Filter = "Text files (*.txt)|*.txt|All files|*.*"
                .FilterIndex = 1

                If .ShowDialog() = DialogResult.OK Then
                    FileName1 = .FileName
                    sw1 = New StreamWriter(FileName1)
                    sw1.Write(TextBox1.Text)
                    MsgBox("保存文件OK")

                End If
            End With

        Catch e1 As Exception
            MessageBox.Show(e1.Message, Me.Text)

        Finally
            If Not (sw1 Is Nothing) Then
                sw1.Close()
            End If
        End Try
    End Sub
End Class

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?