Fire ItemCheck event from GUI only

Posted on 2005-03-25
Medium Priority
Last Modified: 2010-04-23
Hello All,

I have a Sub that looks like this:

Private Sub CheckedListBoxAdd_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBoxAdd.ItemCheck
        Dim i As Integer
        If e.CurrentValue = CheckState.Indeterminate Then

            Dim oRecord As myCLBData = CType(CheckedListBoxAdd.Items.Item(e.Index), myCLBData)

            If CheckedListBox1.FindString(oRecord.Name.ToString) <> -1 Then
                e.NewValue = CheckState.Indeterminate


                e.NewValue = CheckState.Unchecked

            End If

        End If

    End Sub

This sub is used to check for the presence of a value in CLB-A when an item is checked in CLB-B. If found in CLB-A, it will not let you change the checkstate of CLB-B.

The problem I'm having is this event is firing if I check an item in CLB-A using the application, as well as when the state of a CLB item is changed programmically.

I want the event only to fire when using the GUI.

Can someone help with this?


Question by:mooka
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 96

Expert Comment

by:Bob Learned
ID: 13631717
Sample code:

  Dim listBoxValidCheck As Boolean

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Me.CheckedListBox1.Items.Add("test 1")
    Me.CheckedListBox1.Items.Add("test 2")
    Me.CheckedListBox1.Items.Add("test 3")
    Me.CheckedListBox1.Items.Add("test 4")

    Me.CheckedListBox1.CheckOnClick = True

  End Sub

  Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck

    If listBoxValidCheck Then
    End If

  End Sub

  Private Sub CheckedListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles CheckedListBox1.MouseDown

    listBoxValidCheck = True

  End Sub

  Private Sub CheckedListBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles CheckedListBox1.MouseUp

    listBoxValidCheck = False

  End Sub

  Private Sub CheckedListBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles CheckedListBox1.KeyDown

    listBoxValidCheck = True

  End Sub

  Private Sub CheckedListBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles CheckedListBox1.KeyUp

    listBoxValidCheck = False

  End Sub

Author Comment

ID: 13632068
Thanks for this,

Just so I'm clear, I should use the mouse events to handle changes to the GUI and use ItemCheck events for the code?

If so, how can I tell which item in the CLB the user selected from the mouse event?


LVL 96

Accepted Solution

Bob Learned earned 1000 total points
ID: 13643588
The listBoxValidCheck flag tells the ItemCheck event that it can process whatever you need it to.  The only valid ways are if a key is pressed, or a mouse is pressed.  Once the key up event or mouse up event fires, it is no longer valid for the ItemCheck event to handle anything.  When your code causes the ItemCheck to fire, listBoxValidCheck should be false.

So, in other words, You can still process all of it from ItemCheck, but only if the right conditions are met.


Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

777 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