A function-enhanced notepad _ VB. NET

Source: Internet
Author: User
Tags word wrap

I think everyone has heard of notepad2, notepad ++ and other types of notepad enhancement software. Do you want to write one for yourself?

Today we teach you to use VB. NET to write a notebook with enhanced functions (supports full-text word count statistics, selected part of Word Count statistics, supports doc, and rtf formats)

First:



If it feels good, let's look at the source code:


Public Class MainForm
    Dim FileName As String = "Untitled"
    Dim word As String = ""
    Dim ML As Boolean = False

    Private Sub Form1_Load (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.StartPosition = FormStartPosition.CenterScreen
        Me.Text = FileName + "-" + "Notepad-AngelHacker"
        RichTextBox1.WordWrap = False
        RichTextBox1.ContextMenuStrip = ContextMenuStrip1
        RichTextBox1.ScrollBars = RichTextBoxScrollBars.Both
        New NToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.N
        Open OToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.O
        Save SToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.S
        Cut TToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.X
        CToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.C
        Paste PToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.V
        Select All AToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.A
        Delete LToolStripMenuItem.ShortcutKeys = Keys.Delete
        Find FToolStripMenuItem.ShortcutKeys = Keys.Control + Keys.F
        Find the next NToolStripMenuItem.ShortcutKeys = Keys.F3
        Find the next NToolStripMenuItem.Enabled = False
    End Sub

    Private Sub New NToolStripMenuItem_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles New NToolStripMenuItem.Click
        RichTextBox1.Text = ""
        FileName = "Untitled"
        Me.Text = FileName + "-" + "Notepad-AngelHacker"
    End Sub

    Private Sub Open OToolStripMenuItem_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Open OToolStripMenuItem.Click
        OpenFileDialog1.Filter = "Text document (* .txt) | * .txt | RTF document (* .rtf) | * .rtf | All files (*. *) | *. *"
        OpenFileDialog1.FileName = ""
        If OpenFileDialog1.ShowDialog () = Windows.Forms.DialogResult.Cancel Then
            Exit Sub
        Else
            RichTextBox1.Text = ""
            FileName = OpenFileDialog1.FileName
            Select Case OpenFileDialog1.FilterIndex
                Case "1"
                    RichTextBox1.LoadFile (FileName, RichTextBoxStreamType.PlainText)
                Case "2"
                    RichTextBox1.LoadFile (FileName, RichTextBoxStreamType.RichText)
                Case Else
                    On Error GoTo txt
                    RichTextBox1.LoadFile (FileName)
            End Select
            Me.Text = FileName + "-" + "Notepad-AngelHacker"
        End If
        Exit Sub
TXT:
        RichTextBox1.LoadFile (FileName, RichTextBoxStreamType.PlainText)
    End Sub

    Private Sub holds SToolStripMenuItem_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saves SToolStripMenuItem.Click
        If Me.Text = "Untitled-Notepad-AngelHacker" Then
            SaveFileDialog1.Filter = "Text document (* .txt) | * .txt | RTF document (* .rtf) | * .rtf | All files (*. *) | *. *"
            SaveFileDialog1.FileName = ""
            SaveFileDialog1.CheckPathExists = True
            If SaveFileDialog1.ShowDialog () = Windows.Forms.DialogResult.Cancel Then
                Exit Sub
            Else
                Select Case SaveFileDialog1.FilterIndex
                    Case "1"
                        RichTextBox1.SaveFile (SaveFileDialog1.FileName, RichTextBoxStreamType.PlainText)
                    Case "2"
                        RichTextBox1.SaveFile (SaveFileDialog1.FileName, RichTextBoxStreamType.RichText)
                    Case Else
                        RichTextBox1.SaveFile (SaveFileDialog1.FileName)
                End Select
                Me.Text = SaveFileDialog1.FileName + "-" + "Notepad-AngelHacker"
            End If
        Else
            Select Case Mid (Me.Text, Me.Text.Length-21, 3)
                Case "txt"
                    RichTextBox1.SaveFile (Mid (Me.Text, 1, Me.Text.Length-18), RichTextBoxStreamType.PlainText)
                Case "rtf"
                    RichTextBox1.SaveFile (Mid (Me.Text, 1, Me.Text.Length-18), RichTextBoxStreamType.RichText)
                Case Else
                    RichTextBox1.SaveFile (Mid (Me.Text, 1, Me.Text.Length-18))
            End Select
        End If
    End Sub

    Private Sub Save As AToolStripMenuItem_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Save As AToolStripMenuItem.Click
        SaveFileDialog1.Filter = "Text document (* .txt) | * .txt | RTF document (* .rtf) | * .rtf | All files (*. *) | *. *"
        SaveFileDialog1.FileName = ""
        SaveFileDialog1.CheckPathExists = True
        If SaveFileDialog1.ShowDialog () = Windows.Forms.DialogResult.Cancel Then
            Exit Sub
        Else
            Select Case SaveFileDialog1.FilterIndex
                Case "1"
                    RichTextBox1.SaveFile (SaveFileDialog1.FileName, RichTextBoxStreamType.PlainText)
                Case "2"
                    RichTextBox1.SaveFile (SaveFileDialog1.FileName, RichTextBoxStreamType.RichText)
                Case Else
                    RichTextBox1.SaveFile (SaveFileDialog1.FileName)
            End Select
            Me.Text = SaveFileDialog1.FileName + "-" + "Notepad-AngelHacker"
End If
    End Sub

    Private Sub exits XToolStripMenuItem_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exits XToolStripMenuItem.Click
        End
    End Sub

    Private Sub reset UToolStripMenuItem_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles reset UToolStripMenuItem.Click, reset UToolStripMenuItem1.Click
        RichTextBox1.Text = ""
        RichTextBox1.SelectionStart = 1
        word = ""
        Find the next NToolStripMenuItem.Enabled = False
        RichTextBox1.Font = Font
    End Sub

    Private Sub cut TToolStripMenuItem_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cut TToolStripMenuItem.Click, cut TToolStripMenuItem1.Click
        RichTextBox1.Cut ()
    End Sub

    Private Sub Copy CToolStripMenuItem_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Copy CToolStripMenuItem.Click, Copy CToolStripMenuItem1.Click
        RichTextBox1.Copy ()
    End Sub

    Private Sub Paste PToolStripMenuItem_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Paste PToolStripMenuItem.Click, Paste PToolStripMenuItem1.Click
        RichTextBox1.Paste ()
    End Sub

    Private Sub Select All AToolStripMenuItem_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Select All AToolStripMenuItem.Click, Select All AToolStripMenuItem1.Click
        RichTextBox1.SelectAll ()
    End Sub

    Private Sub delete LToolStripMenuItem_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delete LToolStripMenuItem.Click, delete DToolStripMenuItem.Click
        RichTextBox1.Text = RichTextBox1.Text.Remove (RichTextBox1.SelectionStart, RichTextBox1.SelectionLength)
    End Sub

    Private Sub Word Count ToolStripMenuItem_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Word Count ToolStripMenuItem.Click, Word Count SToolStripMenuItem.Click
        If RichTextBox1.SelectedText = "" Then
            MsgBox ("Full text" + RichTextBox1.TextLength.ToString + "Character")
        Else
            MsgBox ("Number of characters selected" + RichTextBox1.SelectionLength.ToString)
        End If
    End Sub

    Private Sub Wrap over WToolStripMenuItem_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Wrap over WToolStripMenuItem.Click
        If ML = False Then
            RichTextBox1.WordWrap = True
            Word wrap WToolStripMenuItem.Checked = True
            ML = True
        Else
            RichTextBox1.WordWrap = False
            Word wrap WToolStripMenuItem.Checked = False
            ML = False
        End If
    End Sub

    Private Sub on AToolStripMenuItem_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles on AToolStripMenuItem.Click
        AboutBox1.ShowDialog ()
    End Sub

    Private Sub Font FToolStripMenuItem_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Font FToolStripMenuItem.Click
        If FontDialog1.ShowDialog () = Windows.Forms.DialogResult.Cancel Then
            Exit Sub
        Else
            RichTextBox1.Font = FontDialog1.Font
        End If
    End Sub

    Private Sub Find FToolStripMenuItem_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Find FToolStripMenuItem.Click
        word = InputBox ("Find content", "Find")
        If word = "" Then
            Exit Sub
        Else
            Dim a As Integer = RichTextBox1.Find (word, RichTextBox1.SelectionStart, RichTextBoxFinds.MatchCase)
            If a = -1 Then
                MsgBox ("Not Found" + "" "" + word + "" "")
            Else
                Find the next NToolStripMenuItem.Enabled = True
                Exit Sub
            End If
        End If
    End Sub

    Private Sub find next NToolStripMenuItem_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles find next NToolStripMenuItem.Click
        Dim wz As Integer
        If RichTextBox1.SelectionStart = RichTextBox1.TextLength Then
            MsgBox ("Not Found" + "" "" + word + "" "")
        Else
            wz = RichTextBox1.SelectionStart
            wz + = 1
            Dim a As Integer = RichTextBox1.Find (word, wz, RichTextBoxFinds.MatchCase)
            If a = -1 Then
                MsgBox ("Not Found" + "" "" + word + "" "")
            Else
                Exit Sub
            End If
        End If
    End Sub

    Private Sub time and date DToolStripMenuItem_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles time and date DToolStripMenuItem.Click
        If ML = False Then
            RichTextBox1.Text + = Date.Now
        Else
            RichTextBox1.Text + = Chr (13) & Chr (10) & Date.Now
        End If
    End Sub

End Class  


Basically, no attribute settings are implemented by code. Because of its simple functions, process-oriented implementation is used, which looks simple.

Microsoft's notebook is actually very simple, and it gives you an open mind.

Need complete source code can be downloaded here: http://download.csdn.net/source/3412684

Welcome reprinted: http://blog.csdn.com/AngelHacker/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.