It has been a while since my last post on this project. I have been held up with other things. But now it is time to move on.

The next thing to do is allow the user, in this case, me, to add, edit or delete contacts. Deleting is a subject which will need further investigation, but at this stage we will just allow simple deletes.

First we need a form. So we will create a new inherited form called ContactEditForm, and inherit from our BaseForm. On this form we will place some Labels, Text Boxes and Buttons, so that it looks like the diagram below.
Contact Edit Form

There are a couple of things to do, all of them fairly straightforward.

First, we have to be able to get to the form, and this is done from the Contact Form, using the New and Edit buttons on that form. We will create a new Contact object in the ContactEdit form by declaring a new private instance of the Contact class, and make it accessible by creating a public property, called Contact. All very standard. The code looks like this:

   Private m_Contact as new ContactEntity

   Public Property Contact() as ContactEntity
      Get
          Return m_Contact
      End Get
      Set(ByVal Value as ContactEntity)
          m_Contact = Value
      End Set
   End Property

Visual Basic is very verbose, and in C# the code to do the same thing is much more concise. The good thing is that the IDE does most of the work for you.

To edit a contact the user needs to select a contact from the Listview control. Selecting the contact creates a new ContactEntity object and this object is passed to the ContactEdit form in the Set part of the property. So m_Contact, in the ContactEdit form is the same object as the Contact selected in the Contact form. The code to open the ContactEdit form also contains a command to set the Text of the ContactEdit form to Edit Contact

If the New button is pressed then the ContactEdit form is created but the Text is set to New Contact

and the IsNew property of the ContactEntity is set to true.

In either case the form is then opened with the Show method.

Next we need to set up databinding. In previous versions of VB databinding was a sad joke, but it is much improved in VB.NET. There are a couple of ways to set up databinding. I usually do it in code like this.

txtSurname.Databindings.Add("Text",m_Contact,"Surname")

And so on for each of the text boxes. Then it is merely a matter of attaching a Save method to the Save button, and a Delete method to the Delete button. We will revisit Delete at a later date when we consider validation.

The Close button has a form.close method attached and we are almost done.

There are two final things to do. The first is to set the Tab order which is done in the designer from the View menu. You merely select Tab Order from the menu and click each control in the order you wish to tab through them.

Lastly, we set the AcceptButton of the form to Save and the CancelButton to Close, and we are done.