【问题标题】:issues with adding item to list in VB在VB中将项目添加到列表中的问题
【发布时间】:2016-05-09 00:12:37
【问题描述】:

这是 Visual Basic 的家庭作业(使用 Visual Studio 2013)。这是我在这个网站(或任何网站)上的第一个问题。

我正在尝试制作一个使用列表的电话簿。该目录应该能够搜索、添加和删除联系人。

在我的表单上,我有一个列表框,其中包含已通过集合属性输入的名称。我有一个输出相应电话号码的标签。我添加了 10 个姓名和 10 个电话号码。 (电话号码与列表一起添加 - 在设计时)

在运行时,用户应该能够单击“添加联系人”按钮将另一个联系人添加到列表中。我正在使用两个输入框;第一个提示输入要添加的名称,第二个提示输入电话号码。

我尝试添加的第一个联系人似乎运行良好。我的问题是当我尝试将第二个联系人添加到列表中时,第一个添加的电话号码会更改以反映为第二个添加添加的相同号码。 (看起来第二个联系电话号码覆盖了我为第一个添加添加的内容)为了清楚起见,当程序运行时,我从列表框中的 10 个姓名和 10 个号码开始,只有单击列表框中的名称。当我添加第一个时,计数为 11,然后添加第二个,计数变为 12。电话号码正在添加到列表中(不是列表框)。

我没有收到任何错误消息,只是信息没有正确放入列表(类似于数组的列表)。

这是我的代码:

如果我需要更清楚地了解问题所在,请告诉我。

仅供参考:我只是在提交问题之前再次对其进行了测试。我只是添加一个名称和一个数字来测试它。当我输入第一个添加的联系人时,我使用了数字 5。添加了第二个联系人,我使用了数字 6。当我当时检查时,第一个联系人显示为 6,第二个联系人显示为 6。但是,我测试了 3添加了联系人。第一次添加的号码相同 (5),第二次添加的号码相同 (6),第三次添加的联系人使用 7。当我查看结果时,第一个添加的联系人更改为 7,第二个联系人保持在 6,第三个联系人显示为 7。我不知道这是怎么回事??

Option Explicit On
Option Strict On

'=========== Class mainForm =================
Public Class mainForm

    Const MAX_SUBSCRIPT_Integer As Integer = 9

    Dim inputNameString As String                'user contact name input
    Dim inputPhoneString As String               'user phone number input

    Private phoneList As New List(Of String) 

    '============== mainForm_Load ====================

    Private Sub mainForm_Load(ByVal sender As Object, _
         ByVal e As System.EventArgs) Handles Me.Load
        splashForm.ShowDialog()

        phoneList.Add("555 - 266 - 9563")
        phoneList.Add("555 - 266 - 5461")
        phoneList.Add("555 - 266 - 7412")
        phoneList.Add("555 - 266 - 5642")
        phoneList.Add("555 - 266 - 6721")
        phoneList.Add("555 - 266 - 1465")
        phoneList.Add("555 - 266 - 3541")
        phoneList.Add("555 - 266 - 2874")
        phoneList.Add("555 - 266 - 9114")
        phoneList.Add("555 - 266 - 2245")

    End Sub

    '======= namesListBox_SelectedIndexChanged() =====================
    Private Sub namesListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles namesListBox.SelectedIndexChanged

        If namesListBox.SelectedIndex >= 0 Then
            phoneNumberLabel.Text = (phoneList(CInt(namesListBox.SelectedIndex.ToString())))
            Dim count As Integer
            contactPictureBox.Image = contactImageList.Images(namesListBox.SelectedIndex)
            count += 1

        End If

    End Sub

    '============== addButton_Click =====================

    Private Sub addButton_Click(sender As Object, e As EventArgs) Handles addButton.Click

        inputNameString = (InputBox("Enter name of Contact. Ex. Doctor: Dr. Sigmund Freud ", "Add Contact"))
        inputPhoneString = (InputBox("Enter contact phone number. Ex. 555-555-1212", "Add Contact Phone Number"))
        namesListBox.Items.Add(inputNameString.ToString())
        phoneList.Add(inputPhoneString.ToString())
        AddElementToPhoneString(inputPhoneString)

    End Sub

    '============== AddElementToPhoneString() ========================

    Public Sub AddElementToPhoneString(ByVal stringToAdd As String)

        phoneList(MAX_SUBSCRIPT_Integer + 1) = stringToAdd
        Dim countInteger As Integer
        countInteger = phoneList.Count()
        countLabel.Text = CStr(CInt(phoneList.Count))

    End Sub

End Class

