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

那年仲夏 提交于 2019-12-11 07:43:40

问题


how to Compare text box text with listbox items text in vb.NET....Please HELP


回答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

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



回答2:


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




回答3:


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


来源:https://stackoverflow.com/questions/4375148/comparing-text-box-text-with-listbox-items-text-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!