Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Personal Phone Numbers Rolodex, Using WinForms

0.00/5 (No votes)
13 Oct 2011 1  
This app stores First Name, Last Name, and Phone Numbers in rolodex style. This is the WinForms version of Steve Krile's Ajax Slider using SSDiver2112's CButton and gTrackBar controls.
PCRolodex1.jpg

PCRolodex2.jpg

Introduction

When I first saw the Ajax Rolodex, by Steve Krile, in the featured articles section, I wanted to see if I could re-create it using WinForms, DataGridView, and SSDiver2112's CButton and gTrackBar controls. I had to choose between Visual Studio's components or SSDiver2112's components. I chose the latter. Next was how to display the data to the user, of course I used the DataGridView. Then it was either a SQL database or a comma delimited file. I chose the file. Don't ask why, I just did.

Background

I tried creating different looking forms that displayed all the information of a certain person. It consisted of FirstName, LastName, Address, City, State, Zip, Phone, Cell, E-mail and so on. I got tired of entering all that information. If I don't know where a friend lives or where my favorite lumber yard is, I don't need to go there. Besides, if they send me an X-Mas card, I can always use the return address. This is why I settled for the "LastName,FirstName,PhoneNumber," comma delimited file.

DataGridView in Short

I have seen in different articles, here and other code banks, that the DataGridView is pretty hard getting use to. But I found, that, the more you use it, the easier it is to work with. The DataGridView has grown from VB 5,6 to a powerful component in dotNet. This is why it overwhelms and probably discourages some coders from using it. Keep working with it. After using it awhile, it will become second nature to you.

Using the Code

I have deleted the contacts.txt file. It will be created when you first run the application. DO NOT click or use any tabbed buttons or the trackbar as you will see run-time exceptions collapsing around you. There must be at least one entry for each letter of the alphabet. For now, just add all your information and I will be adding exception handling and re-posting it later.

There are two ways to enter your information, the first way is to run the app and click the cbtnAddContacts button. This will bring up the little form displayed above. Be sure to add one entry for each letter of the alphabet. The second way is to run the application, then close it. Open Windows Explorer and find your way to the Application.StartUp path (Bin\Debug) and open the newly created text file and add your info this way. Each line should look like this... Bunny,Bugs,212-555-1234, or Lumber,Tyson's Lumber,212-555-5678. With that out of the way... let's continue.

Using the Code... Continued

There are 28 button_clickButtonArea events. We will only look at one(cbtnAll_ClickButtonArea). This opens the contacts.txt file, sets the gTrackBar.Value, gets a row count that we need for the DataGridView.RowCount, then closes the File. Next we re-open the file to extract all the information and display it in the GridView.

Private Sub cbtnAll_ClickButtonArea(ByVal Sender As Object, _
                  ByVal e As System.Windows.Forms.MouseEventArgs) _
		Handles cbtnAll.ClickButtonArea
        gtb1.Value = 0

        Dim i As Integer = 0
        Dim rowCounter As Integer = 0
        Dim fs As FileStream
        Dim sr As StreamReader
        Dim strFile As String
        Dim parts() As String

        dgv1.SelectAll()
        dgv1.ClearSelection()

        'We just get the row count here.
        Try
            fs = New FileStream(strContacts, FileMode.Open, FileAccess.Read)
            sr = New StreamReader(fs)
            strFile = sr.ReadLine()

            Do Until strFile Is Nothing
                rowCounter += 1
                strFile = sr.ReadLine
            Loop
            fs.Close()
        Catch ex As Exception

        End Try

        dgv1.RowCount = rowCounter

        Try
            fs = New FileStream(strContacts, FileMode.Open, FileAccess.Read)
            sr = New StreamReader(fs)
            strFile = sr.ReadLine()

            For i = 0 To dgv1.RowCount - 1
                parts = strFile.Split(",")
                dgv1.Rows(i).Cells(0).Value = parts(0)   'Last Name
                dgv1.Rows(i).Cells(1).Value = parts(1)   'First Name
                dgv1.Rows(i).Cells(2).Value = parts(2)   'Phone Number
                dgv1.Update()
                strFile = sr.ReadLine()

                If sr.EndOfStream Then
                    Exit For
                End If
            Next

            fs.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

The 26 alphabet tabbed buttons all have the same agenda... Set the gTrackBar.Value to the correct position.

Private Sub cbtnA_ClickButtonArea(ByVal Sender As Object, _
                  ByVal e As System.Windows.Forms.MouseEventArgs) _
		Handles cbtnA.ClickButtonArea
        gtb1.Value = 1
    End Sub

    Private Sub cbtnB_ClickButtonArea(ByVal Sender As Object, _
	     	ByVal e As System.Windows.Forms.MouseEventArgs) _
		Handles cbtnB.ClickButtonArea
        gtb1.Value = 2
    End Sub

    Private Sub cbtnC_ClickButtonArea(ByVal Sender As Object, _
	         ByVal e As System.Windows.Forms.MouseEventArgs) _
		Handles cbtnC.ClickButtonArea
        gtb1.Value = 3
    End Sub
	
	'...D thru Z continues	