【问题讨论】:

  • 不是将名称存储在列表中,将数字存储在 UI 控件中,您的列表可以包含这两种信息。相关数据应保存在一起。示例见this answer
  • @Plutonix 我知道我想要我的程序是如何运行的。如果你看过我的 GUI,你可能会明白。我会检查你给我的链接。这是我编程的第一个学期,所以我还是个新手。谢谢
  • @Plutonix 你知道我是否需要常量 MAX_SUBSCRIPT_Integer,因为列表会自行调整大小?
  • 看这可能是你的第一个问题,但它还不够好。您有大量文本。不。只要坚持手头的实际问题。你有大量的代码。不。只需包含失败的代码。这导致了下一个问题。您甚至不知道代码的哪一部分是问题所在。不。在你问之前花点时间弄清楚问题出在哪里。最重要的是,你没有一个实际的问题,只是一个暗示的问题。 I have no idea what is going on?? 不是问题。
  • 感谢上帝!使用 Option Strict On 和 Option Explicit On 的新程序员!以这种优秀的态度,你会走得很远! @Plutonix - 当然,仅此一项就值得一票吗? ;)

标签: vb.net visual-studio list


【解决方案1】:

看起来可能存在一些问题,但现在让您感到困惑的是:

phoneList(MAX_SUBSCRIPT_Integer + 1) = stringToAdd

您已将 MAX_SUBSCRIPT_Integer 设置为常量,因此无论您添加了多少项,您总是将第 10 项添加到列表中。你应该这样做:

phoneList.add(stringToAdd)

这将确保您始终向列表中添加新项目,而不是覆盖最后一个项目。

【讨论】:

  • 感谢您的 cmets。但是,我确实想出了如何解决它。我将上传编辑后的版本。我想我需要设置一个电子邮件来获取答案,因为我一直在检查我的电子邮件而不是在这里。我仍在通过这个程序工作,现在我正在研究删除功能,它也没有按照它的编码方式工作。我让它退出崩溃,但现在代码似乎没有做任何事情,就从列表框中删除联系人而言。我需要作为另一个问题重新发布吗?
  • 最好针对不同的问题发布一个新问题,是的。
  • 我解决了这个问题...我正在使用 Remove 并将其更改为 RemoveAt 现在它正在工作:) 现在开始处理搜索联系人文本框...
【解决方案2】:

这是添加联系人问题的解决方法:我完全删除了 AddElementToPhoneString。我认为它正在做一些富有成效的事情,但它实际上是导致问题的原因。

选项显式打开 选项严格开启

'=========== 类 mainForm ================= 公共类mainForm

Const MAX_SUBSCRIPT_Integer As Integer = 9

Dim inputNameString As String                'user contact name input
Dim inputPhoneString As String

Private phoneList As New List(Of String) 'user phone number input

'============== mainForm_Load ====================

Private Sub mainForm_Load(ByVal sender As Object, _
     ByVal e As System.EventArgs) Handles Me.Load
    splashForm.ShowDialog()

    phoneList.Add("555 - 266 - 9563")
    phoneList.Add("555 - 266 - 5461")
    phoneList.Add("555 - 266 - 7412")
    phoneList.Add("555 - 266 - 5642")
    phoneList.Add("555 - 266 - 6721")
    phoneList.Add("555 - 266 - 1465")
    phoneList.Add("555 - 266 - 3541")
    phoneList.Add("555 - 266 - 2874")
    phoneList.Add("555 - 266 - 9114")
    phoneList.Add("555 - 266 - 2245")

End Sub

'======= namesListBox_SelectedIndexChanged() ====================
Private Sub namesListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles namesListBox.SelectedIndexChanged

    If namesListBox.SelectedIndex >= 0 Then
        phoneNumberLabel.Text = (phoneList(CInt(namesListBox.SelectedIndex.ToString())))
        Dim count As Integer
        contactPictureBox.Image = contactImageList.Images(namesListBox.SelectedIndex)
        count += 1

    End If

End Sub

'============== addButton_Click =====================

Private Sub addButton_Click(sender As Object, e As EventArgs) Handles addButton.Click

    inputNameString = (InputBox("Enter name of Contact. Ex. Doctor: Dr. Sigmund Freud ", "Add Contact"))
    inputPhoneString = (InputBox("Enter contact phone number. Ex. 555-555-1212", "Add Contact Phone Number"))
    namesListBox.Items.Add(inputNameString.ToString())
    phoneList.Add(inputPhoneString.ToString())

End Sub

结束类

【讨论】:

    猜你喜欢
    • 2020-03-02
    • 2015-07-21
    • 2015-10-31
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    相关资源
    最近更新 更多