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

📄 textedit.vb

📁 Programming the .NET Compact Framework with vb 源代码
💻 VB
📖 第 1 页 / 共 2 页
字号:
         '  Show it.
         Select Case fdlgOpen.ShowDialog()

            '  Check user's response.
         Case DialogResult.OK

               '  Save file name.
               strCurrentFile = fdlgOpen.FileName

               '  Open, read, close file.
               Dim srdrFile As New StreamReader _
                  ( _
                     New FileStream(strCurrentFile, _
                                    FileMode.Open), _
                         Me.encodeFile _
                  )
               With srdrFile
                  Me.textInput.Text = .ReadToEnd()
                  .Close()
               End With
            Case Else
         End Select
      End Sub

      Private Sub mitemFileSave_Click( _
                     ByVal sender As Object, _
                     ByVal e As EventArgs _
                     ) _
                     Handles mitemFileSave.Click

         '  If the user has not yet specified
         '     a file name, do SaveAs.
         '  Else,
         '     open, write, close file.
         If strCurrentFile = String.Empty Then
            mitemFileSaveAs_Click(sender, e)
         Else
            Dim swrtFile As New StreamWriter _
               ( _
                  New FileStream(strCurrentFile, _
                                 FileMode.Truncate), _
                  Me.encodeFile _
               )
            With swrtFile
               .Write(Me.textInput.Text)
               .Close()
            End With
         End If
      End Sub

      Private Sub mitemFileSaveAs_Click( _
                     ByVal sender As Object, _
                     ByVal e As EventArgs _
                     ) _
                     Handles mitemFileSaveAs.Click

         '  Get the file name from the user.
         If fdlgSave Is Nothing Then _
            fdlgSave = New SaveFileDialog
         Select Case fdlgSave.ShowDialog()
            Case DialogResult.OK
               '  Save file name.
               strCurrentFile = fdlgSave.FileName
               '  Save file.
               mitemFileSave_Click(sender, e)
            Case Else
         End Select
      End Sub

      Private Sub mitemFileFormat_Click( _
                     ByVal sender As Object, _
                     ByVal e As EventArgs _
                     ) _
                     Handles mitemFFAscii.Click, _
                             mitemFFDefault.Click, _
                             mitemFFUnicode.Click, _
                             mitemFFUtf7.Click, _
                             mitemFFUtf8.Click

         If sender.Equals(mitemFFAscii) Then
            Me.encodeFile = Encoding.ASCII
         ElseIf sender.Equals(mitemFFUnicode) Then
            Me.encodeFile = Encoding.Unicode
         ElseIf sender.Equals(mitemFFUtf7) Then
            Me.encodeFile = Encoding.UTF7
         ElseIf sender.Equals(mitemFFUtf8) Then
            Me.encodeFile = Encoding.UTF8
         Else
            Me.encodeFile = Encoding.Default
         End If
      End Sub

#End Region

