开发者

Comparing text box text with listbox items text in vb.NET

how to Compare text box text with listbo开发者_开发百科x items text in vb.NET....Please HELP


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

If you use custom objects as Datasource of your Listbox, override ToString in the Class to compare them with your Textbox' Text. ListBox.ObjectCollection Class

For example:

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


i tried the following in VB.net it worked fine

the aspx page

<%@ 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>

the code behind

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

End Class


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