WHAT'S NEW?
Loading...

Create customize notepad in Visual Basic.Net


In this tutorial shows how to create simple and customized notepad based on Visual Basic.Net language. Obviously, through Visual Basic we can make various applications quickly in an hour or even minutes. In addition, these program quite the same as usual notepad in our windows, but I make a little bit customization in my designed and source code. Some source codes applied in this simple notepad are gathered in different websites on the internet and add additional features so that it looks like something different. Added features such as counting the number of characters and words, recognize HTML, and color. Unfortunately, it’s not really the same as notepad where all functions are here, this is actually a simple notepad as what I had intended to work with and not necessarily exactly identical as notepad. However, if you want to contribute additional features, your comments plus source code if they have are welcome.

Now, to work with our notepad we need different controls can be drag on our Windows Form Application. Here are:

MenuStrip, ColorDialog, FontDialog, OpenFileDialog, SaveFileDialog, PrintDialog, PrintDocument, PrintPreviewDialog, StatusStrip, and ContextMenuStrip.

Note: When you drag StatusStrip don’t forget to add 2 Status label for counting words and character and Contextual menu, once you add menu items such as cut, copy, paste, etc..., click textbox, and on your properties window, locate contextmenustrip and select the value name MenuStrip1.

Your Form looks like the following: 

Notepad in Visual Basic.Net
Final Output:
Notepad Photo


Source code for each menu:
New
 Dim Save As New SaveFileDialog()
        Dim myStreamWriter As System.IO.StreamWriter
        'Check if there's text added to the textbox
        If txtTextPad.Modified Then
            'If the text of notepad changed, the program will ask the user if they want to save the changes
            Dim ask As MsgBoxResult
            ask = MsgBox("Do you want to save the changes", MsgBoxStyle.YesNoCancel, "New Document")
            If ask = MsgBoxResult.No Then
                txtTextPad.Clear()
            ElseIf ask = MsgBoxResult.Cancel Then
            ElseIf ask = MsgBoxResult.Yes Then

                Save.Filter = "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
                Save.CheckPathExists = True
                Save.Title = "Save File"
                Save.ShowDialog(Me)
                Try
                    myStreamWriter = System.IO.File.AppendText(Save.FileName)
                    myStreamWriter.Write(txtTextPad.Text)
                    myStreamWriter.Flush()
                Catch ex As Exception
                End Try
                txtTextPad.Clear()

            End If
            'If textbox's text is still the same, notepad will open a new page:
        Else
            txtTextPad.Clear()
        End If
Open
 Dim Save As New SaveFileDialog()
        Dim myStreamWriter As System.IO.StreamWriter

        If txtTextPad.Modified Then
            'If the text of notepad changed, the program will ask the user if they want to save the changes
            Dim ask As MsgBoxResult
            ask = MsgBox("Do you want to save the changes", MsgBoxStyle.YesNoCancel, "Open Document")
            If ask = MsgBoxResult.No Then
                OpenFileDialog1.ShowDialog()
                Try
                    txtTextPad.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
                Catch ex As Exception

                End Try
            ElseIf ask = MsgBoxResult.Cancel Then
            ElseIf ask = MsgBoxResult.Yes Then

                Save.Filter = "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
                Save.CheckPathExists = True
                Save.Title = "Save File"
                Save.ShowDialog(Me)
                Try
                    myStreamWriter = System.IO.File.AppendText(Save.FileName)
                    myStreamWriter.Write(txtTextPad.Text)
                    myStreamWriter.Flush()
                Catch ex As Exception
                End Try
            End If
        Else
            'If textbox's text is still the same, notepad will show the OpenFileDialog
            OpenFileDialog1.ShowDialog()
            txtTextPad.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
        End If
Save
 Dim Save As New SaveFileDialog()
        Dim myStreamWriter As System.IO.StreamWriter
        Save.Filter = "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
        Save.CheckPathExists = True
        Save.Title = "Save File"
        Save.ShowDialog(Me)
        Try
            myStreamWriter = System.IO.File.AppendText(Save.FileName)
            myStreamWriter.Write(txtTextPad.Text)
            myStreamWriter.Flush()
        Catch ex As Exception
        End Try
Save As
SaveFileDialog1.ShowDialog()
        ' the application will check if the file is already exists, if exists, it will ask the user if they want to replace it
        If My.Computer.FileSystem.FileExists(SaveFileDialog1.FileName) Then
            Dim ask As MsgBoxResult
            ask = MsgBox("File already exists, would you like to replace it?", MsgBoxStyle.YesNo, "File Exists")
            'if the user decides not to replace the existing file
            If ask = MsgBoxResult.No Then
                SaveFileDialog1.ShowDialog()
                'if the user decides to replace the existing file
            ElseIf ask = MsgBoxResult.Yes Then
                My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, txtTextPad.Text, False)
            End If
            'if the file doesn't exist
        Else
            My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, txtTextPad.Text, False)
        End If

