Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

Fire ItemCheck event from GUI only

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

            Else

                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?

Thanks,

john
0
mooka
Asked:
mooka
  • 2
1 Solution
 
Bob LearnedCommented:
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
      Console.WriteLine("Test")
    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
0
 
mookaAuthor Commented:
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?

Thanks,

John
0
 
Bob LearnedCommented:
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.

Bob
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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