VB.NET 使用表单编辑现有内容
我有一个简单的问题让我困惑。由于我已经离开有一段时间了,所以我需要稍微复习一下 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
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还没有真正添加问题,但看看您的代码有点奇怪。
如果您单击添加,它将显示 frmAddCont,然后在该表单的确认按钮中,它将保存数据,但如果您单击编辑,它将不会显示表单,只会再次添加相同的数据。我认为您在编辑按钮处理程序中缺少
frmAddCont.Show()
。然而,总而言之,您将数据与 GUI 混合得太多了。
Contact
类应该对frmAddCont
一无所知,相反,主窗体中的“添加”和“编辑”按钮应根据需要显示 frmAddCont(但我会执行ShowDialog 而不是
Show
使其成为模态),如果处于编辑模式,我会将要编辑的Contact
发送到frmAddCont
并然后,当用户按下确认时,我将根据需要修改/创建Contact
,如果是添加,我将有一个主表单可以调用的方法来获取新的Contact< /代码>。
我认为 GUI 可以了解您的
Contact
类,但是Contact
类现在应该了解有关表单的任何信息。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 aboutfrmAddCont
, rather, the Add and Edit buttons in the main form should show frmAddCont as required (but I would doShowDialog
rather thanShow
to make it Modal) and if it's in edit mode I'd send in theContact
to be edited tofrmAddCont
and then when the user press confirm I'd amend/create theContact
as needed and if it's an Add I'd have a method that the main form could call to get out the newContact
.I think it's fine for the GUI to know about your
Contact
class, but theContact
class should now know anything about the forms.