Print Preview
        If txtTextPad.Text = "" Then
            MessageBox.Show("Empty is not allowed print out")
        Else
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.ShowDialog()
        End If
Print
        If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

            PrintDocument1.Print()
        End If
Exit
If MessageBox.Show("Are you really want to exit to close the program?", "Closed", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Me.Close()
        End If
Cut, Copy, Paste, Undo, Redo, Select All, and Date and Time
Private Sub UndoToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles UndoToolStripMenuItem.Click
        txtTextPad.Undo()
    End Sub

    Private Sub CopyToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CopyToolStripMenuItem.Click
        txtTextPad.Cut()
    End Sub

    Private Sub PasteToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles PasteToolStripMenuItem.Click
        txtTextPad.Copy()
    End Sub

    Private Sub PasteToolStripMenuItem1_Click(sender As System.Object, e As System.EventArgs) Handles PasteToolStripMenuItem1.Click
        txtTextPad.Paste()
    End Sub

    Private Sub SelectToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SelectToolStripMenuItem.Click
        txtTextPad.SelectAll()
    End Sub

    Private Sub RedoToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles RedoToolStripMenuItem.Click
        txtTextPad.redo()
    End Sub

    Private Sub TimeAndDateToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles TimeAndDateToolStripMenuItem.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"
        txtTextPad.Text = txtTextPad.Text + " " + time + ", " + d
    End Sub

Font
FontDialog1.Font = txtTextPad.Font
        FontDialog1.ShowDialog()
        txtTextPad.Font = FontDialog1.Font
Color
 Dim colorDialog As New ColorDialog
        Try
            colorDialog.ShowDialog()
            txtTextPad.ForeColor = colorDialog.Color
        Catch ex As Exception
        End Try
Find
 Dim x As String
        Dim y As String
        x = InputBox("Enter text to be found")
        y = InStr(txtTextPad.Text, x)
        If y Then
            txtTextPad.Focus()
            txtTextPad.SelectionStart = y - 1
            txtTextPad.SelectionLength = Len(x)
        Else
            MessageBox.Show("Text not found!")
        End If
Status Bar On and Off
StatusStrip1.Visible = True
StatusStrip1.Visible = False

Accept Tab
If txtTextPad.AcceptsTab = True Then
            txtTextPad.AcceptsTab = False
        Else
            txtTextPad.AcceptsTab = True
        End If

Recognize HTML
If txtTextPad.DetectUrls = True Then
            txtTextPad.DetectUrls = False
            RecognizeToolStripMenuItem.Checked = False
        Else
            txtTextPad.DetectUrls = True
            RecognizeToolStripMenuItem.Checked = True
        End If
About
MessageBox.Show("The features of this notepad the same in usual notepad can be found in your computer, a simple notepad program created in Visual Basic.Net 2010 with my own version of customization and function. " & vbNewLine & vbNewLine & "By: Tesear.Com", "About", MessageBoxButtons.OK, MessageBoxIcon.Information)

Help
MessageBox.Show("Simply easy, almost the same function as notepad", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information)

Double click the textbox and put this code
        Dim split() As String = txtTextPad.Text.Split(" ")
        'count the number of characters
        'it will display on your status strip
        statusstripnumberoofchar.Text = "Characters: " & txtTextPad.Text.Count()
        'count the number of words
        statustripnumberofwords.Text = "Word: " & split.Length
   
Shortcut command can be found in your properties window.

Add item in listbox using c sharp in WPF


Windows Form Application and Window Presentation Foundation (WPF) are almost identical in some controls and coding style either Visual Basic or C# developer, but their some instances that both of them has their own advantages depends the users need. In this tutorials, I’ll show you a simple way how to add items in listbox when the user click add button and automatically add in a listbox list, I am using C# in WPF, this code also run in Windows Form Application.

Here is the source code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPF_My_First_Apps
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string name, address;//I declared this variable as public
                             //so that it will use when it call
                             //name and address value when the users
                             //click listbox item.
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            name = txtname.Text;
            address = txtaddress.Text;
            listBox1.Items.Add(name);
            listBox1.Items.Add(address);

        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int select;
            select = listBox1.SelectedIndex;

            switch (select)
            {
                case 0:
                    MessageBox.Show("You click name: " + name);
                    break;
                case 1:
                    MessageBox.Show("You click Address: " + address);
                    break;
            }
        }
    }
}

Sample screenshot:
WPF C sharp

To count the number items listed in the listbox, here is the code.
     int count;
            count = listBox1.Items.Count;
            MessageBox.Show(count.ToString());

