我正在寫一個簡單的應用程序 - 地址簿。用戶輸入新地址,並將它們作爲條目添加到主窗體(frmStart)上可見的列表中。我使用一種形式來添加和編輯(AddContForm)。在frmStart上的添加按鈕工作正常,但是我遇到了編輯按鈕的一些問題,因爲當我按下它並輸入新數據時,它們被添加爲新條目,但是前一條目仍然存在。邏輯由Contact.vb類處理。請讓我知道如何解決這個問題。下面是代碼:VB.NET處理不同形式的數據
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
下面是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
下面是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
更新:
我真的不在乎AddForm上的數據是舊數據還是新數據。我不明白這個代碼如何幫助我實現我的目標。我認爲現在的問題是,當我點擊編輯列表中的前一個條目沒有被刪除(我相信我已經在我的問題中指出了) - 我試圖刪除它(通過使用if條件並檢查按下了哪個按鈕)它似乎由於某種原因無法正常工作。我不明白你的代碼可以如何幫助我 - 請儘可能解釋一下。