Please check out the new Help Developer forum

Show Sidebar

Creating a simple notebook - VB.NET

August 2, 2007

Creating a notebook is one of the first projects a beginner to VB.NET tends to face, after the hello world application. A notebook is simple but has the ability to be developed into something amazing.

In this tutorial I will show you how to create a simple notebook that can open, save, cut, copy and paste. You can take this much further by adding facilities such as printing.

Firstly, I will create a new project in Microsoft Visual Studio 2005, this should also work in Sharpe Develop, but both programs are free so I suggest you use Microsoft’s version, it is much better suited to beginners. You can create a new project by clicking File - New Project, on the window that appears choose Windows Application and give it a name like Notebook and click the OK button.You will be taken to the form view, resize the form to about 700 width x 500 height, you can do this by grabbing the bottom right of the form or by changing the Size property in the property pane on the right of the window.

Change the title that appears in the titlebar of the form by changing the text property to Notebook.

Now, go to the Toolbox panel and find text box, click and drag this onto the form. Select the text box on the form and change the Multiline property to true.

Now add a toolbar from the toolstrip to the form, this should automatically dock to the top.

Select the textbox again and change the Dock property to Fill, you will then need to right click on the text box and select Bring to Front, this means that it only takes up all the space that is available instead of taking the whole form which would mean part of the text box would be underneath the toolstrip.

Now click the toolstrip, you will notice an icon appear on the far left, click this and a button will appear on the toolstrip. Add six buttons to the toolstrip.

Right click on the first button and select Set Image, on the window that appears select the Import button and find the “new” icon, you can download the icons from the attachments below, and then click OK.

Do this for the other 5 icons adding the icons: open, save, cut, copy and paste.

Now click the toolstrip again and the original button will appear, click on the arrow to the right of the button and a menu will appear, select the Selector option from the menu. Click and drag the selector to in between the save and cut buttons.

Now click the Run button in the main toolbar of Visual Studio or press F5. The program should launch. Close the Notebook and you should go back to Visual Studio.

Now we are going to add the code to the six buttons in order to make them work.

Firstly, the new button. Double click on the New button on the toolstrip and the code area should appear. Now add this code:

textbox1.clear()

Basically what this does is, clears all the contents in the text box to show a clean text box, ie, a clean document.

Next, you will add the code to the open button, but first go back to the Design view and add an openfiledialog to the form, double click on the open button on the toolstrip and add this code:

If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim fn As String
Dim sr As IO.StreamReader
fn = Trim(OpenFileDialog1.FileName)
sr = IO.File.OpenText(fn)
TextBox1.Text = sr.ReadToEnd
sr.Close()
End If

This code opens the text file as a stream and then adds it to the textbox as it is read.

Now go back to the design view and add a savefiledialog and double click on the Save button on the toolstrip, and add this code:

If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim SW As New IO.StreamWriter(SaveFileDialog1.FileName)
SW.Write(TextBox1.Text)
SW.Flush()
SW.Close()
FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output)
Print(1, TextBox1.Text)
FileClose(1)
End If

This adds the text you have added to the text box to a streamwriter which writes it into a valid text file.

Now go back to the design view and add double click the Cut button and add this code:

TextBox1.Cut()

Add this code to the copy button:

TextBox1.Copy()

Finally, add this code to the paste button:

TextBox1.Paste()

Now save the application and run it, you should now have a fully functioning Notebook program. I will now show you some code to make the application much bigger:

Add a Wordwrap

If textbox1.WordWrap = False Then
textbox1.WordWrap = True
Else
textbox1.WordWrap = False
End If

Change the font

TextBox1.SelectAll()
Dim f As New FontDialog
If f.ShowDialog = Windows.Forms.DialogResult.OK Then
TextBox1.Font = f.Font
End If

Print the text file

Static intCurrentChar As Int32
Dim fontt As New Font(textbox1.Font.FontFamily, textbox1.Font.SizeInPoints )

Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32
With PrintDocument.DefaultPageSettings
intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
marginLeft = .Margins.Left
marginTop = .Margins.Top
End With

Dim intLineCount As Int32 = (Int(intPrintAreaHeight / fontt.Height))
Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight)
Dim Font As New StringFormat(StringFormatFlags.LineLimit)
Dim intlinesfilled, intcharsfitted As Int32
e.Graphics.MeasureString(Mid(rtb1.Text, intCurrentChar + 1), fontt, _
New SizeF(intPrintAreaWidth, intPrintAreaHeight), Font, _
intcharsfitted, intlinesfilled)

e.Graphics.DrawString(Mid(rtb1.Text, intCurrentChar + 1), fontt, _
Brushes.Black, rectPrintingArea, Font)
intCurrentChar += intCharsFitted
If intCurrentChar < rtb1.Text.Length Then
e.HasMorePages = True
Else
e.HasMorePages = False
intCurrentChar = 0
End If

Find a word (you will have to add a new text box and call it txtFind)

textbox1.Find(txtFind.Text)

With this extra code you should be able to produce a notebook on the same level as Windows Notepad.

See the attachments for sample files and the icons for the toolbar.

Attachments:
Icons for Toolbar: vbnet_notebook_icons.zip
Sample Project Files: vbnet_notebook_source.zip

Simon North

Submit to Stumbleupon Submit to Del.icio.us Submit to Google

SUBSCRIBE TO RSS

RSS FEED - EMAIL FEED

Comments RSS Feed

Comments are closed.