⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 vb 应用实例 简单应用 ddddddddddddddddddddddddddddddddd
💻 VB
📖 第 1 页 / 共 2 页
字号:
                '可选之档案类型(档案的扩展名), 以|分隔, FilterIndex 由1计算起
                .Filter = "Text files (*.txt)|*.txt|All files|*.*"
                .FilterIndex = 1

                '储存档案
                If .ShowDialog() = DialogResult.OK Then
                    FileName1 = .FileName
                    sw1 = New StreamWriter(FileName1)
                    sw1.Write(TextBox1.Text)

                    Me.HasChanges = False
                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


    Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        '有修改
        Me.HasChanges = True
    End Sub


    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Try
            _ClosingComplete = True

            If Me.HasChanges Then
                Dim strDocTitle As String
                '窗体标题有 * 表示有修改过
                If Me.Text.EndsWith("*") Then
                    strDocTitle = Me.Text.Substring(0, (Me.Text.Length - 1))
                Else
                    strDocTitle = Me.Text
                End If

                '有修改过问是否要储存?
                Select Case MessageBox.Show("是否要储存 " & strDocTitle, "关闭中...", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
                    Case DialogResult.Yes
                        '储存
                        SaveDoc()

                    Case DialogResult.No


                    Case DialogResult.Cancel
                        '取消
                        e.Cancel = True
                        _ClosingComplete = False
                        '起动事件停止 application, 以避免关闭到其它开启的文件 
                        RaiseEvent SaveWhileClosingCancelled(Me, Nothing)

                End Select
            End If
        Catch exp As Exception
            MessageBox.Show(exp.Message, exp.Source, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub



    Private Sub MenuItem8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem8.Click
        Dim ts1 As StreamReader

        Try
            With OpenFileDialog1
                '检查所选择的档案是否存在
                .CheckFileExists = True

                '检查所选择的档案路径是否存在
                .CheckPathExists = True

                '预设的档案扩展名 .txt 檔
                .DefaultExt = "txt"

                '如果对话框传回快捷方式所参照的档案位置,则为 true ;否则为 false 。默认值是 true 。
                .DereferenceLinks = True

                '可选之档案类型(档案的扩展名), 以|分隔
                .Filter = "Text files (*.txt)|*.txt|All files|*.*"

                '可多选 Multiselect = True
                .Multiselect = False

                '如果对话框在使用者搜寻档案过程中变更目录时,将目前的目录还原成它原来的值,属性值为 true ;否则为 false 。默认值是 false 。
                .RestoreDirectory = True

                '显示 Help 按钮
                .ShowHelp = True

                '显示 Read-Only checkbox
                .ShowReadOnly = False

                '显示 Read-Only checkbox 时, 预设为打勾
                '.ReadOnlyChecked = False

                .Title = "选择待开启的文字文件"

                '仅接受有效的 Win32 檔名
                .ValidateNames = True

                '读取文字文件到 TextBox1
                If .ShowDialog() = DialogResult.OK Then
                    FileName1 = .FileName
                    ts1 = New StreamReader(.OpenFile)
                    TextBox1.Text = ts1.ReadToEnd()
                End If
            End With

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

        Finally
            If Not (ts1 Is Nothing) Then
                ts1.Close()
            End If
        End Try

    End Sub
End Class



Public Class MyForm

    Private Shared _MyForm As New Collection()
    Private Shared FormsCount As Integer = 0
    Private Shared _CancelExit As Boolean = False
    Private Shared _ShutdownInProgress As Boolean = False

    Public Shared ReadOnly Property Count() As Integer
        Get
            Return _MyForm.Count
        End Get
    End Property

    Public Shared Sub Main()

        Try
            '开启新窗体
            MyForm.AddNewForm()

        Catch exp As Exception
            MessageBox.Show("无法开启新窗体.", "Application Main", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Application.Exit()
        End Try

        '设定主要执行绪 (main thread), 让一个窗体关闭不会影响所有窗体
        Application.Run()
    End Sub

    '开启新窗体
    Public Shared Sub AddNewForm()
        Try
            MyForm.FormsCount += 1

            '开启新窗体
            Dim frm As New Form1()
            '窗体标题
            frm.Text = "记事本 " & MyForm.FormsCount.ToString()
            _MyForm.Add(frm, frm.GetHashCode.ToString())

            '关闭事件
            AddHandler frm.Closed, AddressOf MyForm.Form1_Closed
            'SaveWhileClosingCancelled事件
            AddHandler frm.SaveWhileClosingCancelled, AddressOf MyForm.Form1_SaveWhileClosingCancelled
            '结束事件
            AddHandler frm.ExitApplication, AddressOf MyForm.Form1_ExitApplication

            '显示窗体
            frm.Show()

        Catch e1 As Exception
            MessageBox.Show(e1.Message, e1.Source, MessageBoxButtons.OK, MessageBoxIcon.Error)
            If MyForm.Count = 0 Then
                '再触发错误以便结束 process
                Throw e1
            End If
        End Try
    End Sub

    Private Shared Sub CloseFrom(ByVal frm1 As Form1)
        '移除窗体
        _MyForm.Remove(frm1.GetHashCode.ToString())
        MyForm.FormsCount -= 1

        '若无窗体则结束整个应用程序
        If MyForm.FormsCount = 0 Then
            Application.Exit()
        End If
    End Sub

    Private Shared Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            Dim frm1 As Form1 = CType(sender, Form1)

            '移除事件
            RemoveHandler frm1.Closed, AddressOf MyForm.Form1_Closed
            RemoveHandler frm1.SaveWhileClosingCancelled, AddressOf MyForm.Form1_SaveWhileClosingCancelled
            RemoveHandler frm1.ExitApplication, AddressOf MyForm.Form1_ExitApplication

            MyForm.CloseFrom(frm1)
        Catch exp As Exception
            MessageBox.Show(exp.Message, exp.Source, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Shared Sub Form1_SaveWhileClosingCancelled(ByVal sender As Object, ByVal e As System.EventArgs)
        If _ShutdownInProgress Then
            MyForm._CancelExit = True
        End If
    End Sub

    Private Shared Sub Form1_ExitApplication(ByVal sender As Object, ByVal e As System.EventArgs)
        Try
            _ShutdownInProgress = True

            Dim frm1 As Form1
            Dim i As Integer

            For i = _MyForm.Count To 1 Step -1
                frm1 = CType(_MyForm(i), Form1)
                If frm1.HasChanges Then
                    frm1.Close()
                End If

                If _CancelExit = True Then
                    _CancelExit = False
                    Exit Sub
                End If
            Next

            If _MyForm.Count > 0 Then
                For i = _MyForm.Count To 1 Step -1
                    frm1 = CType(_MyForm(i), Form1)
                    frm1.Close()
                Next
            End If
        Catch exp As Exception
            MessageBox.Show(exp.Message, exp.Source, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Application.Exit()
        Finally
            _ShutdownInProgress = False
        End Try
    End Sub

End Class

⌨️ 快捷键说明

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