When the gTrackBar.Value is changed, the gTrackBar.ValueChanged event is fired. For each value in the gTrackBar we set the label lblLetter.Text value to the current letter on the track bar's track. Then we call the sub OpenTextFile(ByVal fName As String). This is where everything happens for the letters (A to Z). Here, we create a temp file (temp.txt) to hold the current letters selected information. We get a row count for the grid, add the data that fits our selected letter to the new temp file, then close and dispose. Next we clean out the DataGridView, Read from the main file(contacts.txt) and write to the temp file(temp.txt), set its DataGridView.RowCount, re-open the temp file and add the data to the DataGridView. Then we start all over by deleting the temp file and re-creating it.

Private Sub OpentextFile(ByVal fname As String)
        Dim i As Integer = 0
        Dim rowCounter As Integer = 0
        Dim ts As StreamReader
        Dim fs As FileStream
        Dim sw As StreamWriter
        Dim sr As StreamReader
        Dim strFile As String

        Dim parts() As String

        If File.Exists(strTemp) Then
            File.Delete(strTemp)
        End If

        'Then re-create it.
        ts = New StreamReader(File.Create(strTemp))
        ts.Close()
        ts.Dispose()

        'Clean out the DataGridView.
        dgv1.SelectAll()
        dgv1.ClearSelection()

        'We just get the row count here.
        Try
            fs = New FileStream(fname, FileMode.Open, FileAccess.Read)
            sw = New StreamWriter(strTemp, True)
            sr = New StreamReader(fs)
            strFile = sr.ReadLine()
            
            Do Until strFile Is Nothing
                parts = strFile.Split(",")
                If parts(0).StartsWith(lblLetter.Text) Then
                    rowCounter += 1
                    sw.WriteLine(strFile)
                Else
                    'Do nothing
                End If
                strFile = sr.ReadLine
            Loop

            'Do Clean up.
            fs.Close()
            sw.Close()
            sw.Dispose()
            sr.Close()
        Catch ex As Exception
        End Try

        dgv1.RowCount = rowCounter

        Try
            sr = File.OpenText(strTemp)
            strFile = sr.ReadLine()

            For i = 0 To dgv1.RowCount - 1
                parts = strFile.Split(",")
                If parts(0).StartsWith(lblLetter.Text) Then

                    dgv1.Rows(i).Cells(0).Value = parts(0)   'Last Name
                    dgv1.Rows(i).Cells(1).Value = parts(1)   'First Name
                    dgv1.Rows(i).Cells(2).Value = parts(2)   'Phone Number
                    dgv1.Update()
                Else
                    'Do nothing
                End If
                strFile = sr.ReadLine()
            Next

            'Do Clean up.
            sr.Close()
            sr.Dispose()
        Catch ex As Exception
        End Try
    End Sub

Add Contacts Form

Not too much goes on here. All we do is open the contacts.txt file and append what we type in the textboxes on the form. Code below...

Private Sub cbtnSave_ClickButtonArea(ByVal Sender As Object, _
                  ByVal e As System.Windows.Forms.MouseEventArgs) _
		Handles cbtnSave.ClickButtonArea

        'If something is blank, Get it corrected.
        If txtLastName.Text = "" Or txtFirstName.Text = _
		"" Or txtPhoneNumber.Text = "" Then
            	MessageBox.Show("Make sure ALL text boxes are filled in.", _
		"Info to the Rescue!", _
		MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        If File.Exists(strPath) Then
            'Set the FileStream and StreamWriter
            fs = New FileStream(strPath, FileMode.Append)
            sw = New StreamWriter(fs)

            'Set and Write our data.
            strAppend = Trim(txtLastName.Text) & "," & Trim(txtFirstName.Text) & _
			            "," & Trim(txtPhoneNumber.Text) & "," & vbCrLf
            sw.WriteLine(strAppend)

            'Clean-Up on aisle 7.
            CleanUp()
            sw.Close()
            fs.Close()
            sw.Dispose()
            fs.Dispose()
        Else
            MessageBox.Show("File seams to be missing. Would you like to create it?", _
			"ERROR", MessageBoxButtons.YesNo, MessageBoxIcon.Error)
            If ret = Windows.Forms.DialogResult.Yes Then
                File.Create(strPath)

                'Display Success! if the file was created.
                MessageBox.Show("File created...(contacts.txt)", "Success!", _
			MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                'Do nothing...Let the user add his/her contacts.
            End If
        End If
    End Sub

Points of Interest

History

  • First version uploaded on 10·13·2011

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here