将文本框文本与vb.NET中的列表框项文本进行比较

时间:2010-12-07 09:32:00

标签: .net vb.net text textbox listbox

如何将文本框文本与vb.NET中的列表框项目文本进行比较....请帮助

3 个答案:

答案 0 :(得分:1)

Dim text As String = Me.TxtName.Text
For Each item As Object In Me.ListBox1.Items
    If item.ToString = text Then
        'Do something'
    Else
        'Do something else'
    End If
Next

如果您使用自定义对象作为列表框的数据源,请覆盖类中的ToString以将其与文本框“文本”进行比较。 ListBox.ObjectCollection Class

例如:

Class FooClass
    Private _name As String

    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property
    Public Overrides Function ToString() As String
        Return Me.Name
    End Function
End Class

答案 1 :(得分:1)

我在VB.net中尝试了以下内容 它运作良好

aspx页面

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

              

</div>
<asp:ListBox ID="ListBox1" runat="server">
    <asp:ListItem>zero</asp:ListItem>
    <asp:ListItem>first</asp:ListItem>
    <asp:ListItem>second</asp:ListItem>
</asp:ListBox>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>

背后的代码

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    For Each item In ListBox1.Items
        If item.ToString = TextBox1.Text Then
            Response.Write("matching " + item.ToString)
        End If
    Next
End Sub

结束班

答案 2 :(得分:0)

Dim tempInt = lbTeams.Items.Count - 1
While (tempInt > -1)
    If (lbTeams.GetItemText(lbTeams.Items.Item(tempInt)).ToString().Equals(txtTeamName.Text) = True) Then
       MsgBox("Team Already Exist")
       Exit Sub
   End If
   tempInt -= 1
End While