[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

CheckedListBox .net Read Values

Hello all,

Here is a brief description of what i am trying to accomplish:

I have populated a checkedlistbox with more than one options sometimes (it depends on the user's selection). I want to check one, two or all options. I have a variable in my program called "Rate". Once the user selects any option in the checkedlistbox, i want to add the value for that specific selection to the "Rate" variable.

Ex: My rate variable carries a value of 0.50. I make a selection in my checkedlistbox that has a rate of 0.25. Now i want my rate variable to become 0.75.

I have a code below that works only when i display one option in the checkedListBox. Everytime i have two or more options, it ingores the whole procedure if i only select one option and leave the other ones unchecked.

Thanks for any inputs

'---loops through all items in the checkedlistbox
For i = 0 To (chkEndorsement.Items.Count - 1)
          '----checks for the checked options
            If chkEndorsement.GetItemCheckState(i) = CheckState.Checked Then
                With rs
                    If Not .BOF Then
                        .MoveFirst()
                        Do While Not .EOF
                            endorsement = rs.Fields.Item("description").Value.ToString
                           '---Stores the rate for the selected item
                            endvalue = rs.Fields.Item("id").Value.ToString
                            '---add the the endvalue to the current rate
                            Grid.Columns("Rate").Value = Val(endvalue) + rate
                            .MoveNext()
                        Loop
                    End If
                End With
            Else
                LoadRate(formcodeval, township, range, cropclass, cropname, stateid, countyid)
            End If

        Next
0
virgilar
Asked:
virgilar
1 Solution
 
ElrondCTCommented:
Your code setting endorsement and endvalue is not making any reference to the values in chkEndorsement; it's only referencing fields in the database. You need to look for the row for which the description is equal to chkTest.Items(i), or if each row has an entry in chkEndorsement, you can go through the rs dataset and each time you move a row, increment your counter through chkEndorsement, such as:

rs.MoveFirst()
i = 0
Do While Not rs.EOF
    If chkEndorsement.GetItemCheckState(i) = CheckState.Checked Then
        endorsement = rs.Fields.Item("description").Value.ToString
        '---Stores the rate for the selected item
        endvalue = rs.Fields.Item("id").Value.ToString
        '---add the the endvalue to the current rate
        Grid.Columns("Rate").Value = Val(endvalue) + rate
    End If
    .MoveNext()
    i += 1
Next

I'm not sure where the LoadRate call is supposed to go, but this should give you the idea.

Note that if you have more than one item checked, endorsement will only display the last description and your grid value will only reflect the final value from the dataset.
0
 
virgilarAuthor Commented:
Here is the answer, I replaced all that code with the lines below and it is working.
Elrondct, since your idea got me thinking about recoding my routine, i will award you the points.

Thanks
Andre

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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now