Question Checkedlistbox Problems

paulthepaddy

Well-known member
Joined
Apr 9, 2011
Messages
222
Location
UK
Programming Experience
Beginner
hey guys/girls just wondering can any1 explain why this is happening and how to solve it.

i have a check list box and the count is incorrect, made a very quick video just to show whats happening, any help is much appreaicated

YouTube - Checkedlistbox problem VB.net
 
Could you provide us with the code you are using to get that value?

-Josh
 
their is no code, most of the code is doing the calculations for the sub and total value, the only code related to the label is

VB.NET:
Public Sub SSR_Car1_Total()
        Dim Damage_Count As Integer = CheckedListBox1.CheckedItems.Count
        If Damagecount = 0 Then
            SUBTOTAL = 0
            SUBTOTAL = 25
        End If
        If Damage_Count = 1 Then
            SUBTOTAL = 0
            SUBTOTAL = 40
        End If
        If Damagecount >= 2 Then
            SUBTOTAL = 0
            SUBTOTAL = (((Damagecount + 1) * 15) + 10)
        End If
        TXT_Subtotal.Value = SUBTOTAL
    End Sub

VB.NET:
 Private Sub CheckedListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckedListBox1.Click
        Call SSR_Car1_Total()
        Label7.Text = Damage_Count


me.these_are_invalied_Checkboxs_Not_the_CheckListbox
        If Me.CheckBox1.Checked = True Then
            TOTAL = SUBTOTAL - txt_add_Minus.Value
            TXT_Total.Value = TOTAL
        End If
        If Me.CheckBox1.Checked = False Then
            TOTAL = SUBTOTAL + txt_add_Minus.Value
            TXT_Total.Value = TOTAL
        End If
    End Sub

and jmcilhinney, for a start i made the video last night, so im not using someone elses examples,

how can i explain that visual studio is messing up a checklistbox.selecteditems.count already with a video ppl are asking code?simple code the only code above that matters is
Dim Damage_Count As Integer = CheckedListBox1.CheckedItems.Count
this was only put in because it was messing p the main calculations.

and i dont see why you wouldn't watch a video first of all you can right click a link and copy link location then paste it into the address bar... that way u know exactly where it is going to, u can hover your mouse above the link and normally a box on the bottom left hand side will appear showing the location.... their wqe go we now know that the link it going to youtube noware else. B) is a video not much easier to see whats happening, if my english is bad and trust me it really is, then me trying to explain something if i couldn't explian it good a video WOUOLD SHOW YOU EXACTLY WHATS GOING ON :D same as the teamview software :D
 
I created a test project and can see your issue, I believe that it is a timing issue. If you check or uncheck an item, it doesn't update the count until after the click event is fired. So the reason it was adding to the count when you unchecked the item is because it is always displaying the 1 value behind what the real count is. Here's what I came up with:

VB.NET:
'Put this code in the checkedlistbox's "ItemCheck" event.

Dim chkCount As Integer = CheckedListBox1.CheckedItems.Count
If e.CurrentValue = CheckState.Unchecked Then
    chkCount += 1
Else
    chkCount -= 1
End If
Label7.Text = chkCount
Damage_Count = chkCount
SSR_Car1_Total()

This eliminates the Click event all together. You will also want to remove the line in the SSR_Car1_Total that sets the count. I'm not sure if this is the best way to go about this or not, but it works. :)

-Josh
 
The only relevant event that is raised when items are checked is CheckedListBox.ItemCheck Event (System.Windows.Forms)
help said:
The check state is not updated until after the ItemCheck event occurs.
So using this event you add/subtract 1 to Count based on e.NewValue, like previous poster, or like this:
VB.NET:
Dim newCount = Me.CheckedListBox1.CheckedItems.Count
newCount = If(e.NewValue = CheckState.Checked, newCount + 1, newCount - 1)
 
JohnH is right, you can replace this line:

VB.NET:
If e.CurrentValue = CheckState.Unchecked Then

with this line:

VB.NET:
If e.NewValue = CheckState.Checked Then
 
thank you, the both of you. i have been battling this one alone for a few days, TBH this has what has been whats holding back the update lol. just as well this is for family otherwise i would have ben fired by now LMFAO.

another question while i have got both of your attention, do any of you know anything about microsoft.office.interop.word
 
I have done a little with Microsoft.Office.Interop.Word, what would you like to know?

-Josh
 
another question while i have got both of your attention, do any of you know anything about microsoft.office.interop.word
Start a new thread if you have new topic inquiries.
 
Back
Top