Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to create Notepad Application using VB.NET

BhaktisBhaktis Posts: 2Member
hello everyone
i am new to the world of VB.NET. Can any one please help me, and tell me how does one create Notepad application in VB.NET?
thanks
Bhaktis

Comments

  • knetcozdknetcozd Posts: 4Member
    : hello everyone
    : i am new to the world of VB.NET. Can any one please help me, and
    : tell me how does one create Notepad application in VB.NET?
    : thanks
    : Bhaktis
    :


    I am also a newbie to vb.net,my advice is just to stick to the small stuff first so you dont confuse yourself,but heres a simple way to do it(not very efficient but simple enough for a newbie)

    drag a rich textbox and dock it to the parent container ie. the form,create a menu strip for edit,cut and save etc....

    then use a stream reader to open the file,streamwriter to write the file to
    a txt file.

  • jeffmanjeffman Posts: 112Member
    : : hello everyone
    : : i am new to the world of VB.NET. Can any one please help me, and
    : : tell me how does one create Notepad application in VB.NET?
    : : thanks
    : : Bhaktis
    : :
    :
    :
    : I am also a newbie to vb.net,my advice is just to stick to the small
    : stuff first so you dont confuse yourself,but heres a simple way to
    : do it(not very efficient but simple enough for a newbie)
    :
    : drag a rich textbox and dock it to the parent container ie. the
    : form,create a menu strip for edit,cut and save etc....
    :
    : then use a stream reader to open the file,streamwriter to write the
    : file to
    : a txt file.
    :
    :

    now iam an intermiedate-advanced programmer but have not fully transitioned to advanced vb.net programmer. so i can say you can also open notepad and type into it. here is the code to do that:
    Dim ProcId as integer
    ProcId = Shell("C:WindowsSystem32
    otepad.exe,,True,number of seconds to wait here")
    AppActivate(ProcId)
    'first example hard coded in
    My.Computer.Keyboard.SendKeys("keys to send",true)
    'second example using variables
    Dim sendkeys as string
    sendkeys = "my text here"
    My.Computer.Keyboard.SendKeys(sendkeys,true)

    this can be done with word but if you write commerical app rememeber cannot include that code in it

    hope this helps
    jeffman
    the planner

  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    Hmm, posted in March...

    Creating a notepad program is one of my favorite first-time coder activities. I also have people I tutor write a simple Calculator program too.

    The first step to creating a notepad application is to create a form with a Textbox docked in it. Set the "MultiLine" property of your Textbox to "True" and then set the "Dock" property to "Fill" (by clicking the center box). You can also set the ScrollBars property to Vertical to get the scroll bar in.

    Next you want to create a menu. Open notepad and just recreate all of the menu items. If you don't know how to add a menu, look in your Toolbox for something called MenuStrip (2.0 framework) or I think it's just Menu in 1.1 framework.

    Next step is to add the code under each menu item.

    Saving and Loading should be as simple as doing a FileOpenDialog or FileSaveDialog and adding code to read / write the files.

    Since the calculator doesn't require saving or openning, it may seem simpler, but it isn't. The hardest part of notepad is the undo feature, but note pad only goes back one change, that is fairly simple.

    Good luck!
  • IvanITIvanIT Posts: 1Member
    Sean etal,
    I like this thread so far. My challenge is to write the codes for doing the Find & Replace routine as in the usual Notepad. I have seen tons of samples that use a RichTextBox instead on a multiline TextBox. Does anyone have code samples to share on how to do Find/Replace? Gracias!
  • 420tim420tim Posts: 1Member
    iv'e got most of my notepad program finished, like most people i'm missing some codes for some of my menu items.... so far i have the following menu items coded: New, Open, Save, Save As, Print Preview, Print, Exit, Undo, Redo, Cut, Copy, Paste, goto line, Select All, Time and Date, Find and Font

    on my form i used a menustrip with the following objects: FontDialog, PrintDialog, PrintPreviewDialog and PrintDocument

    that should be all u need, if anyone happens to know the code for PageSetup, Delete, Replace or Word Wrap any help on those would be much appreciated also if any knows how to make a status bar to look just like the 1 in notepad that would help

    hope the codes help others out

    Public Class Form1
    Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
    If RichTextBox1.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"
    RichTextBox1.Clear()
    End If
    Me.Text = "Untitled"
    RichTextBox1.Clear()

    End If

    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
    Try
    Dim dlg As OpenFileDialog = New OpenFileDialog
    dlg.Title = "Open"
    dlg.Filter = "Rich Text Files (*.rtf|*.rtf"
    If dlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
    RichTextBox1.LoadFile(dlg.FileName)
    End If
    Catch ex As Exception : End Try
    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
    Try
    Dim dlg As SaveFileDialog = New SaveFileDialog
    dlg.Title = "Save"
    dlg.Filter = "Rich Text Files(*.rtf)|*.rtf"
    If dlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
    RichTextBox1.SaveFile(dlg.FileName, RichTextBoxStreamType.RichText)
    End If
    Catch ex As Exception : End Try
    End Sub

    Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
    Try
    Dim dlg As SaveFileDialog = New SaveFileDialog
    dlg.Title = "Save As"
    dlg.Filter = "Rich Text Files(*.rtf)|*.rtf"
    If dlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
    RichTextBox1.SaveFile(dlg.FileName, RichTextBoxStreamType.RichText)
    End If
    Catch ex As Exception : End Try
    End Sub

    Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewToolStripMenuItem.Click
    PrintPreviewDialog1.Document = PrintDocument1
    PrintPreviewDialog1.ShowDialog()
    End Sub

    Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
    PrintDialog1.ShowDialog()

    If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

    PrintDocument1.Print()
    End If
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
    'exit the program
    End
    End Sub

    Private Sub UndoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UndoToolStripMenuItem.Click
    If RichTextBox1.CanUndo Then

    RichTextBox1.Undo()

    End If
    End Sub

    Private Sub RedoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RedoToolStripMenuItem.Click
    RichTextBox1.Redo()
    End Sub

    Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
    RichTextBox1.Clear()
    End Sub

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
    RichTextBox1.Copy()
    End Sub

    Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
    RichTextBox1.Paste()
    End Sub

    Private Sub GoToToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoToToolStripMenuItem.Click
    Dim input As String = InputBox("Enter a Number between 1 and" & RichTextBox1.Lines.Length & " to goto that postion", " Select At ", RichTextBox1.SelectionStart)

    If IsNumeric(input) AndAlso input <= RichTextBox1.Lines.Length AndAlso input > 0 Then
    RichTextBox1.SelectionStart = RichTextBox1.GetFirstCharIndexFromLine(input - 1)
    RichTextBox1.ScrollToCaret()
    End If
    End Sub

    Private Sub SelectAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAllToolStripMenuItem.Click
    RichTextBox1.SelectAll()
    End Sub

    Private Sub TimeDateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimeDateToolStripMenuItem.Click
    Dim h As String = My.Computer.Clock.LocalTime.Hour
    Dim m As String = My.Computer.Clock.LocalTime.Minute
    Dim d As String = My.Computer.Clock.LocalTime.Date
    If m < 10 Then m = "0" + m
    If h > 12 Then h = h - 12
    Dim time As String = h + ":" + m
    If h > 12 Then time = time + "AM"
    If h < 12 Then time = time + "PM"
    RichTextBox1.Text = RichTextBox1.Text + time + "," + d

    End Sub

    Private Sub FindToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindToolStripMenuItem.Click
    Dim a As String
    Dim b As String

    a = InputBox("Enter text to be found")
    b = InStr(RichTextBox1.Text, a)

    If b Then

    RichTextBox1.Focus()
    RichTextBox1.SelectionStart = b - 1
    RichTextBox1.SelectionLength = Len(a)
    Else

    MsgBox("Text not found.")
    End If
    End Sub

    Private Sub FontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click
    FontDialog1.ShowDialog()
    RichTextBox1.Font = FontDialog1.Font
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Static currentChar As Integer
    Static currentLine As Integer

    Dim textfont As Font = RichTextBox1.Font
    Dim h, w As Integer
    Dim left, top As Integer

    With PrintDocument1.DefaultPageSettings

    h = .PaperSize.Height - .Margins.Top - .Margins.Bottom

    w = .PaperSize.Width - .Margins.Left - .Margins.Right

    left = PrintDocument1.DefaultPageSettings.Margins.Left

    top = PrintDocument1.DefaultPageSettings.Margins.Top

    End With

    e.Graphics.DrawRectangle(Pens.Blue, New Rectangle(left, top, w, h))

    If PrintDocument1.DefaultPageSettings.Landscape Then

    Dim a As Integer

    a = h

    h = w

    w = a

    End If

    Dim lines As Integer = CInt(Math.Round(h / textfont.Height))

    Dim b As New Rectangle(left, top, w, h)

    Dim format As StringFormat

    If Not RichTextBox1.WordWrap Then

    format = New StringFormat(StringFormatFlags.NoWrap)

    format.Trimming = StringTrimming.EllipsisWord

    Dim i As Integer

    For i = currentLine To Math.Min(currentLine + lines, RichTextBox1.Lines.Length - 1)

    e.Graphics.DrawString(RichTextBox1.Lines(i), textfont, Brushes.Black, New RectangleF(left, top + textfont.Height * (i - currentLine), w, textfont.Height), format)

    Next

    currentLine += lines

    If currentLine >= RichTextBox1.Lines.Length Then

    e.HasMorePages = False

    currentLine = 0

    Else

    e.HasMorePages = True

    End If

    Exit Sub

    End If

    format = New StringFormat(StringFormatFlags.LineLimit)

    Dim line, chars As Integer

    e.Graphics.MeasureString(Mid(RichTextBox1.Text, currentChar + 1), textfont, New SizeF(w, h), format, chars, line)

    If currentChar + chars < RichTextBox1.Text.Length Then

    If RichTextBox1.Text.Substring(currentChar + chars, 1) <> " " And RichTextBox1.Text.Substring(currentChar + chars, 1) <> vbLf Then

    While chars > 0

    RichTextBox1.Text.Substring(currentChar + chars, 1)

    RichTextBox1.Text.Substring(currentChar + chars, 1)

    chars -= 1

    End While

    chars += 1

    End If

    End If

    e.Graphics.DrawString(RichTextBox1.Text.Substring(currentChar, chars), textfont, Brushes.Black, b, format)

    currentChar = currentChar + chars

    If currentChar < RichTextBox1.Text.Length Then

    e.HasMorePages = True

    Else

    e.HasMorePages = False

    currentChar = 0

    End If
    End Sub

    Private Sub ReplaceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReplaceToolStripMenuItem.Click

    End Sub

    Private Sub PageSetupToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PageSetupToolStripMenuItem.Click

    End Sub
    End Class
Sign In or Register to comment.