Phone Book Tutorial - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

For this tutorial you need 2 Forms

Form1:  Will be used to search contacts.

Form2: Will be used to add new contacts.

 

Form1: Add a textbox, a listbox, a label and 2 buttons:

Add the following textbox change event

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim item As String = TextBox1.Text.ToString()
        Dim index As Integer = ListBox1.FindString(item)

        If index = -1 Then
            ListBox1.SelectedIndex = ListBox1.SelectedIndex
        Else
            ListBox1.SetSelected(index, True)
        End If
    End Sub

Add "New Contact" button click code:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Form2.Show()
End Sub

Click on Project menu, then click on the last menu, (in this project: Simple Phone Book Properties)

Then click on Settings tab.

Add Names under Name Column, and System.Collections.Specialized.StringCollection under Type:

Add the following Form1.Load event code:


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            For Each Name As String In My.Settings.Names
                ListBox1.Items.Add(Name)
            Next
        Catch ex As Exception
        End Try
End Sub

Add the following code to "Remove Contact" Button:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ListBox1.SelectedIndex < 0 Then
        Else
            Dim ask As MsgBoxResult
            ask = MsgBox("Are you sure you want to remove " & ListBox1.SelectedItem & " ?", MsgBoxStyle.YesNo)
            If ask = MsgBoxResult.Yes Then
                My.Settings.Names.Remove(ListBox1.SelectedItem)
                My.Settings.Save()
                ListBox1.Items.Clear()
                For Each Name As String In My.Settings.Names
                    ListBox1.Items.Add(Name)
                Next
            ElseIf ask = MsgBoxResult.No Then

            End If
        End If
    End Sub

Form2: Add 2 textboxes, 2 labels and 2 buttons:

Add the following code to "Cancel" Button:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
End Sub

Add the following code to "Save" Button:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If My.Settings.Names Is Nothing Then
            If TextBox1.Text = "" Or TextBox2.Text = "" Then
                MsgBox("Must Fill all Fields")
            Else
                My.Settings.Names.Add(TextBox1.Text + " - " + TextBox2.Text)
                My.Settings.Save()
                Me.Close()
            End If
        Else
            If TextBox1.Text = "" Or TextBox2.Text = "" Then
                MsgBox("Must Fill all Fields")
            Else
                My.Settings.Names.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.Names
            Form1.ListBox1.Items.Add(Name)
        Next
        

End Sub


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.