#Region "Binary File IO routines"

      Private strDirName As String = _
                              "My Documents\YaoDurant\NotepadCE"
      Private strFileName As String = _
                              "Settings.dat"

      '  A structure containing font information and 
      '     routines to dump and restore that info 
      '     to / from a file.
      Private Structure ourFontInfo
         Public strName As String
         Public sglSize As Single
         Public intStyle As Integer

         Public Sub New(ByVal strName As String, _
                        ByVal sglSize As Single, _
                        ByVal intStyle As Integer)
            Me.strName = strName
            Me.sglSize = sglSize
            Me.intStyle = intStyle
         End Sub

         Public Sub WriteToFile(ByVal strDirName As String, _
                                ByVal strFileName As String, _
                                ByVal encodeFile As Encoding)

            If Not Directory.Exists(strDirName) Then _
                   Directory.CreateDirectory(strDirName)
            Directory.SetCurrentDirectory(strDirName)
            If Not File.Exists(strFileName) Then _
                              File.Create(strFileName).Close()

            Dim bwrtFile As New BinaryWriter _
               ( _
                  New FileStream(strFileName, _
                                 FileMode.OpenOrCreate, _
                                 FileAccess.Write, _
                                 FileShare.None), _
                  encodeFile _
               )
            With bwrtFile
               .Write(Me.strName)
               .Write(Me.sglSize)
               .Write(Me.intStyle)
               .Close()
            End With
         End Sub

         Public Sub ReadFromFile(ByVal strDirName As String, _
                                 ByVal strFileName As String, _
                                 ByVal encodeFile As Encoding)

            Directory.SetCurrentDirectory(strDirName)

            Dim brdrFile As New BinaryReader _
               ( _
                  New FileStream(strFileName, _
                                 FileMode.OpenOrCreate, _
                                 FileAccess.Read, _
                                 FileShare.None), _
                  encodeFile _
               )
            With brdrFile
               Me.strName = .ReadString()
               Me.sglSize = .ReadSingle()
               Me.intStyle = .ReadInt32()
               .Close()
            End With
         End Sub

      End Structure

      Private Sub SaveSettingsToFile()

         '  Create the directory and the file, if necessary.
         If Not Directory.Exists(strDirName) Then _
                           Directory.CreateDirectory(strDirName)
         Directory.SetCurrentDirectory(strDirName)
         If Not File.Exists(strFileName) Then _
                           File.Create(strFileName).Close()

         '  Open a writer for the file
         Dim bwrtFile As New BinaryWriter _
            ( _
               New FileStream(strFileName, _
                              FileMode.OpenOrCreate, _
                              FileAccess.Write, _
                              FileShare.None), _
               Me.encodeFile _
            )

         '  Write the data to the file.
         With bwrtFile
            .Write(textInput.Font.Name)      '  String.
            .Write(textInput.Font.Size)      '  Single.
            .Write(textInput.Font.Style)     '  Integer.
            .Close()
         End With

         '  Create an ourFontInfo structure, load it from
         '     TextInput's font, have the structure save
         '     its contents to the file.
         'Dim fiFont As ourFontInfo
         'With textInput.Font
         '   fiFont.strName = .Name
         '   fiFont.sglSize = .Size
         '   fiFont.intStyle = .Style
         'End With
         'fiFont.WriteToFile(strDirName, _
         '                   strFileName, _
         '                   Encoding.Default)
      End Sub

      Private Sub ReadSettingsFromFile()

         '  Make the directory the current directory.
         Directory.SetCurrentDirectory(strDirName)

         '  Open a reader for the file.
         Dim brdrFile As New BinaryReader _
            ( _
               New FileStream(strFileName, _
                              FileMode.OpenOrCreate, _
                              FileAccess.Read, _
                              FileShare.None), _
               Me.encodeFile _
            )

         '  Read each field from the file, then 
         '     close the file.
         With brdrFile
            Dim strName As String = .ReadString()
            Dim sglSize As Single = .ReadSingle()
            Dim intStyle As Integer = .ReadInt32()
            textInput.Font = _
               New Font(strName, sglSize, intStyle)
            .Close()
         End With

      End Sub

#End Region

