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

时间:2018-07-27 12:16:21

标签: .net vb.net visual-studio-2012 visual-studio-2015

如果我能阻止添加特定的重复单词,我会更受伤, 我不是在谈论物品

我说要防止添加特定词,如果该特定词已存在于列表框中,那么,如果创建了该特定词,就不会再次添加它,但同时可以将其更改为“ Doby”的另一个值

例如:

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

因此,我在文本框中输入了“ Dianna”,如果它在列表框中找到了,那么它将阻止在列表框中添加重复的“ Dianna”名称,但同时会更改,即只有从列表框中创建的“ Dianna”这个词才能变为“多比”

有可能吗,我已经创建了代码,检查它是否已经建立,但是有问题 (我无法阻止将特定的重复单词添加到同一ListBox中,并且无法将特定的单词“ 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

对不起,我的英语不好,谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您的代码与正确无误, 如果您看下面的示例, 它写得有些冗长,但目的是试图帮助您了解正在发生的事情,然后可以根据自己喜欢的编码样式来缩短代码。

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