ListBox selection

Posted on 2006-05-19
Last Modified: 2010-04-23
The code below checks if an item in the list box is checked. If it checked, i add a rate to the current rate. If it is not checked, the rate reamains the same. it works perfectly with one item in the listbox. If i have two items i must select both items or all of the item in order to update the "rate".

Based on that description, the problem is related to the if statement up at the top of the code that analyzes if an item is checked or not.

Summaryzing: I need some help understanding why the code work properly for one item and it does not work properly for more than one item. Again when two items are displayed in the box, i must select both of them or the code thinks that it has either one has been checked.



For i = 0 To (chkEndorsement.Items.Count - 1)
            If chkEndorsement.GetItemCheckState(i) = CheckState.Checked Then
                With rs
                    If Not .BOF Then
                        Do While Not .EOF
                            endorsement = rs.Fields.Item("description").Value.ToString
                            endvalue = rs.Fields.Item("id").Value.ToString
                            Grid.Columns("Rate").Value = Val(endvalue) + Grid.Columns("Rate").CellValue(Grid.Row).ToString
                    End If
                End With
                LoadRate(formcodeval, township, range, cropclass, cropname, stateid, countyid)
            End If

Question by:virgilar
    LVL 35

    Expert Comment

    first change from ADO to ADO.NET. Since you are using .NET, it makes sense to start using ADO.NET:

    change recordset to DataReader and use datareader in your code:

    Dim reader as OleDbDataReader
    Dim conn as OleDbConnection
    conn = New OleDbConnection("your connection string here")
    DIm query as String="SELECT * FROM Table"
    Dim cmd As OleDbCommand = New OleDbCommand(query, conn)
    reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    For i = 0 To (chkEndorsement.Items.Count - 1)
                If chkEndorsement.GetItemCheckState(i) = CheckState.Checked Then
                   While reader.Read
                                endorsement = reader("description").Value.ToString
                                endvalue = reader("id").Value.ToString
                                Grid.Columns("Rate").Value = Val(endvalue) + Grid.Columns("Rate").CellValue(Grid.Row).ToString
                    End While
                    LoadRate(formcodeval, township, range, cropclass, cropname, stateid, countyid)
                End If


    Author Comment

    Thanks, but still does not change the fact that the code only works when all options have been selected in the list box. When there is only one option, it all works fine of course but there are more than one option, i have to select all of them for it to work.

    Author Comment

    I have come up with the answer:

     For Each x In Me.chkEndorsement.CheckedItems
                    checked = True
                    endvalue = Val(endvalue) + Me.chkEndorsement.ValueMember
                    Me.Grid.Columns("Rate").Value = Val(rate) + Val(endvalue)

    That code above will do what i was looking for.


    This question can be closed please.

    Accepted Solution

    PAQed with points refunded (500)

    Community Support Moderator

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now