how to disable multi select of checkbox in listview ?
-
Hi , Is there any way to disable multi select of checkbox in a list view ?.ie one should be able to check only one row at a time. With warm regards :laugh:
-
Hi , Is there any way to disable multi select of checkbox in a list view ?.ie one should be able to check only one row at a time. With warm regards :laugh:
You can use
ItemChecked
event and uncheck all already checked items in this event and leave only the newly checked intact. Although I think it's not a good UI design if you have checkboxes for list items and still the user is able to check only one item at a time. Checkboxes are used when multiple choices can be selected, so single selection list with check boxes would give the user a wrong impression. Could you simply set MultiSelect to false (without checkboxes).The need to optimize rises from a bad design.My articles[^]
-
Hi , Is there any way to disable multi select of checkbox in a list view ?.ie one should be able to check only one row at a time. With warm regards :laugh:
did you mean listbox or CheckBoxList???? if you mean listbox, so there is no problem, just set SelectionMode property to Single. if you mean CheckBoxList, so it's better to use RadioListBox for single selection. if you want CheckBoxList for single selection you must code for it on selectedindexchange event on checkboxList.
Human knowledge belongs to the world
-
Hi , Is there any way to disable multi select of checkbox in a list view ?.ie one should be able to check only one row at a time. With warm regards :laugh:
I know this question was asked over a year ago, but I have been searching for an answer myself with little success and wanted to get this out there to save others who may be searching the hours of frustration I encountered. I finally came up with the following VB .NET code to take care of the problem:
'Declare a Form global boolean to keep the ItemChecked event from being called in an infinite loop Dim bFirstChange as Boolean = True Private Sub Listview1\_ItemChecked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles Listview1.ItemChecked If ((ModifierKeys = Keys.Control) Or (ModifierKeys = Keys.Shift)) Then If (bFirstChange) Then bFirstChange = False e.Item.Checked = Not e.Item.Checked Else bFirstChange = True End If Exit Sub End If 'Accentuate that the row is checked If (e.Item.Checked) Then e.Item.SubItems(0).Text = "+" e.Item.BackColor = Color.LightYellow Else e.Item.SubItems(0).Text = "-" e.Item.BackColor = Color.White End If End Sub
This worked for me to keep the Checked property unchanged. Not sure about the "thread safe-ness" of this, but not a problem for me in the form I am working with. You should be able to easily convert this if you need it in C# instead of VB. HTH - hground
-
I know this question was asked over a year ago, but I have been searching for an answer myself with little success and wanted to get this out there to save others who may be searching the hours of frustration I encountered. I finally came up with the following VB .NET code to take care of the problem:
'Declare a Form global boolean to keep the ItemChecked event from being called in an infinite loop Dim bFirstChange as Boolean = True Private Sub Listview1\_ItemChecked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles Listview1.ItemChecked If ((ModifierKeys = Keys.Control) Or (ModifierKeys = Keys.Shift)) Then If (bFirstChange) Then bFirstChange = False e.Item.Checked = Not e.Item.Checked Else bFirstChange = True End If Exit Sub End If 'Accentuate that the row is checked If (e.Item.Checked) Then e.Item.SubItems(0).Text = "+" e.Item.BackColor = Color.LightYellow Else e.Item.SubItems(0).Text = "-" e.Item.BackColor = Color.White End If End Sub
This worked for me to keep the Checked property unchanged. Not sure about the "thread safe-ness" of this, but not a problem for me in the form I am working with. You should be able to easily convert this if you need it in C# instead of VB. HTH - hground
Cool - thanks for this, and for letting me know on the other, older thread. I ended up with a work-around keeping track of which entries were supposed to be checked, but this looks much simpler. I'll convert it to C# and give it a go. Thanks again :thumbsup:
Days spent at sea are not deducted from one's alloted span - Phoenician proverb
-
I know this question was asked over a year ago, but I have been searching for an answer myself with little success and wanted to get this out there to save others who may be searching the hours of frustration I encountered. I finally came up with the following VB .NET code to take care of the problem:
'Declare a Form global boolean to keep the ItemChecked event from being called in an infinite loop Dim bFirstChange as Boolean = True Private Sub Listview1\_ItemChecked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) Handles Listview1.ItemChecked If ((ModifierKeys = Keys.Control) Or (ModifierKeys = Keys.Shift)) Then If (bFirstChange) Then bFirstChange = False e.Item.Checked = Not e.Item.Checked Else bFirstChange = True End If Exit Sub End If 'Accentuate that the row is checked If (e.Item.Checked) Then e.Item.SubItems(0).Text = "+" e.Item.BackColor = Color.LightYellow Else e.Item.SubItems(0).Text = "-" e.Item.BackColor = Color.White End If End Sub
This worked for me to keep the Checked property unchanged. Not sure about the "thread safe-ness" of this, but not a problem for me in the form I am working with. You should be able to easily convert this if you need it in C# instead of VB. HTH - hground
I've got a better solution:
Private Sub ListView1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck
If (ModifierKeys And (Keys.Shift Or Keys.Control)) <> 0 Then e.NewValue = e.CurrentValue
End SubEnjoy
-
I've got a better solution:
Private Sub ListView1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck
If (ModifierKeys And (Keys.Shift Or Keys.Control)) <> 0 Then e.NewValue = e.CurrentValue
End SubEnjoy
For some reason, the first solution, translated into C++/CLI, will create a stack overflow if you shift- or control-click anywhere besides the "Name" column. MarqW's solution, translated into C++/CLI works fine. No need for global variables.
private: System::Void listView1_ItemCheck(System::Object^ sender, System::Windows::Forms::ItemCheckEventArgs^ e)
{
if( (ModifierKeys == Keys::Control) || (ModifierKeys == Keys::Shift) )
{
e->NewValue = e->CurrentValue;
}
}It's the difference between the
ItemCheck
andItemChecked
events.modified on Monday, August 23, 2010 3:30 PM
-
I've got a better solution:
Private Sub ListView1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck
If (ModifierKeys And (Keys.Shift Or Keys.Control)) <> 0 Then e.NewValue = e.CurrentValue
End SubEnjoy
-
Seriously? The reply above has already converted it to C#. Even if you hadn't seen that, it's one line of code which pretty much translates as-is, apart from If Then, becomes If( )
-
Phew, i found out : this is in C#
if (Control.ModifierKeys == Keys.Shift || Control.ModifierKeys == Keys.Control)
{
e.NewValue = e.CurrentValue;
}