如何防止在列表框中添加特定的重复单词

How To prevent To Add specific duplicate Word in List Box

如果我能防止添加特定的重复单词,我会很受伤, 我不是在谈论项目

我说要防止添加特定的单词,如果它已经存在于列表框中,所以如果找到那个特定的单词,不会再次添加它,但同时可以将它更改为另一个值 "Doby"

例如:

我有(1 个文本框,1 个列表框,1 个按钮)

所以我在文本框中输入"Dianna",如果在列表框中找到,那么它会阻止在列表框中添加重复的"Dianna"名称,但同时会改变,只有"Dianna" 从列表框创建的单词 To "Doby"

这可能吗,我已经创建了代码,来检查它是否已经建立,但是问题 (我无法阻止在同一个列表框中添加特定的重复单词,也无法将特定单词 "dianna" 更改为 "Doby")

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim One As String = Me.TextBox1.Text
    Dim Two As ListBox = Me.ListBox1
    ' Two.SelectedIndex = -1
    TextBox1.Text = "Doby"
    ListBox1.Items.Add(TextBox1.Text)
    If One.Length > 0 Then
        For Check As Integer = 0 To Two.Items.Count - 1
            If Two.Items(Check).ToString.Contains(One) Then
                ' Two.SelectedIndices.Add(Check)
            End If
        Next
    End If
End Sub
End Class

Forum Picture

对不起,我的英语太差了,谢谢你的帮助。

你的代码离正确还有点距离, 如果您查看下面的示例, 它写得有点啰嗦,但目的是帮助您了解正在发生的事情,然后您可以根据自己喜欢的编码风格缩短代码。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    '
    Dim TextItem As Web.UI.WebControls.ListItem

    '-> load the inital list items
    TextItem = New Web.UI.WebControls.ListItem("Gen-Adams", "Gen-Adams")
    ListBox1.Items.Add(TextItem)
    TextItem = New Web.UI.WebControls.ListItem("Michael-Dianna", "Michael-Dianna")
    ListBox1.Items.Add(TextItem)
    TextItem = New Web.UI.WebControls.ListItem("Gessy-Kristen", "Gessy-Kristen")
    ListBox1.Items.Add(TextItem)
    '-> Init textboxes
    TextBox1.Text = ""
    TextBox2.Text = "Doby"

End Sub


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    '
    Dim Counta As Integer
    Dim Matched As Boolean
    Dim TmpListItem As Web.UI.WebControls.ListItem
    '
    '-> validate "look for" text
    If Trim(TextBox1.Text) <> "" Then
        '-> validate "replace with" text
        If Trim(TextBox2.Text) <> "" Then
            '-> if listbox contains items
            If ListBox1.Items.Count > 0 Then
                Matched = False
                For Counta = 0 To ListBox1.Items.Count - 1
                    'ListBox1.SetSelected(Counta, True)
                    TmpListItem = ListBox1.Items(Counta)
                    '-> try to search for text
                    If InStr(TmpListItem.Text, TextBox1.Text) > 0 Then
                        '-> search text DOES exist in listitem text
                        TmpListItem.Text = Replace(TmpListItem.Text, TextBox1.Text, TextBox2.Text)
                        ListBox1.Items(Counta) = TmpListItem
                        ListBox1.Refresh()
                        Matched = True
                    Else
                        'word not found in this list-item
                    End If
                Next
                If Not Matched Then
                    '-> No matches were found - add search word to list
                    TmpListItem = New Web.UI.WebControls.ListItem(TextBox1.Text)
                    ListBox1.Items.Add(TmpListItem)
                End If
            Else
                'The listbox is empty - do something here
                TmpListItem = New Web.UI.WebControls.ListItem(TextBox1.Text)
                ListBox1.Items.Add(TmpListItem)
            End If
        Else
            MsgBox("No replacement word entered!")
        End If
    Else
        MsgBox("No search word entered!")
    End If
    '-> Clear search/replacement textboxes
    TextBox1.Text = ""
    TextBox2.Text = ""
    '
End Sub