Results 1 to 12 of 12

Thread: ListBox Search with TextBox

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2009
    Location
    vbforums
    Posts
    809

    ListBox Search with TextBox

    I have an web reference application which have combox added with server1 and server2 .. server2 is triggered in form load event..

    when while debugging server1 or server2 class is called and its items are invoked in listbox.. as I have shown in fig.

    I want to make Listbox search with TextBox. so that when user puts product number in textbox and hit enter only that product number should be searched .

    please help me


  2. #2
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: ListBox Search with TextBox

    basically it depends on how you are filling the listbox,
    just go through the code & note please
    (1) i have a listbox & a textbox control
    (2) declared a binding source + datatable
    (3) filled the data ( in your case data from server 1,2 etc..) in to a datatble
    (4) next i am filtering the listbox contents with textbox text

    fallow the code

    vb Code:
    1. Public Class Form1
    2.     Dim Bs As New BindingSource
    3.  
    4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.  
    6.         Dim Dt As New DataTable
    7.         Dt.Columns.Add("ProductID", Type.GetType("System.String"))
    8.         Dim Row As DataRow
    9.         Dim i As Integer
    10.  
    11.         With Dt
    12.  
    13.             For i = 0 To 10
    14.                 Row = .NewRow
    15.                 Row("ProductID") = "product " & i
    16.                 .Rows.Add(Row)
    17.             Next i
    18.  
    19.         End With
    20.  
    21.         Bs.DataSource = Dt
    22.         Me.ListBox1.DataSource = Bs
    23.         ListBox1.DisplayMember = "ProductID"
    24.  
    25.  
    26.     End Sub
    27.  
    28.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    29.  
    30.         Dim sEARCHsTRING As String = Me.TextBox1.Text
    31.  
    32.         With Me.Bs
    33.             .Filter = "ProductID Like  '" & sEARCHsTRING & "%'"
    34.         End With
    35.  
    36.     End Sub
    37. End Class
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2009
    Location
    vbforums
    Posts
    809

    Re: ListBox Search with TextBox

    thanks but I get 3 Errors

    Code:
    Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Dt As New DataTable
            Dt.Columns.Add("productid", Type.GetType("System.String"))
            Dim Row As DataRow
            Dim i As Integer
    
            With (Dt)
    
                For i = 0 To 10
    
                Next
                Row = .NewRow
                Row("productid") = "product " & i
                Rows.Add(Row)
                Next i
    
            End With
    
            Bs.DataSource = Dt
            Me.ListBox.DataSource = Bs
            ListBox.DisplayMember = "productid"
    
        End Sub
    Error 1. Name 'Rows' is not declared.
    Error 2. 'Next' must be preceded by a matching 'For'.

    Code:
     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
            Dim sEARCHsTRING As String = Me.TextBox1.Text
    
            With (Me.Bs)
                Filter = "productid Like  '" & sEARCHsTRING & "%'"
            End With
    End Sub
    Error 3. Overload resolution failed because no accessible 'Filter' accepts this number of arguments.

    please help

  4. #4
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: ListBox Search with TextBox

    read the contents of #2 carefully
    this what is there in my code
    vb Code:
    1. .Rows.Add(Row)
    this what in your code
    Rows.Add(Row)
    dot is missing

    next
    this is what your code
    For i = 0 To 10

    Next

    Row = .NewRow
    Row("productid") = "product " & i
    Rows.Add(Row)
    Next i
    see care fully in my code & yours , i have highlighted the mistakes
    i believe that you have copy'd & pasted but failed to read & check my code
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2009
    Location
    vbforums
    Posts
    809

    Re: ListBox Search with TextBox

    THANK you very much .. I m really sorry!

    but what about error. 3 .. about Filter Please Solve this also

  6. #6
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: ListBox Search with TextBox

    i would like to repeat to recheck #2 carefully
    this your code
    Code:
     With (Me.Bs)
                Filter = "productid Like  '" & sEARCHsTRING & "%'"
            End With
    this is mine
    Code:
     With (Me.Bs)
                .Filter = "productid Like  '" & sEARCHsTRING & "%'"
            End With
    code

    next importantly Google for what is overload resolution
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2009
    Location
    vbforums
    Posts
    809

    Re: ListBox Search with TextBox

    thanks .. but with no errors..

    not succeed when inserting product number in textbox .. after hitting enter no response.. .... listbox is as filled with the items ...

  8. #8
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: ListBox Search with TextBox

    presumably since your listbox contains some lenth of text also like product details...No[123456]
    you need to concatenate your text box text with this , ofcourse if you are entering only numbers.

    do some thing like
    vb Code:
    1. textbox1.text = "product details...No[" & textbox1.text & "]"
    and then try
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  9. #9
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: ListBox Search with TextBox

    This is the same code posted by make_me_rain in post#2. I have made only 2 changes. Create a new project, add a ListBox and a TextBox into the form. Then copy-paste this code and test it. Type a product number (say "10") in the TextBox and see how it works.
    Code:
    Public Class Form1
        Dim Bs As New BindingSource
    
    
        Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    
            Dim sEARCHsTRING As String = Me.TextBox1.Text
    
            With Me.Bs
                .Filter = "ProductID Like  '%" & sEARCHsTRING & "%'"
            End With
    
        End Sub
    
        Private Sub Form1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Dt As New DataTable
            Dt.Columns.Add("ProductID", Type.GetType("System.String"))
            Dim Row As DataRow
            Dim i As Integer
    
            With Dt
    
                For i = 0 To 10
                    Row = .NewRow
                    Row("ProductID") = "Product Details ...No..[" & i & "]"
                    .Rows.Add(Row)
                Next i
    
            End With
    
            Bs.DataSource = Dt
            Me.ListBox1.DataSource = Bs
            ListBox1.DisplayMember = "ProductID"
        End Sub
    End Class


    Edit:

    Same code. But little bit of rearranging, renaming and commenting:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Dim bs As New BindingSource
    4.  
    5.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    6.  
    7.         Dim strSearch As String = Me.TextBox1.Text
    8.  
    9.         bs.Filter = "ProductID Like '%" & strSearch & "%'"
    10.  
    11.     End Sub
    12.  
    13.     Private Sub Form1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    14.  
    15.         Dim dt As New DataTable
    16.         Dim row As DataRow
    17.         Dim i As Integer
    18.  
    19.         dt.Columns.Add("ProductID", Type.GetType("System.String"))
    20.  
    21.         '~~~ Fill up some sample data
    22.         For i = 0 To 10
    23.             row = dt.NewRow  '~~~ create a new data row
    24.             row("ProductID") = "Product Details ...No..[" & i & "]" '~~~ insert value to this row
    25.  
    26.             dt.Rows.Add(row) '~~~ add this row to our DataTable
    27.         Next i
    28.  
    29.         '~~~ bind it
    30.         bs.DataSource = dt
    31.         Me.ListBox1.DataSource = bs
    32.         ListBox1.DisplayMember = "ProductID"
    33.  
    34.     End Sub
    35. End Class
    Last edited by akhileshbc; Dec 30th, 2011 at 01:05 AM. Reason: added rearranged code

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2009
    Location
    vbforums
    Posts
    809

    Re: ListBox Search with TextBox

    how do I use vbcode tag to post my codes

  11. #11
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: ListBox Search with TextBox

    just type [[HIGHLIGHT]] your complete code goes in between these two highlight & /highlight tags tags[[//HIGHLIGHT]]
    Last edited by make me rain; Dec 30th, 2011 at 09:18 AM.
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  12. #12
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: ListBox Search with TextBox

    Quote Originally Posted by make me rain View Post
    just type [[HIGHLIGHT]] your complete code goes in between these two highlight & /highlight tags tags[[//HIGHLIGHT]]
    Or, click on the VBCode icon(), when you post it. And an inputbox will popout. Enter "vb.net". This will create the [HIGHLIGHT] tags for you. If you have the code already pasted in the editor and it is selected, then by doing the above will enclose that selection in [HIGHLIGHT] tags.

    Eg:
    Code:
    [HIGHLIGHT="vb.net"]
    
    '~~~ vb.net code here
    MessageBox.Show("Hi janu...")
    
    [/HIGHLIGHT]

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width