Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / Visual Basic
Article

Notepad.NET - Creating a clone of notepad in Visual Basic

Rate me:
Please Sign up or sign in to vote.
2.57/5 (21 votes)
3 May 2008CPOL3 min read 129.2K   14.5K   41   21
Creating a clone of notepad in Visual Basic

Introduction

This is a simple and fully functional clone of the standard Windows Notepad. The article describes about the functions used in Notepad and tells the logic to write the code. Code is also provided

Background

Notepad basically has File, Edit, Format, View and Help.

I have recreated the menus File, Edit, Format and Help. I have excluded View menu because I think a simple notepad Application doesn't require any status bar. If you think you need a status bar, I've also provided the code for it.

Using the code

File Menu

New - If the document is modified then a Message Box with the buttons Yes and No is displayed asking whether it should be saved or not. Code is :

VB
If doc.Modified = True Then
            Dim x As Integer = MsgBox("Do you want to save the modified document ?", MsgBoxStyle.YesNo)
            If x = vbYes Then
                SaveToolStripMenuItem.PerformClick()
            Else
                Me.Text = "Untitled - Notepad.NET"
                doc.Clear()
            End If
            Me.Text = "Untitled - Notepad.NET"
            doc.Clear()

        End If          

Open - File is opened and Notepad.NET uses Rijndael encryption - HEX is encoding type. I have took a Cryptography class from this site called "Crypto.vb". I have implemented this to decrypt the data in the TextBox doc.

Save - File is saved using the same encryption algorithm.

And

A PageSetupDialog is also used.

Edit menu has the standard Cut, Copy, Paste, Undo etc.

When it came to the part of Find, Find Next and Replace, It was a big confusion and there was a difficulty doing it. But I turned up successfully with it.

Logic and Code of both Find and Replace

First I created a function called FindText with a dependency on the start_pos (declared as Integer). This is the starting position. Then I declared two private variables - target_pos (integer for determining the target's position) and target (declared as string).

Then I declared pos as integer

Pos is assigned to the function InStr that determines the position of the text.

So if pos is above 0 then text is found. So to select text, target_pos is assigned to ps.

The selection was a quite a difficulty for me.

The textbox's SelectionStart property determines the starting position. So it is target_pos minus 1. The selection goes up to the end. So the SelectionLength property is set to

Len(target)-(Len(target)-Len(target))

If its not found , Clear the target variable and display message that text is not found.

Ain't it easy ???

The code I came up with is :

The code is as following

Private Sub FindText(ByVal start_pos as integer)
 Dim pos As Integer


        pos = InStr(start_pos, doc.Text.ToLower, tf.Text.ToLower)
        If pos > 0 Then
            target_pos = pos
            doc.SelectionStart = target_pos - 1
            doc.SelectionLength = Len(target) - (Len(target) - Len(target))

        Else
            MsgBox("Text Not Found")
            target = ""
        End If

End Sub    


   target = InputBox("Enter Word to find")
        FindText(1)

        If doc.SelectedText <> target Then

        Else
            x = InputBox(String.Format("Selected text is : {0}. Enter text to replace", target))
            doc.SelectedText = x
        End If

The above is the code for Replace.

Word Wrap is a default property of the textbox.

Code for FindNext is also simple

Just add 1 to target_pos.

So code will be

FindText(target_pos + 1)

Font can be changed by declaring a new FontDialog, assigning the selected font to the textbox font and textbox fontstyle to the dialog's selected style.

Thats All.

Simple isn't it ?

If you think new functions can be added, please comment so I will receive a notification and respond as soon as possible.

<script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> var pageTracker = _gat._getTracker("UA-2980480-6"); pageTracker._initData(); pageTracker._trackPageview(); </script>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionquestion about project Pin
Member 113466521-Jan-15 8:06
Member 113466521-Jan-15 8:06 
Questionvery nice Pin
ibasskung16-Jul-14 0:25
ibasskung16-Jul-14 0:25 
QuestionC# version Pin
Luis Perez ----- (Simply Good Code)1-Aug-12 4:56
Luis Perez ----- (Simply Good Code)1-Aug-12 4:56 
GeneralMy vote of 2 Pin
noreply-thephoenixprod21-Mar-12 0:03
noreply-thephoenixprod21-Mar-12 0:03 
GeneralNotepad with C#.net Pin
PramodKandpal17-Jan-12 22:50
PramodKandpal17-Jan-12 22:50 
GeneralRe: Notepad with C#.net Pin
Luis Perez ----- (Simply Good Code)1-Aug-12 4:56
Luis Perez ----- (Simply Good Code)1-Aug-12 4:56 
GeneralNo problem Pin
Terence Wallace6-Dec-08 18:44
Terence Wallace6-Dec-08 18:44 
Question.LOG Files? Pin
Gulsath30-Jun-08 8:02
Gulsath30-Jun-08 8:02 
AnswerRe: .LOG Files? Pin
Gulsath30-Jun-08 8:12
Gulsath30-Jun-08 8:12 
AnswerRe: .LOG Files? Pin
Anshul R1-Jul-08 4:09
Anshul R1-Jul-08 4:09 
GeneralRe: .LOG Files? Pin
Gulsath1-Jul-08 7:32
Gulsath1-Jul-08 7:32 
GeneralRe: .LOG Files? Pin
Anshul R2-Jul-08 4:04
Anshul R2-Jul-08 4:04 
GeneralUpdated Pin
Anshul R3-May-08 23:46
Anshul R3-May-08 23:46 
GeneralNot up to mark but Pin
Maruf Maniruzzaman27-Apr-08 4:32
Maruf Maniruzzaman27-Apr-08 4:32 
GeneralRe: Not up to mark but Pin
Jeffrey Walton27-Apr-08 4:50
Jeffrey Walton27-Apr-08 4:50 
Hi Maruf,
Maruf Maniruzzaman wrote:
The writer claims that he is 11

I'm glad someone else picked up on his age. His handle is pranav95, so I would expect his age is 12 or 13.

Jeff
GeneralRe: Not up to mark but Pin
Anshul R3-May-08 22:04
Anshul R3-May-08 22:04 
GeneralNeeds work PinPopular
Rob Graham27-Apr-08 3:29
Rob Graham27-Apr-08 3:29 
GeneralRe: Needs work PinPopular
Anshul R3-May-08 22:10
Anshul R3-May-08 22:10 
QuestionWhich is it? Pin
#realJSOP27-Apr-08 1:42
mve#realJSOP27-Apr-08 1:42 
AnswerRe: Which is it? Pin
Paul Conrad27-Apr-08 4:29
professionalPaul Conrad27-Apr-08 4:29 
GeneralRe: Which is it? Pin
Anshul R3-May-08 22:14
Anshul R3-May-08 22:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.