How to make a Phone Book

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
23 posts Page 1 of 3
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

How to make a Phone Book
Usman55
Hello Everyone,

Today I remembered that I haven't wrote a tutorial on my easy to use, simple phone book application so I am going to do it right now. It is an application in which we can add and remove contacts using Settings. In this tutorial you will also learn how to search a listbox.

---------------------------------------------------------------------------

First of all, open Visual Basic 2008, create a new project, choose Windows Forms Application, type in the textbox Phone Book and press the OK button. As soon as you have done this, the Form1 will appear on the screen on which we will design our application and code it.


First we will do the designing and then the coding. So now we will change the Form1's properties to make it look good. Change the Form1's Text property to Phone Book. Change it's StartPosition property to CenterScreen. Change it's Size property to 550, 400.


Now add the following things to the form:

(1)_ 3 GroupBoxes. Change GroupBox1's Text property to "Search:", Size property to "518, 45" and Anchor property to "Top, Left, Right". Place it at the very top of the form. Change GroupBox2's Text property to "Actions:" or anything you want and Size property to "112, 77". Place it in the left or right part of the form below the GroupBox1. Change GroupBox3's Text property to "Contacts:", Size property to "400, 291" and Anchor property to "Top, Bottom, Left, Right". Place it below GroupBox1 with the GroupBox2.

(2)_ 1 Label. Change it's Text property to "Contact:" or "Name of Contact:". Place it in the first GroupBox.

(3)_ 1 TextBox. Place it next to the Label in GroupBox1. Change it's Anchor property to "Top, Left, Right".

(4)_ 2 Buttons. Change Button1's Text property to "Add New" and Button2's to "Remove". Place both of them in the GroupBox2.

(5)_ 1 ListBox. Change it's Anchor property to "Top, Bottom, Left, Right" and place it in the GroupBox3. Change it's Size property to "388, 264".


Now is the time to code Form1 and it's components. So now please pay attention and follow each and every step very carefully.

Double-click on TextBox1 and type in the following code (to the TextBox1_TextChanged event):
Code: Select all
Dim Contact As String = TextBox1.Text.ToString()
        Dim Index As Integer = ListBox1.FindString(Contact)
        If Index = -1 Then
            ListBox1.SelectedIndex = ListBox1.SelectedIndex
        Else
            ListBox1.SetSelected(Index, True)
        End If
