Check Only One!

TomPhillips

Active member
Joined
Feb 24, 2005
Messages
33
Programming Experience
10+
Check Only One! RESOLVED

I need a way to make the check boxes in a ListView act like radio buttons. Only one can be checked at a time and if a new one is checked, any other one is cleared.
 
Last edited:
I think this is how to do it (I don't have it (.NET) here in front of me so I can't verify, but if I remember right...)
1) Use the ItemChecked event.
2) Inside that event, loop through all items in the list, if it isn't the one clicked on, clear the Checked value.

I think that's right.

Tg
 
OK, I'm not seeing it. How do I tell if the item is not the one that triggered the event?

VB.NET:
Private Sub ListView1_ItemCheck( _
	 ByVal sender As Object, _
	 ByVal e As System.Windows.Forms.ItemCheckEventArgs) _
	 Handles ListView1.ItemCheck
 
Dim i As Int16 
 
For i = 0 To Me.ListView1.Items.Count - 1
 
	If Me.ListView1.Items.Item(i).Checked Then
 
		Me.ListView1.Items(i).Checked = False
 
	End If
 
Next

This just get's me a stack overflow.
 
Hmm.... didn't think about that.... each time you set one of those to False, it fires off the event again, which then sets another one to false, causing it to fire, etc.....

ick... will need to re-think this. Unfortunately I don't have .NET infront of me so I won't be able to try anything out until later.

Tg
 
How about this:

VB.NET:
Private Sub ListView1_ItemCheck( _
	 ByVal sender As Object, _
	 ByVal e As System.Windows.Forms.ItemCheckEventArgs) _
	 Handles ListView1.ItemCheck
	 Dim i As Int16 
 
	 For i = 0 To Me.ListView1.Items.Count - 1
		 Try
			 If e.Index <> i Then
				 Me.ListView1.Items.Item(i).Checked = False
			 End If
		 Catch exo As Exception
			 MessageBox.Show(exo.ToString,...
		 End Try
	 Next
End Sub

Seems to work. See any problems?
 
Back
Top