2010-04-28 55 views
1

我有一個簡單的問題,困惑我。因爲我已經離開了一段時間,所以我需要對VB進行一些改動。我有一個添加新聯繫人的表單。通過按下適當的按鈕添加新的聯繫人,並且它們以表格的列表形式出現在條目中。我現在嘗試添加編輯按鈕來編輯現有條目。用戶將在列表中選擇一個給定的條目,然後按編輯按鈕,並將顯示一個適當的表單(AddContFrm)。現在它只是添加具有相同標題的另一個條目。邏輯在名爲Contact.vb的類中處理。這是我的代碼。VB.NET編輯現有的表格

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 

和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 
+0

我可以告訴你使用我以前的問題的建議。很高興它解決了。你忘記標記答案了,讓我們先關注一下。 – 2010-04-28 19:01:11

+0

我該如何標記答案? – niuchu 2010-04-29 15:41:52

回答

1

你還沒有真正加入了問題,而是看你的代碼,這是一個有點怪異。
如果你點擊添加,它會顯示frmAddCont,然後在確認按鈕的那個窗體它會保存數據,但如果你點擊編輯它不會顯示窗體,只會再次添加相同的數據。我想你的編輯按鈕處理程序中缺少frmAddCont.Show()

但是,總而言之,您將數據與GUI混合得太多。 Contact類應該對frmAddCont一竅不通,相反,主窗體中的添加和編輯按鈕應該根據需要顯示frmAddCont(但我會做ShowDialog而不是Show來使它成爲Modal),如果它處於編輯模式,我會發送Contact將被編輯爲frmAddCont,然後當用戶按確認我會修改/創建Contact根據需要,如果它是一個添加我會有一種方法,主窗體可以調用以獲得新的Contact
我認爲GUI可以知道您的Contact課程,但Contact課程現在應該知道有關表單的任何信息。