Now go back to the Designer and double-click on Button1 aka "Add New" and type the following simple code:
Code: Select all
Form2.Show()
Add the following code to Button2 aka "Remove" by the same method as for Button1 code:
Code: Select all
If ListBox1.SelectedIndex < 0 Then
        Else
            Dim dlgrst As MsgBoxResult
            dlgrst = MessageBox.Show("Are you sure you want to remove the contact: '" & ListBox1.SelectedItem & "'?", "Remove Contact?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If dlgrst = MsgBoxResult.Yes Then
                My.Settings.Contacts.Remove(ListBox1.SelectedItem)
                My.Settings.Save()
                ListBox1.Items.Clear()
                For Each Name As String In My.Settings.Contacts
                    ListBox1.Items.Add(Name)
                Next
            ElseIf dlgrst = MsgBoxResult.No Then

            End If
        End If
Double-click the Form using the Designer window and enter the following code in the Form1_Load event:
Code: Select all
Try
            For Each Name As String In My.Settings.Contacts
                ListBox1.Items.Add(Name)
            Next
        Catch ex As Exception

        End Try

Okay, the Form1's coding is done so now let us head to design and code the Add New Contact form. So to add another form, look up at the menustrip, click Project and then press Add Windows Form... When the Add New Item dialog shows up, just press Add and a new form will be automatically created and shown in front of you.


Now lets start designing the form. Change the Form2's FormBorderStyle to FixedSingle. Change it's MaximizeBox and MinimizeBox property to False. Change it's ShowInTaskbar property to False. Change it's size to "378, 108". Change it's StartPosition property to CenterScreen and at last change it's Text property to "Add New Contact...".


Add the following items/controls to the Form:

(1)_ 2 Labels. Change Label1's Text property to "Name:" and Label2's to "Number:". Place both on the right side of the Form in a vertical manner.

(2)_ 2 TextBoxes. Place first one next to the first label and second one next to the second label.

(3)_ 2 Buttons. Change Button1's Text property to "Add" and Button2's to "Cancel". Place them as you wish next to the TextBoxes.


Now we will code Form2. So right-click on the Form2 so be attentive!

Double-click Button1 on the Form2's Designer window and type the following code:
Code: Select all
If My.Settings.Contacts Is Nothing Then
            My.Settings.Contacts = New System.Collections.Specialized.StringCollection
            If TextBox1.Text = "" Or TextBox2.Text = "" Then
                MsgBox("One or more fields are empty or invalid.", MsgBoxStyle.Exclamation, "Invalid Information")
            Else
                My.Settings.Contacts.Add(TextBox1.Text + " - " + TextBox2.Text)
                My.Settings.Save()
                Me.Close()
            End If
        Else
            If TextBox1.Text = "" Or TextBox2.Text = "" Then
                MsgBox("One or more fields are empty or invalid.", MsgBoxStyle.Exclamation, "Invalid Information")
            Else
                My.Settings.Contacts.Add(TextBox1.Text + " - " + TextBox2.Text)
                My.Settings.Save()
                Me.Close()
            End If
        End If
        TextBox1.Clear()
        TextBox2.Clear()
        Form1.ListBox1.Items.Clear()
        For Each Name As String In My.Settings.Contacts
            Form1.ListBox1.Items.Add(Name)
        Next
Now add this code to Button2 with the same technique as with Button1:
Code: Select all
Me.Close()

After you have done all that, go to the Project window by double clicking on My Project. Click the Settings tab on the right side of the window. Now add a new setting, "Contacts". Change it type to "System.Collections.Specialized.StringCollection".


---------------------------------------------------------------------------

Voila! You have just completed making a Phone Book. Now debug and run it by pressing the Start Debugging button located at the main toolstrip. Test it, if it works fine then its SAVING time. You can save your project by going to File -> Save All and then pressing Save.

And there! You have your project and application that works. If you have any problem or can't understand something, please feel free to ask by either a comment or by PM. I have also attached a screenshot.

Please use the Give Thanks button and the Reputation System to appreciate my hard work. I have wrote this tutorial while making a Phone Book project myself so you can download the source file in the attachments.

Thank you.

Image
Image
You do not have the required permissions to view the files attached to this post.
Image
User avatar
Skillful
Skillful Coders
Skillful Coders
Posts: 969
Joined: Tue Nov 16, 2010 10:07 am

Re: How to make a Phone Book
Skillful
Nice tut dude!Well for some reason i cant give you +rep coz its saying something you should spread your rep before giving it to the same user. Anyways keep up the good work.
Instead of LOL use this -
LSIBMHBIWFETALOL

Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

Re: How to make a Phone Book
Axel
CoderBoy1201 wrote:
Nice tut dude!Well for some reason i cant give you +rep coz its saying something you should spread your rep before giving it to the same user. Anyways keep up the good work.
viewreputation.php?id=547 this is why. You're the last repper :P
http://vagex.com/?ref=25000
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Re: How to make a Phone Book
Usman55
Thank you guys, and Axel, you should have commented lol.
Image
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Re: How to make a Phone Book
GoodGuy17
Nice tutorial, but it would be great if you included how to save all of the data in the listview.
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Re: How to make a Phone Book
Usman55
GoodGuy17 wrote:
Nice tutorial, but it would be great if you included how to save all of the data in the listview.
Explain a bit, I can't understand.
Image
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

Re: How to make a Phone Book
Axel
Oh yeah
Nice tutorial but you should make some options to encrypt it
and save it to a database :P
Cuz when obama wants to use this , he would store 10 000 000 Phone numbers and saving them into resources/settings won't be really safe/good :P
Last edited by Axel on Sun Jan 30, 2011 6:37 pm, edited 1 time in total.
http://vagex.com/?ref=25000
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Re: How to make a Phone Book
GoodGuy17
How can I save the contact info to a text file or something and load it too?
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Re: How to make a Phone Book
Usman55
I first added the feature to store into text file but then I didn't liked the idea, so I used settings. And Axel, Nice one lol.
Image
GoodGuy17
Coding God
Coding God
Posts: 1610
Joined: Mon Sep 07, 2009 12:25 am

Re: How to make a Phone Book
GoodGuy17
Could you add the saving to text feature then? Would make the tutorial a lot better.

P.S. Why don't you upgrade to Visual Basic 2010? It supports the higher standard in .NET Framework.
23 posts Page 1 of 3
Return to “Tutorials”