避免TextBox中的现有项目

时间:2017-09-15 08:41:20

标签: vb.net winforms

  Private Sub BtnMoveItemstoTemplate_Click(sender As Object, e As EventArgs) Handles BtnMoveItemstoTemplate.Click
    Try
        If LBMessageContents.SelectedItem Is Nothing Then
            ShowMessage("Select Some Details to Send a Message")
        Else
           RTMsgContent.Text = RTMsgContent.Text & LBMessageContents.SelectedItem
        End If

    Catch ex As Exception
        HandleClassException(ex)
    End Try
End Sub

我编写了一个代码,用于将项目从ListBox移动到RichTextBox。

如何验证列表项是否已存在。

2 个答案:

答案 0 :(得分:2)

我得到了我的问题的解决方案。

我使用[.contains()]函数来验证文本是否已存在。

   Private Sub BtnMoveItemstoTemplate_Click(sender As Object, e As EventArgs) Handles BtnMoveItemstoTemplate.Click
    Try
    If LBMessageContents.SelectedItem Is Nothing Then
        ShowMessage("Select Some Details to Send a Message")
    Else

   **If RtMsgContent.Text.Contains(LBMessageContents.SelectedItem) then Exit Sub**

       RTMsgContent.Text = RTMsgContent.Text & LBMessageContents.SelectedItem
    End If

  Catch ex As Exception
    HandleClassException(ex)
  End Try
  End Sub

答案 1 :(得分:2)

正如vinayak所说,您可以使用.contains()来了解文本框是否包含字符串

Private Sub BtnMoveItemstoTemplate_Click(sender As Object, e As EventArgs) Handles BtnMoveItemstoTemplate.Click
  Try
    If LBMessageContents.SelectedItem Is Nothing Then
        ShowMessage("Select Some Details to Send a Message")
    Else
        'check if the item already exists
        If RtMsgContent.Text.Contains(LBMessageContents.SelectedItem) then
            'do what you want to do if the item already exists. like showing message
            MsgBox("Error! This item already exists")
        Else
            'doesn't already exists, add to the textbox
            RTMsgContent.Text = RTMsgContent.Text & LBMessageContents.SelectedItem
        End If
    End If
Catch ex As Exception
    HandleClassException(ex)
  End Try
End Sub