#Region "Save/Restore to Registry"

      '  Utility object for registry access.
      Dim urNotepad As New UtilRegistry

      '  Field names for the settings in the registry.
      Dim strNotepadCE As String = "NotepadCe", _
          strFont As String = "Font", _
          strName As String = "Name", _
          strSize As String = "Size", _
          strStyle As String = "Style", _
          strBold As String = "Bold", _
          strItalic As String = "Italic", _
          strUnderline As String = "Underline", _
          strOptions As String = "Options", _
          strMenu As String = "Menu", _
          strToolBar As String = "ToolBar", _
          strScrollBars As String = "ScrollBars", _
          strTextAlign As String = "TextAlign", _
          strWordWrap As String = "WordWrap"

      Private Sub mitemSettingsSave_Click( _
                           ByVal sender As System.Object, _
                           ByVal e As System.EventArgs _
                           ) _
                           Handles mitemSettingsSave.Click

         With textInput.Font
            urNotepad.SetValue(strNotepadCE + "\" + strFont, _
                               strName, _
                               .Name)
            urNotepad.SetValue(strNotepadCE + "\" + strFont, _
                               strSize, _
                               Convert.ToInt16(.Size))
            urNotepad.SetValue(strNotepadCE + "\" + strFont, _
                               strStyle, _
                               Convert.ToInt16(.Style))

            urNotepad.SetValue(strNotepadCE + "\" + strOptions, _
                               strMenu, _
                               Me.Menu Is Me.menuMain)
            urNotepad.SetValue(strNotepadCE + "\" + strOptions, _
                               strToolBar, _
                               Me.Controls.Contains(tbarCommands))
            urNotepad.SetValue(strNotepadCE + "\" + strOptions, _
                               strScrollBars, _
                               textInput.ScrollBars)
            urNotepad.SetValue(strNotepadCE + "\" + strOptions, _
                               strTextAlign, _
                               textInput.TextAlign)
            urNotepad.SetValue(strNotepadCE + "\" + strOptions, _
                               strWordWrap, _
                               textInput.WordWrap)
         End With
      End Sub

      Private Sub mitemSettingsRestore_Click( _
                           ByVal sender As System.Object, _
                           ByVal e As System.EventArgs _
                           ) _
                           Handles mitemSettingsRestore.Click

         '  Read Font info, if any, from the registry, 
         '  Create a font from that info, or use default
         '     values if no info is in the registry
         '  Set textInput.Font = that font
         With urNotepad
            Dim strFontName As String
            Dim intFontSize As Integer
            Dim intFontStyle As Integer

            strFontName = "Tahoma"
            .GetValue(strNotepadCE + "\" + strFont, _
                      strName, _
                      strFontName)

            intFontSize = 9
            .GetValue(strNotepadCE + "\" + strFont, _
                      strSize, _
                      intFontSize)

            intFontStyle = FontStyle.Regular
            .GetValue(strNotepadCE + "\" + strFont, _
                      strStyle, _
                      intFontStyle)

            textInput.Font = _
               New Font(strFontName, intFontSize, intFontStyle)
         End With

         '  Read Option info, if any, from the registry
         '  Set the properties from that info, or use default
         '     values if no info is in the registry
         Dim boolTemp As Boolean
         Dim intTemp As Integer
         With urNotepad

            '  .Menu is either menuMain or Nothing
            boolTemp = True
            .GetValue(strNotepadCE + "\" + strOptions, _
                      strMenu, _
                      boolTemp)
            Me.Menu = IIf(boolTemp, Me.menuMain, Nothing)

            '  .Controls either contains 
            '     tbarCommands or it doesn't
            boolTemp = True
            .GetValue(strNotepadCE + "\" + strOptions, _
                      strToolBar, _
                      boolTemp)
            If boolTemp Then
               If Not Me.Controls.Contains(Me.tbarCommands) Then
                  Me.Controls.Add(Me.tbarCommands)
               End If
            Else
               If Me.Controls.Contains(Me.tbarCommands) Then
                  Me.Controls.Remove(Me.tbarCommands)
               End If
            End If

            '  .ScrollBars
            intTemp = ScrollBars.Both
            .GetValue(strNotepadCE + "\" + strOptions, _
                      strScrollBars, _
                      intTemp)
            textInput.ScrollBars = intTemp

            '  .TextAlign
            intTemp = HorizontalAlignment.Left
            .GetValue(strNotepadCE + "\" + strOptions, _
                      strTextAlign, _
                      intTemp)
            textInput.TextAlign = intTemp

            '  .WordWrap
            boolTemp = True
            .GetValue(strNotepadCE + "\" + strOptions, _
                      strWordWrap, _
                      boolTemp)
            textInput.WordWrap = boolTemp
         End With
      End Sub

      Private Sub mitemSettingsInit_Click( _
                           ByVal sender As System.Object, _
                           ByVal e As System.EventArgs _
                           ) _
                           Handles mitemSettingsInit.Click

         urNotepad.DeleteKey(strNotepadCE)
         mitemSettingsRestore_Click(Me, EventArgs.Empty)
      End Sub

#End Region

      Private Sub FormMain_Load(ByVal sender As Object, _
                                ByVal e As EventArgs _
                                ) _
                                Handles MyBase.Load

         mitemSettingsRestore_Click(Me, EventArgs.Empty)
         SaveSettingsToFile()
         ReadSettingsFromFile()
      End Sub

   End Class
End Namespace

⌨️ 快捷键说明

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