Results 1 to 11 of 11

Thread: Acquiring the index number in a listbox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    67

    Acquiring the index number in a listbox

    Hi. I have a listbox (LBProd) that is populated at form load from a text file using:

    Dim Prods() As String = IO.File.ReadAllLines("E:\Rajja\TxtFiles\Product1.txt")
    LBProd.Items.AddRange(Prods)

    It's an extensive list! There is a Textbox into which the user can start to type the name of a product and it will highlight the product in the listbox.

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles tb1.TextChanged

    Dim i As Integer = LBProd.FindString(tb1.Text)
    LBProd.SelectedIndex = i
    If tb1.Text = "" Then
    LBProd.SelectedIndex = -1
    End If

    End Sub

    The user can then select the product in the listbox from the list of matches; but, having selected the product I need to retrieve the index of that selection in the list to use in the next stage of the program, but I'm struggling to get the index when the user clicks the selection in the listbox.

    Would be grateful for any suggestions and thanks in advance.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Acquiring the index number in a listbox

    Quote Originally Posted by Pianoman23 View Post
    The user can then select the product in the listbox from the list of matches;
    Um, what list of matches? The code you posted doesn't create a list of matches. It selects the first match in the ListBox. You already have the index of that item because you're setting it yourself. I'm afraid that what you say you're doing and what you say you want don't actually make sense together.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Acquiring the index number in a listbox

    Also, wouldn't it make more sense to check whether the TextBox is empty first and then only call FindString if it's not?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    67

    Re: Acquiring the index number in a listbox

    Quote Originally Posted by jmcilhinney View Post
    Also, wouldn't it make more sense to check whether the TextBox is empty first and then only call FindString if it's not?
    Apologies but I'm very new to this. Everything I've done so far has been through snippets found on the net and obviously I've started wrongly. Sorry to have taken up your time.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    67

    Re: Acquiring the index number in a listbox

    Ok - apologies for stupidity out of the way - where do I go from Here?

    I have a populated listbox of 30,000 items. A search textbox into which the user can start to type a product name and 'close' matches are highlighted in the listbox. They need to be able to select the product in the listbox and that selection needs to be able to generate the index which can be used as a variable to 'link' the product to other lists created in the same order.
    For each product name shown in the listbox there are numerous codes and tariffs all contained in other lists, but all of the lists are in the same order, so the same index should select the correct details - or am I missing something?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Acquiring the index number in a listbox

    You don't have to apologise. We are here to help after all. What you do need to do is provide a FULL and CLEAR explanation of what you're trying to do. We can't really help otherwise, or else we'll waste our time and yours making false starts.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    67

    Re: Acquiring the index number in a listbox

    Quote Originally Posted by jmcilhinney View Post
    You don't have to apologise. We are here to help after all. What you do need to do is provide a FULL and CLEAR explanation of what you're trying to do. We can't really help otherwise, or else we'll waste our time and yours making false starts.
    Thanks for that so let's start afresh.

    I work within the pharmacy world and my expertise is with Excel. In Excel I created a buying model, but, by the very nature of the beast, the spreadsheets are becoming unwieldy. I deal with over 90,000 products all of which have APPID codes, VPPID codes, Pipcodes, pack sizes, unit of measurement, tariffs, etc, etc. So I've set about developing a stand-alone program that can cope with all of the numerous calculations, etc in a more efficient way.

    Initially I created a series of .txt files from the original Excel sheet and made a start in VB creating a basic way to display details of a product (codes, etc) when it was selected from a list. I managed to work out how to populate the lists from the .txt files using:
    Dim Prods() As String = IO.File.ReadAllLines("E:\Rajja\TxtFiles\Product1.txt") - this populates all of the product names.
    I used this to populate a ListBox and included a 'search' textbox on the form for the user to type the name of a product. As they type, the products are highlighted in the listbox.

    Obviously there are a lot of products that start with the same characters, so the more they type, the more exact is the match; but the need to be able to select the exact match from the list which will open a form displaying the full details of that product.

    As all of the txt files involved are in the same sorted order, I thought that if I got the index of the selected product and stored that in a variable (ind) then it would be straightforward to find corresponding information in the other files by using, e.g. VPPID(ind); APPID(ind) etc., hence the need to get hold of the index and place it in a variable.

    Not sure if all of that makes sense - it does in my head but, as I said, I'm teaching myself VB and at the age of 72 it's not an easy task; but it certainly keeps the brain active!!!

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Acquiring the index number in a listbox

    I would suggest that you populate a DataTable with your data, bind that to a BindingSource and then bind that to your ListBox. As the user types, you can set the Filter property of the BindingSource and that will filter the data displayed in the ListBox. The SelectedItem of the ListBox wil then be a DataRowView and it will have a Row property that returns a DataRow. You can call IndexOf on the Rows collection of the DataTable to get the index you want.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2013
    Posts
    67

    Re: Acquiring the index number in a listbox

    Thanks for the suggestion. So it's back to the textbooks for info on DataTables and BindingSources. Hey Ho. I should have stuck with Excel!!
    Have a good day.

  10. #10
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,190

    Re: Acquiring the index number in a listbox

    Quote Originally Posted by Pianoman23 View Post
    Thanks for the suggestion. So it's back to the textbooks for info on DataTables and BindingSources. Hey Ho. I should have stuck with Excel!
    Not really. You should put your data into a database ideally. I think having to deal with recreating the text files for any product changes will become a major thorn in your side.

    There are plenty of examples here with using DataTables and BindingSources. I'll throw in a quick example (won't say it's the best but it works fine for me for 6000 records). Just assume Proj[ect]ID is ProductID for you.
    VB.Net Code:
    1. 'at the top of the form/module
    2.     Public bsProjects As New BindingSource
    3.     Public dtProjects As New DataTable
    4.  
    5.     'called before you use dtProjects
    6.     Private Sub LoadProjects()
    7.  
    8.         Dim Sql As String = "SELECT whatever FROM wherever WHERE certain criteria are met ORDER BY crProjID DESC"
    9.         Try
    10.             Using cmd As New OleDbCommand(Sql, gCnnProject) 'gCnnProject is the database connection to the database
    11.                 Using dr As OleDbDataReader = cmd.ExecuteReader()
    12.                     dtProjects.Clear()
    13.                     dtProjects.Load(dr)
    14.                 End Using
    15.             End Using
    16.         Catch ex As Exception
    17.             MessageBox.Show(ex.ToString)
    18.         End Try
    19.     End Sub
    20.  
    21.     'once dtProjects is loaded
    22.     bsProjects.DataSource = dtProjects
    23.  
    24.     'once bsProjects.DataSource has been set, you can bind it to your control
    25.     dgvProjects.DataSource = bsProjects 'I am using a DataGridView rather than ListView
    26.  
    27.     'after DGV has been bound, I can filter the DataTable to show any matches for whatever the person has typed in a textbox.
    28.     'I assume it would work the same if I filtered the BindingSource instead
    29.     'My full filter string is pretty complex so I just gave a small example here which will match any project ID that contains 20725 in it
    30.     dtProjects.DefaultView.RowFilter = "(crProjID LIKE '%20725%')"
    31.  
    32.     'for when a user double-clicks any cell in the DGV (I have SelectionMode property set to FullRowSelect)
    33.     Private Sub dgvProjects_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvProjects.CellDoubleClick
    34.  
    35.         If e.RowIndex >= 0 Then
    36.  
    37.             gProjId = CInt(dgvProjects.Rows(e.RowIndex).Cells("crProjID").Value)
    38.             'do whatever you need with the ProjectID
    39.         End If
    40.  
    41.     End Sub

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Acquiring the index number in a listbox

    Quote Originally Posted by topshot View Post
    Not really. You should put your data into a database ideally. I think having to deal with recreating the text files for any product changes will become a major thorn in your side.
    Quite so. What you should have done is moved to Access, or some other database.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

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