根据列表框字符串删除集合中的项目。

问题描述 投票:0回答:1

点击删除按钮时有问题。如果我的代码需要更多,请告诉我。我在AddressList.Remove(selectedName)这一行得到这个错误。

System.ArgumentException: 'Argument 'Key' is not a valid value.

我尝试了许多不同的方法,但不明白为什么不能工作。我认为这与列表框中字符串的连接方式有关。我需要能够从集合和列表框中删除条目。任何帮助将是非常感激的。

Module EmailCollection
Public AddressList As New Collection

Public Sub AddRecord(ByVal a As cAddress)
    Try
        AddressList.Add(a)
    Catch ex As Exception
        MessageBox.Show("Error: inputs must be characters valid in string format")
    End Try
End Sub

End Module
public class form1 

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim frmAdd As New AddNewName

        frmAdd.ShowDialog()

        UpdateListBox()


    End Sub
Private Sub UpdateListBox()
        lstAddress.Items.Clear()

        Dim a As cAddress
        For Each a In AddressList
            lstAddress.Items.Add(String.Concat(a.strName, a.strEmail, a.strPhone, a.strComment))
        Next

        If lstAddress.Items.Count > 0 Then
            lstAddress.SelectedIndex = 0

        End If
    End Sub
Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click

        Dim selectedName As String
        Try
            ' Get the selected value.
            selectedName = lstAddress.SelectedItem.ToString()

            ' Remove the selected name from the list box and the collection.
            If MessageBox.Show("Are you sure?",
                               "Confirm Deletion",
                               MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then

                lstAddress.Items.Remove(selectedName)
                AddressList.Remove(selectedName)
            End If

        Catch ex As NullReferenceException
            MessageBox.Show("Select an item to remove.", "Selection Needed")
        End Try
    End Sub
end class
vb.net
1个回答
0
投票

在您的模块中,我修改了 AddressList 从旧的VB6 Collection 类型到.net List(Of T). T代表的是类型。

Module EmailCollection

    Public AddressList As New List(Of cAddress)

    Public Sub AddRecord(ByVal a As cAddress)
        AddressList.Add(a)
    End Sub

End Module

我猜到你的 cAddress 类看起来像这样。我添加了一个自定义的 .ToString 方法,所以列表框将显示你想要的数据,但项目本身将是一个 cAddress 对象。

Public Class cAddress
    Public Property strName As String
    Public Property strEmail As String
    Public Property strPhone As String
    Public Property strComment As String

    Public Overrides Function ToString() As String
        Return $"{strName}, {strEmail}, {strPhone}, {strComment}"
    End Function
End Class

在表格中...

我没有在列表框中添加字符串,而是添加了 cAddress 对象。列表框调用 .ToString 来获取显示值。

Private Sub UpdateListBox()
    ListBox1.Items.Clear()
    For Each a As cAddress In AddressList
        ListBox1.Items.Add(a)
    Next
    If ListBox1.Items.Count > 0 Then
        ListBox1.SelectedIndex = 0
    End If
End Sub

在删除按钮中,我将所选项目投向其底层类型。cAddress. 这是被删除的项目。AddressList. 然后从列表框中删除所选项目即可。

Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
    If MessageBox.Show("Are you sure?",
                           "Confirm Deletion",
                           MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
        AddressList.Remove(DirectCast(ListBox1.SelectedItem, cAddress))
        ListBox1.Items.Remove(ListBox1.SelectedItem)
    End If
End Sub

我把列表框的名字改为 ListBox1 来匹配我的测试项目。


0
投票

这里可以试试,使用BindingSource来设置ListBox。在类中把ToString覆盖到ListBox中要显示的内容,而不是像你现在这样没有DataSource。

我的版本中你的类有名字和属性名的变化。

Public Class Address
    Public Property Name() As String
    Public Property Email() As String
    Public Property Phone() As String
    Public Property Comment() As String

    Public Overrides Function ToString() As String
        Return $"{Name} {Email} {Phone} {Comment}"
    End Function
End Class

Mocked数据被用来填充ListBox。

Public Class Form1
    Private ReadOnly _bsAddresses As New BindingSource

    Private Sub UpdateListBox()
        Dim AddressList = New List(Of Address) From
                {
                    New Address() With {
                        .Name = "John",
                        .Email = "[email protected]",
                        .Phone = "555-444-3456",
                        .Comment = "AAA"},
                    New Address() With {
                        .Name = "Mary",
                        .Email = "[email protected]",
                        .Phone = "888-333-2222",
                        .Comment = "BBB"},
                    New Address() With {
                        .Name = "Bob",
                        .Email = "[email protected]",
                        .Phone = "111-555-9999",
                        .Comment = "CCC"}
                }

        _bsAddresses.DataSource = AddressList

        lstAddress.DataSource = _bsAddresses

        lstAddress.SelectedIndex = 0

    End Sub

    Private Sub Form1_Shown(sender As Object, e As EventArgs) _
        Handles Me.Shown

        UpdateListBox()

    End Sub

    Private Sub RemoveButton_Click(sender As Object, e As EventArgs) _
        Handles RemoveButton.Click

        If lstAddress.Items.Count > 0 AndAlso lstAddress.SelectedItem IsNot Nothing Then
            Dim address = CType(_bsAddresses.Current, Address)
            If My.Dialogs.Question($"Remove {address.Name}") Then
                _bsAddresses.RemoveCurrent()
                RemoveButton.Enabled = _bsAddresses.Count > 0
            End If
        End If

    End Sub
End Class

用于提问的代码模块

Namespace My
    <ComponentModel.EditorBrowsable(ComponentModel.EditorBrowsableState.Never)>
    Partial Friend Class _Dialogs
        Public Function Question(text As String) As Boolean
            Return (MessageBox.Show(
                text,
                My.Application.Info.Title,
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question,
                MessageBoxDefaultButton.Button2) = MsgBoxResult.Yes)
        End Function
    End Class
    <HideModuleName()>
    Friend Module WinFormsDialogs
        Private instance As New ThreadSafeObjectProvider(Of _Dialogs)
        ReadOnly Property Dialogs() As _Dialogs
            Get
                Return instance.GetInstance()
            End Get
        End Property
    End Module
End Namespace

enter image description here


0
投票

凯伦的帖子似乎相当全面。 我的回答是试图集中在您的直接问题上。

我没有看到您的代码中显示的所有类型定义,但在回答您的问题时,我相信您的问题是 "为什么我得到System.ArgumentException: 'Argument 'Key'is not a valid value'"。

在这行错误的代码中

AddressList.Remove(selectedName)

AddressList是一个集合 要使用 .Remove你必须传入一个地址列表集合的对象来删除它。 你传递的是一个简单的字符串,这不是一个AddressList对象。 你需要根据你的字符串创建一个对象。selectedName 通入 .Remove 而这一行应该可以用。 你所做的其余部分似乎更复杂。

© www.soinside.com 2019 - 2024. All rights reserved.