1

我有一个令我困惑的简单问题。因为我已经离开一段时间了,所以我需要用 VB 重新振作一下。我有一个添加新联系人的表单。通过按适当的按钮添加新联系人,它们会在表单的列表中显示为条目。我现在尝试添加一个编辑按钮来编辑现有条目。用户将在列表中选择一个给定条目并按下编辑按钮,然后将显示一个适当的表单 (AddContFrm)。现在它只是添加了另一个具有相同标题的条目。逻辑在一个名为 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
    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

添加ContFrm

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
4

1 回答 1

1

你还没有真正添加一个问题,但看看你的代码有点奇怪。
如果您单击添加,它将显示 frmAddCont,然后在该表单的确认按钮中它将保存数据,但如果您单击编辑,它将不会显示该表单,只会再次添加相同的数据。我认为您frmAddCont.Show()的编辑按钮处理程序中缺少 a 。

但是,总而言之,您将数据与 GUI 混合得太多。该类Contact应该一无所知frmAddCont,相反,主窗体中的 Add 和 Edit 按钮应该根据需要显示 frmAddCont (但我宁愿做ShowDialog而不是Show使其成为模态),如果它处于编辑模式,我会发送Contact要编辑的到frmAddCont,然后当用户按下确认时,我会Contact根据需要修改/创建,如果它是 Add 我会有一个方法,主窗体可以调用来获取新的Contact.
我认为 GUI 可以了解您的Contact课程,但该Contact课程现在应该知道有关表单的任何信息。

于 2010-04-28T18:22:52.347 回答