Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Event "ListView.ItemCheck" fires too early 1

Status
Not open for further replies.

DotNetter

Programmer
May 19, 2005
194
US
I have the following event handler:
Code:
	Private Sub lvwRequests_auth_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles lvwRequests_auth.ItemCheck
		If lvwRequests_auth.CheckedItems.Count = 0 Then
			btnApprove_auth.Enabled = False
			btnView_auth.Enabled = False
			btnDeny_auth.Enabled = False
		Else
			btnApprove_auth.Enabled = True
			btnView_auth.Enabled = True
			btnDeny_auth.Enabled = True
		End If
	End Sub
I want this to enable buttons only when there's at least one ListViewItem that's checked.

What actually happens, is if none are checked, then I check one, the event fires and it says that [tt]lvwRequests_auth.CheckedItems.Count = 0[/tt].

How can I get this to do what I want it to?

Thanks,
Dot
 
You can try this:

Private Sub ListView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp
Button1.Enabled = (ListView1.CheckedItems.Count <> 0)
End Sub


I would also prefare to enable or not the buttons like i show to you.

 
TipGiver, that's my preferred way of handling Booleans.

However when I used to teach programming I found that a number of students couldn't visualise what was happening and preferred the more descriptive approach as used by DotNetter.

By the way, how is your Assembler program coming along?



[vampire][bat]
 
It is ok now, I'll post the code: Dec to Binary. It works, although it could be done better with less lines of code.
 
It is ok now, I'll post the code: Dec to Binary. It works, although it could be done better with less lines of code.
TipGiver please do explain what you mean...

Thanks,
Dot
 
Just replied...

Did you find any solution to you problem? (listview)
 
Well, I don't really think MouseUp is the right event. If a user just un/checks a checkbox, nothing happens until they move the mouse. Isn't there a way to use ItemCheck (which would seem to be the right event) to get it to do what I'd like it to do?

Thanks,
Dot
 
... nothing happens until they move the mouse.

Wrong>
Mouse up means to let a button on the control. MouseUp + MouseDown = Click, if they are applied to the same control.

Anyway, you do not lose anything trying it... just some seconds.
 
TipGiver,

Of course I tried it before replying... :) But, as I said, if they check a ListViewItem, it actually doesn't dis/able the buttons until they move their mouse. This is a behavior that I've observed when trying your code. For some reason, however, it works just fine if they double-click a ListViewItem (somewhere else on the row) - then the checkbox gets checked and the buttons are instantly dis/abled...

My question is: isn't there some way to get the event to fire before they move, once they've clicked on the checkbox?

Thanks much,
Dot
 
There is no need to move the mouse. It takes less than a second, and the buttons are affected. Try to check a listviewitem and not move the mouse.

Maybe i did not understand something; there are other people to help too.
 
I've run accross this same issue, what happens is the item checked values is not updated until the sub has ended. If you move it to the MouseUp handler, you would also need to put it the KeyUp handler (some people still use the keyboard). You can use the e.NewValue in the .ItemCheck handler.
Code:
Private Sub lvwRequests_auth_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles lvwRequests_auth.ItemCheck

Dim i As Int16 = IIf(e.NewValue = CheckState.Checked, 1, -1) + lvwRequests_auth.CheckedItems.Count
Dim b as Boolean = IIf(i > 0, True, False)

btnApprove_auth.Enabled = b
btnView_auth.Enabled = b
btnDeny_auth.Enabled = b

End Sub

Maybe this world is another planet’s Hell.
Aldous Huxley

eyes_015.gif___1129027102664
eyes_015.gif___1129027102664
 
Big,

That is terrific!!! As far as what you wrote:
If you move it to the MouseUp handler, you would also need to put it the KeyUp handler (some people still use the keyboard).
I don't understand why - but it's getting handled with exactly what you wrote...

Thanks so much - it's great!
(star)
Dot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top