📄 form1.vb
字号:
Imports System.IO
Public Class note
Inherits System.Windows.Forms.Form
Private myfile As FileInfo
#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 MnuMain As System.Windows.Forms.MainMenu
Friend WithEvents mnuFile As System.Windows.Forms.MenuItem
Friend WithEvents mnuFileNew As System.Windows.Forms.MenuItem
Friend WithEvents mnuFileOpen As System.Windows.Forms.MenuItem
Friend WithEvents mnuFileSave As System.Windows.Forms.MenuItem
Friend WithEvents mnuFileExit As System.Windows.Forms.MenuItem
Friend WithEvents mnuEdit As System.Windows.Forms.MenuItem
Friend WithEvents mnuEditCopy As System.Windows.Forms.MenuItem
Friend WithEvents mnuEditCut As System.Windows.Forms.MenuItem
Friend WithEvents mnuEditPaste As System.Windows.Forms.MenuItem
Friend WithEvents mnuEditSelectAll As System.Windows.Forms.MenuItem
Friend WithEvents dlgOpen As System.Windows.Forms.OpenFileDialog
Friend WithEvents dlgSave As System.Windows.Forms.SaveFileDialog
Friend WithEvents mainText As System.Windows.Forms.TextBox
Friend WithEvents FontDialog1 As System.Windows.Forms.FontDialog
Friend WithEvents ColorDialog1 As System.Windows.Forms.ColorDialog
Friend WithEvents mnuFormat As System.Windows.Forms.MenuItem
Friend WithEvents mnuFont As System.Windows.Forms.MenuItem
Friend WithEvents mnuColor As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents tbarbunNew As System.Windows.Forms.ToolBarButton
Friend WithEvents ToolBarButton1 As System.Windows.Forms.ToolBarButton
Friend WithEvents tbarbunSave As System.Windows.Forms.ToolBarButton
Friend WithEvents ToolBarButton2 As System.Windows.Forms.ToolBarButton
Friend WithEvents tbarbunOpen As System.Windows.Forms.ToolBarButton
Friend WithEvents ToolBarButton3 As System.Windows.Forms.ToolBarButton
Friend WithEvents tbarbunCut As System.Windows.Forms.ToolBarButton
Friend WithEvents ToolBarButton4 As System.Windows.Forms.ToolBarButton
Friend WithEvents tbarbunCopy As System.Windows.Forms.ToolBarButton
Friend WithEvents ToolBarButton5 As System.Windows.Forms.ToolBarButton
Friend WithEvents tbarbunPaste As System.Windows.Forms.ToolBarButton
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.MnuMain = New System.Windows.Forms.MainMenu
Me.mnuFile = New System.Windows.Forms.MenuItem
Me.mnuFileNew = New System.Windows.Forms.MenuItem
Me.mnuFileOpen = New System.Windows.Forms.MenuItem
Me.mnuFileSave = New System.Windows.Forms.MenuItem
Me.MenuItem1 = New System.Windows.Forms.MenuItem
Me.mnuFileExit = New System.Windows.Forms.MenuItem
Me.mnuEdit = New System.Windows.Forms.MenuItem
Me.mnuEditCopy = New System.Windows.Forms.MenuItem
Me.mnuEditCut = New System.Windows.Forms.MenuItem
Me.mnuEditPaste = New System.Windows.Forms.MenuItem
Me.mnuEditSelectAll = New System.Windows.Forms.MenuItem
Me.mnuFormat = New System.Windows.Forms.MenuItem
Me.mnuFont = New System.Windows.Forms.MenuItem
Me.mnuColor = New System.Windows.Forms.MenuItem
Me.dlgOpen = New System.Windows.Forms.OpenFileDialog
Me.dlgSave = New System.Windows.Forms.SaveFileDialog
Me.mainText = New System.Windows.Forms.TextBox
Me.FontDialog1 = New System.Windows.Forms.FontDialog
Me.ColorDialog1 = New System.Windows.Forms.ColorDialog
Me.tbarbunNew = New System.Windows.Forms.ToolBarButton
Me.ToolBarButton1 = New System.Windows.Forms.ToolBarButton
Me.tbarbunSave = New System.Windows.Forms.ToolBarButton
Me.ToolBarButton2 = New System.Windows.Forms.ToolBarButton
Me.tbarbunOpen = New System.Windows.Forms.ToolBarButton
Me.ToolBarButton3 = New System.Windows.Forms.ToolBarButton
Me.tbarbunCut = New System.Windows.Forms.ToolBarButton
Me.ToolBarButton4 = New System.Windows.Forms.ToolBarButton
Me.tbarbunCopy = New System.Windows.Forms.ToolBarButton
Me.ToolBarButton5 = New System.Windows.Forms.ToolBarButton
Me.tbarbunPaste = New System.Windows.Forms.ToolBarButton
Me.SuspendLayout()
'
'MnuMain
'
Me.MnuMain.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile, Me.mnuEdit, Me.mnuFormat})
'
'mnuFile
'
Me.mnuFile.Index = 0
Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFileNew, Me.mnuFileOpen, Me.mnuFileSave, Me.MenuItem1, Me.mnuFileExit})
Me.mnuFile.Text = "文件 (&F)"
'
'mnuFileNew
'
Me.mnuFileNew.Index = 0
Me.mnuFileNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN
Me.mnuFileNew.Text = "新建"
'
'mnuFileOpen
'
Me.mnuFileOpen.Index = 1
Me.mnuFileOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO
Me.mnuFileOpen.Text = "打开"
'
'mnuFileSave
'
Me.mnuFileSave.Index = 2
Me.mnuFileSave.Shortcut = System.Windows.Forms.Shortcut.CtrlS
Me.mnuFileSave.Text = "保存"
'
'MenuItem1
'
Me.MenuItem1.Index = 3
Me.MenuItem1.Text = "-"
'
'mnuFileExit
'
Me.mnuFileExit.Index = 4
Me.mnuFileExit.Text = "退出"
'
'mnuEdit
'
Me.mnuEdit.Index = 1
Me.mnuEdit.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuEditCopy, Me.mnuEditCut, Me.mnuEditPaste, Me.mnuEditSelectAll})
Me.mnuEdit.Text = "编辑 (&E)"
'
'mnuEditCopy
'
Me.mnuEditCopy.Index = 0
Me.mnuEditCopy.Text = "复制 (&Ctrl+C)"
'
'mnuEditCut
'
Me.mnuEditCut.Index = 1
Me.mnuEditCut.Text = "剪切 (&Ctrl+X)"
'
'mnuEditPaste
'
Me.mnuEditPaste.Index = 2
Me.mnuEditPaste.Text = "粘贴 (&Ctrl+V)"
'
'mnuEditSelectAll
'
Me.mnuEditSelectAll.Index = 3
Me.mnuEditSelectAll.Text = "全选 (&Ctrl+A)"
'
'mnuFormat
'
Me.mnuFormat.Index = 2
Me.mnuFormat.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFont, Me.mnuColor})
Me.mnuFormat.Text = "格式 (&O)"
'
'mnuFont
'
Me.mnuFont.Index = 0
Me.mnuFont.Text = "字体"
'
'mnuColor
'
Me.mnuColor.Index = 1
Me.mnuColor.Text = "颜色"
'
'mainText
'
Me.mainText.Dock = System.Windows.Forms.DockStyle.Fill
Me.mainText.Location = New System.Drawing.Point(0, 0)
Me.mainText.Multiline = True
Me.mainText.Name = "mainText"
Me.mainText.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.mainText.Size = New System.Drawing.Size(384, 337)
Me.mainText.TabIndex = 1
Me.mainText.Text = ""
'
'note
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(384, 337)
Me.Controls.Add(Me.mainText)
Me.Menu = Me.MnuMain
Me.Name = "note"
Me.Text = "note"
Me.ResumeLayout(False)
End Sub
#End Region
Public Sub fileName()
If Me.mainText.Text <> Nothing Then
If MessageBox.Show("是否保存现在文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.No Then
Me.mainText.Text = ""
Else
dlgSave.Filter = "文本文件|*.txt"
If myfile Is Nothing Then
If dlgSave.ShowDialog = DialogResult.OK Then
myfile = New FileInfo(dlgSave.FileName)
writefile()
End If
Else
writefile()
End If
End If
End If
End Sub
Private Sub mnuFileNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuFileNew.Click
fileName()
End Sub
Private Sub mnuFileOpen_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click
If Me.dlgOpen.ShowDialog = DialogResult.OK Then
dlgSave.Filter = "文本文件|*.txt"
Dim oFile As FileStream
Dim oReader As StreamReader
Try
myfile = New FileInfo(dlgOpen.FileName)
oFile = myfile.Open(FileMode.Open)
oReader = New StreamReader(oFile)
Me.mainText.Text = oReader.ReadToEnd
Catch ex As Exception
MessageBox.Show(ex.Message, "Error")
Finally
oFile.Close()
oReader.Close()
End Try
End If
End Sub
Private Sub mnuFileExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click
Me.Close()
End Sub
Private Sub mnuFileSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuFileSave.Click
dlgSave.Filter = "文本文件|*.txt"
If dlgSave.ShowDialog = DialogResult.OK Then
myfile = New FileInfo(dlgSave.FileName)
writefile()
End If
End Sub
Public Sub writefile()
Try
Dim oOpen As FileStream
oOpen = myfile.Open(FileMode.OpenOrCreate)
Dim oWrite As New StreamWriter(oOpen, System.Text.Encoding.Default)
oWrite.Write(mainText.Text)
oWrite.Close()
oOpen.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "error")
End Try
End Sub
Private Sub mnuEditCopy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuEditCopy.Click
Clipboard.SetDataObject(mainText.SelectedText)
End Sub
Private Sub mnuEditCut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuEditCut.Click
Clipboard.SetDataObject(mainText.SelectedText)
mainText.SelectedText = ""
End Sub
Private Sub mnuEditPaste_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuEditPaste.Click
mainText.SelectedText = Clipboard.GetDataObject.GetData(DataFormats.Text)
End Sub
Private Sub mnuEditSelectAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuEditSelectAll.Click
mainText.SelectAll()
End Sub
Private Sub numHelpAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim strNote As String = "小小记事本1.0"
Dim strDate As String = "2004.11.25"
MessageBox.Show(strNote + vbCrLf + strDate)
End Sub
Private Sub note_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If MessageBox.Show("是否保存文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
dlgSave.Filter = "文本文件|*.txt"
If myfile Is Nothing Then
If dlgSave.ShowDialog = DialogResult.OK Then
myfile = New FileInfo(dlgSave.FileName)
writefile()
End If
Else
writefile()
End If
Else
e.Cancel = False
End If
End Sub
Private Sub mnuFont_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFont.Click
If FontDialog1.ShowDialog = DialogResult.OK Then
mainText.Font = FontDialog1.Font
mainText.ForeColor = FontDialog1.Color
End If
End Sub
Private Sub mnuColor_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuColor.Click
If ColorDialog1.ShowDialog <> DialogResult.Cancel Then
mainText.ForeColor = ColorDialog1.Color
End If
End Sub
Private Sub mainText_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mainText.TextChanged
End Sub
Private Sub mnuFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFile.Click
End Sub
Private Sub note_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -