tags:

views:

24

answers:

2

Hi,

I'm writing a simple application - address book. User enters new addresses and they are added as an entry to a list visible on the main form (frmStart). I use one form to add and edit (AddContForm). Add button on the frmStart works fine, however I experience some problems with the edit button as when I press it and enter new data they are added as new entry however the previous entry is still there. Logic is handled by Contact.vb class. Please let me know how to fix this problem. Here is the code:

Contact.vb

Public Class Contact
    Public Contact As String
    Public Title As String
    Public Fname As String
    Public Surname As String
    Public Address As String
    Private myCont As String
    Public Property Cont()
        Get
            Return myCont
        End Get
        Set(ByVal value)
            myCont = Value
        End Set
    End Property
    Public Overrides Function ToString() As String
        Return Me.Cont
    End Function

    Public Sub Display()
        Dim C As New Contact
        C.Cont = frmAddCont.txtTitle.Text
        C.Fname = frmAddCont.txtFName.Text
        C.Surname = frmAddCont.txtSName.Text
        C.Address = frmAddCont.txtAddress.Text
        frmStart.lstContact.Items.Add(C)
    End Sub
End Class

Here is frmStart.vb

Public Class frmStart
    Public Button As String

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Button = ""
        Button = "Add"
        frmAddCont.ShowDialog()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click

        Button = ""
        Button = "Del"
        Dim DelCont As Contact
        DelCont = Me.lstContact.SelectedItem()


        lstContact.Items.Remove(DelCont)

    End Sub

    Private Sub lstContact_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstContact.SelectedIndexChanged

    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Button = ""
        Button = "Edit"
        Dim C As Contact

        If lstContact.SelectedItem IsNot Nothing Then

            C = DirectCast(lstContact.SelectedItem, Contact)
            frmAddCont.ShowDialog()

        End If
    End Sub
End Class

Here is AddContFrm.vb

Public Class frmAddCont
    Public Class ControlObject
        Dim Title As String
        Dim FName As String
        Dim SName As String
        Dim Address As String
        Dim TelephoneNumber As Integer
        Dim emailAddress As String
        Dim Website As String
        Dim Photograph As String

    End Class

    Private Sub btnConfirmAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirmAdd.Click

        Dim B As String
        B = frmStart.Button

        Dim C As New Contact

        C.Display()
        Me.Hide()
        If B = "Edit" Then
            C = DirectCast(frmStart.lstContact.SelectedItem, Contact)
            frmStart.lstContact.SelectedItems.Remove(C)
        End If     

     End Sub

    Private Sub frmAddCont_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

End Class

A: 

If you're going to use the shared instance of frmAddCont, then it'll remember the data each time you show it, so you'd have to add some kind of Initialize method where you initialize all the controls to the defaults you want (for example empty textboxes).

However, the other way around it would be to do something like:

using(dlg as new frmAddCont)
{
    If(dlg.ShowDialog() = DialogResult.OK)
    {
         Dim C As Contact = dlg.GetNewContact()
    }
}

Please note, the above code is not tested and is just meant as a sample rather than the exact code you need.

ho1
A: 

I really don't care much for whether the data on the AddForm is the old data or the new data. I don't understand how this code would help me achieve my goal. I think the problem now is that when I click edit previous entry on the list is not deleted (what I believe I have indicated in my question) - I tried to delete it (by using the if condition and checking what button was pressed) however it seems it is not working properly for some reason. I don't understand how your code could help me - please if possible explain it better.

thx

niuchu