vb.net - 避免文本框中存在现有项目

标签 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。

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

最佳答案

正如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

关于vb.net - 避免文本框中存在现有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46235300/

相关文章:

c# - 在 Winforms 和 .net 中难以在设计时保留引用内部属性的集合

.net - Threading.Timer与Forms.Timer

C# WinForms 禁用 DPI 缩放

vb.net - 为什么使用公共(public)属性而不是函数来公开自定义集合<T>

mysql - VB.NET:使用变量生成 MySQL 查询

vb.net - 从父控件/窗体中完全删除控件的正确方法是什么?

c# - 强制绑定(bind)到数据源的复选框在尚未查看时更新

vb.net - Option Strict On,设置未知对象类型的焦点

c# - WinForms - 有什么方法可以将 Visual C++ 项目的 UI 转换为 Visual C# 吗?

c# - 存储过程返回varchar