To add item in listbox using checkbox, here is the code:
 private void checkBox1_Checked(object sender, RoutedEventArgs e)
        {
            if (checkBox1.IsChecked == true)
            {
                listBox1.Items.Add("Banana");
            }
        }

        private void checkBox2_Checked(object sender, RoutedEventArgs e)
        {
            if (checkBox1.IsChecked == true)
            {
                listBox1.Items.Add("Orange");
            }
        }
        private void checkBox3_Checked(object sender, RoutedEventArgs e)
        {
            if (checkBox1.IsChecked == true)
            {
                listBox1.Items.Add("Grapes");
            }
        }

To clear items in listbox, here is the code:
     listBox1.Items.Clear();
            checkBox1.IsChecked = false;
            checkBox2.IsChecked = false;
            checkBox3.IsChecked = false;

How to use mail merge to print multiple certificate


Microsoft World mail merge wizard is very useful to print multiple names from a data source in ease of use, yet neglected by many newbie’s in MS Word because they don’t familiar to use it. For instance, to print a certificate with different names, with the help of mail merge wizard, no need to change each name or perhaps to create one by one certificate for its name to be printed rather, record first the list of names in a data source, for example in Excel, Access, etc. and link it to enable to print simultaneously based on the sequence of names you typed.

To do so, here are the samples step by step tutorial in how to use mail merge wizard.
list of name in excel photo
Put the field name Name as a column and save it mailMerge, later on we are going to link it.

Now, back to MS Word Document and type the following. CERTIFICATE OF ATTENDANCE IS PRESENTED TO. The output looks like.
mail merge certificate
To link from our data source, we need to select Mailings Tab, select Start Mail Merge, and finally select Step by Step Mail Merge Wizard… in your right corner it shows.
mail merge properties
 We have an option for what type of document are you working on. Let the default option Letters. In steps 1 to 6 we need to start our document, so next to step 3.
mail merge browse

Now, we need to browse our files as what we created previously. Select browse and locate your files in the directory where you saved.
excel worksheet mail merge
When you noticed, we had three Sheets, shows where our records are created, select Sheet1$ because we create our record in Sheet 1. Let’s continue to step 4.
mail merge field
In ribbon area, select Insert Merge Field, the Name appear when you select it, that’s name of your field name or column name in your Excel.

certificate link data source
Noticed <<Name>> appears on your document to ensure you’ve already linked to your data source. Then continue to step 5. In arrow << Recipient: 1 >> determines the number of records in a list. Finally, step 6, mail merge is ready for printing.


Make a button and label at runtime in VB.NET


To create a control at design time is simply easy because you are free to decide which controls you want to add and no more codes needed to create controls. However, it’s more convenient to create controls at runtime specifically if you don’t know how many pieces of data you will need to display until runtime. In addition, displays unknown amounts of data in various controls is more flexible than creating at design time. For instance, you may want to output your result in label or to trigger a button to add controls to display a message, at runtime you’re free where to set its position of every control to display on your forms by setting SetBounds method.

To create a label at runtime, simply add this piece of code in you button.

 Dim lblNewLabel As New Label
        lblNewLabel.SetBounds(10, 10, 100, 25)
        lblNewLabel.Text = "Hello"
        Me.Controls.Add(lblNewLabel)

First declare a variable of type Label and initializes it with the New keyword. SetBounds methods set the position x, y, w, and h SetBounds(x,y,w,h) where the label shows in the forms.

If you are going to display the control in a container, you might use the statement grpLabels.Controls.Add(lblNewLabel)

Create a button at runtime using WithEvents keywords.

Sample Code #1.
Public Class FrmCreateButton
    Private WithEvents btnNewButton As Button

    Private Sub btnCreateButton_Click(sender As System.Object, e As System.EventArgs) Handles btnCreateButton.Click
        btnNewButton = New Button
        btnNewButton.SetBounds(16, 16, 80, 23)
        btnNewButton.Text = "Say Hi"
        Me.Controls.Add(btnNewButton)
    End Sub

    Private Sub btnNewButton_Click() Handles btnNewButton.Click
        MessageBox.Show("Now, I'm created")
    End Sub

End Class

This approach, when the user click btnCreateButton, Say Hi button created. When the users click btnNewButton_Click, event handler executes and displays a message.

Sample Code #2
Private Sub btnCreateHelloButton_Click(sender As System.Object, e As System.EventArgs) Handles btnCreateHelloButton.Click
        Dim CreateHelloButton As New Button
        CreateHelloButton.SetBounds(250, 65, 80, 23)
        CreateHelloButton.Text = "Hello"
        Me.Controls.Add(CreateHelloButton)

        AddHandler CreateHelloButton.Click, AddressOf Hello
    End Sub
    Private Sub Hello()
        MessageBox.Show("Hello Visual Basic.Net")
    End Sub

One solution to this dilemma is to use the AddHandler statement to add event handlers to the new controls. When you click the btnCreateHelloButton, its click event handler creates a new button object, storing it in a locally declared variable.

When you compare sample #1 and sample #2 is quite different.