get listbox2 value

didgydont

Active member
Joined
Mar 2, 2009
Messages
37
Programming Experience
Beginner
hi all
i am trying to add the value of listbox2 as price but it adds -1 every thing else is okay
any ideas ?
thank you for your time
VB.NET:
Rec.Open("SELECT * FROM Records", Conn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
        Dim ki As Integer = 0
        For Each item As String In ListBox1.Items
            Rec.AddNew()
            Rec.Fields("TransID").Value = My.Settings.TransID
            Rec.Fields("ItemID").Value = My.Settings.ItemID
            Rec.Fields("Item").Value = item
            Rec.Fields("Price").Value = ListBox2.Items.IndexOf(ki)
            Rec.Fields("Sold").Value = Date.Today
            Rec.Update()
            My.Settings.ItemID = My.Settings.ItemID + 1
            ki = ki + 1
        Next
        Rec.Close()
 
This:
VB.NET:
ListBox2.Items.IndexOf(ki)
is going to return the index in ListBox2 of the value you specify, or -1 if the item doesn't exist in the ListBox. The values you're specifying are 0, 1, 2, etc. Does ListBox2 contain those values? If not then that would explain whay you're getting -1 all the time.

Do you really want to get the index of those values, or is it actually the values at those indexes that you want?
 
this is it
VB.NET:
Rec.Open("SELECT * FROM Records", Conn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
        Dim ki As Integer = 0
        For Each item As String In ListBox1.Items
            Rec.AddNew()
            Rec.Fields("TransID").Value = My.Settings.TransID
            Rec.Fields("ItemID").Value = My.Settings.ItemID
            Rec.Fields("Item").Value = item
            Rec.Fields("Price").Value = ListBox2.Items(ki)
            Rec.Fields("Sold").Value = Date.Today
            Rec.Update()
            My.Settings.ItemID = My.Settings.ItemID + 1
            ki = ki + 1
        Next
        Rec.Close()
 
I'm just not sure why you're using a For Each loop and a separate counter variable when you could simply use a For loop and the counter would be built in. The right tool for the job.
 
what is it you recomend i do to improve this code ?
As I said in my previous post, you should be using a For loop. A For loop is controlled by a numeric counter. A numeroc counter is what you're using to access the second ListBox. You should be just using the same numeric counter to access both ListBoxes, so you always get corresponding items.
 
i tried this but it has error
VB.NET:
 Rec.Open("SELECT * FROM Records", Conn, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
        'Dim ki As Integer = 0
        For item As String In ListBox1.Items
            Rec.AddNew()
            Rec.Fields("TransID").Value = My.Settings.TransID
            Rec.Fields("ItemID").Value = item
            Rec.Fields("Item").Value = item
            Rec.Fields("Price").Value = ListBox2.Items
            Rec.Fields("Sold").Value = Date.Today
            Rec.Update()
            'ki = ki + 1
        Next
        Rec.Close()
 
That's not how you construct a For loop. That's just a For each loop with the Each missing. I suggest that you read the documentation for the For statement if you don;t know how a For loop works, but here's an example:
VB.NET:
For index As Integer = startIndex To endIndex

Next
I'd also suggest that you stop using ADO and switch to ADO.NET.
 
thank you i finaly found vb documentation this my test works a treat.
is this what you recommend ?
VB.NET:
Dim startIndex As Integer = 0
        Dim endIndex As Integer = ListBox1.Items.Count - 1

        For index As Integer = startIndex To endIndex
            MessageBox.Show(ListBox1.Items(index) & ListBox2.Items(index))


        Next
 
Looks good, except I probably wouldn't bother declaring startIndex and endIndex. for that simple case I'd probably just put the values right into the loop statement.
 
is this what you mean ?
if so thank you for your input it has reduced the amount of code needed quite a bit and made it much easier to read.
VB.NET:
For index As Integer = 0 To ListBox1.Items.Count - 1
 
is this what you mean ?
if so thank you for your input it has reduced the amount of code needed quite a bit and made it much easier to read.
VB.NET:
For index As Integer = 0 To ListBox1.Items.Count - 1
Eggsackly. ;)
 
Back
Top