tags:

views:

37

answers:

1

Hi there,

I have a simple questions that puzzles me. I need a little bit of refreashment with VB as I have been away for a while. I have a form that adds new contacts. New contacts are added by pressing an appropriate button and they appear as an entry in the list on the form. I try now to add an edit button that will edit existing entries. User will select a given entry on the list and press edit button and will be presented with an appropriate form (AddContFrm). Right now it simply adds another entry with the same title. Logic is handled in a class called Contact.vb Here is my code.

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
    Sub NewContact()
        FName = frmAddCont.txtFName.ToString
        frmStart.lstContact.Items.Add(FName)
        frmAddCont.Hide()
    End Sub
    Public Sub Display()
        Dim C As New Contact
        'C.Cont = InputBox("Enter a title for this 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.Cont.ToString)
        frmStart.lstContact.Items.Add(C)
    End Sub
End Class

AddContFrm

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 C As New Contact
        C.Display()
        Me.Hide()

    End Sub

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

    End Sub
End Class

and frmStart.vb

Public Class frmStart

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        frmAddCont.Show()
    End Sub

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

        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
        Dim C As Contact
        If lstContact.SelectedItem IsNot Nothing Then
            C = DirectCast(lstContact.SelectedItem, Contact)
            C.Display()
        End If
    End Sub
End Class
+1  A: 

You haven't really added a question but looking at your code it's a bit weird.
If you click Add it will show frmAddCont and then in the confirm button of that form it'll save the data, but if you click Edit it won't show the form and will only add the same data again. I think you're missing a frmAddCont.Show() in your edit button handler.

However, all in all, you're mixing data with GUI too much. The Contact class should know nothing about frmAddCont, rather, the Add and Edit buttons in the main form should show frmAddCont as required (but I would do ShowDialog rather than Show to make it Modal) and if it's in edit mode I'd send in the Contact to be edited to frmAddCont and then when the user press confirm I'd amend/create the Contact as needed and if it's an Add I'd have a method that the main form could call to get out the new Contact.
I think it's fine for the GUI to know about your Contact class, but the Contact class should now know anything about the forms.

ho1