18.01.2013 Views

Mastering Visual Basic .NET

Mastering Visual Basic .NET

Mastering Visual Basic .NET

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

method, so we must provide our own ToString method that describes each contact. The company<br />

name should be adequate, but if there are two companies by the same name, you can use another<br />

field to differentiate them. I’ve used the contact name, but you can use any of the other properties<br />

(the URL would be a good choice).<br />

Each contact is stored in a variable of the Contact type and added to the ListBox control. Now,<br />

we must change the code of the main form a little. First, remove the navigational buttons; we no<br />

longer need them. Their function will be replaced by a few lines of code in the ListBox control’s<br />

SelectedIndexChanged event. Every time the user selects another item on the list, the statements<br />

shown in Listing 8.29 display the contact’s properties on the various TextBox controls on the form.<br />

Listing 8.29: Displaying the Fields of the Selected Contact Object<br />

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, _<br />

ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged<br />

currentContact = ListBox1.SelectedIndex<br />

ShowContact()<br />

End Sub<br />

The ShowContact() subroutine reads the object stored at the location specified by the current-<br />

Contact variable and displays its properties on the various TextBox controls on the form.<br />

When a new contact is added, the code creates a new Contact object and adds it to the ListBox control.<br />

When a contact is edited, a new Contact object replaces the currently selected object on the control.<br />

The code is very similar to the code of the Contacts application. I should mention that the ListBox<br />

control is locked while a contact is being added or edited, because it doesn’t make sense to select<br />

another contact at that time. Besides, we want to be able to replace the contact being edited when the<br />

user is done.<br />

To delete a contact (Listing 8.30), we simply remove the currently selected object on the control.<br />

In addition, we must select the next object, or the first object if the deleted object was the last<br />

one in the list.<br />

Listing 8.30: Deleting an Object on the ListBox<br />

Private Sub bttnDelete_Click(ByVal sender As System.Object, _<br />

ByVal e As System.EventArgs) Handles bttnDelete.Click<br />

If currentContact > -1 Then<br />

ListBox1.Items.RemoveAt(currentContact)<br />

If currentContact = ListBox1.Items.Count Then _<br />

currentContact = ListBox1.Items.Count - 1<br />

If currentContact = -1 Then<br />

ClearFields()<br />

MsgBox(“There are no more contacts”)<br />

Else<br />

ShowContact()<br />

End If<br />

A “REAL” CLASS<br />

365

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!