|
-
Nov 10th, 2016, 02:12 PM
#1
Thread Starter
Lively Member
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.
-
Nov 10th, 2016, 07:40 PM
#2
Re: Acquiring the index number in a listbox
 Originally Posted by Pianoman23
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.
-
Nov 10th, 2016, 07:40 PM
#3
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?
-
Nov 11th, 2016, 02:52 AM
#4
Thread Starter
Lively Member
Re: Acquiring the index number in a listbox
 Originally Posted by jmcilhinney
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.
-
Nov 11th, 2016, 03:27 AM
#5
Thread Starter
Lively Member
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?
-
Nov 11th, 2016, 03:30 AM
#6
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.
-
Nov 11th, 2016, 04:10 AM
#7
Thread Starter
Lively Member
Re: Acquiring the index number in a listbox
 Originally Posted by jmcilhinney
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!!!
-
Nov 11th, 2016, 04:58 AM
#8
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.
-
Nov 11th, 2016, 05:21 AM
#9
Thread Starter
Lively Member
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.
-
Nov 11th, 2016, 08:50 AM
#10
Re: Acquiring the index number in a listbox
 Originally Posted by Pianoman23
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:
'at the top of the form/module Public bsProjects As New BindingSource Public dtProjects As New DataTable 'called before you use dtProjects Private Sub LoadProjects() Dim Sql As String = "SELECT whatever FROM wherever WHERE certain criteria are met ORDER BY crProjID DESC" Try Using cmd As New OleDbCommand(Sql, gCnnProject) 'gCnnProject is the database connection to the database Using dr As OleDbDataReader = cmd.ExecuteReader() dtProjects.Clear() dtProjects.Load(dr) End Using End Using Catch ex As Exception MessageBox.Show(ex.ToString) End Try End Sub 'once dtProjects is loaded bsProjects.DataSource = dtProjects 'once bsProjects.DataSource has been set, you can bind it to your control dgvProjects.DataSource = bsProjects 'I am using a DataGridView rather than ListView 'after DGV has been bound, I can filter the DataTable to show any matches for whatever the person has typed in a textbox. 'I assume it would work the same if I filtered the BindingSource instead '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 dtProjects.DefaultView.RowFilter = "(crProjID LIKE '%20725%')" 'for when a user double-clicks any cell in the DGV (I have SelectionMode property set to FullRowSelect) Private Sub dgvProjects_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvProjects.CellDoubleClick If e.RowIndex >= 0 Then gProjId = CInt(dgvProjects.Rows(e.RowIndex).Cells("crProjID").Value) 'do whatever you need with the ProjectID End If End Sub
-
Nov 11th, 2016, 06:48 PM
#11
Re: Acquiring the index number in a listbox
 Originally Posted by topshot
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|