Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to disable multi select of checkbox in listview ?

how to disable multi select of checkbox in listview ?

Scheduled Pinned Locked Moved C#
tutorialquestion
11 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Denver Thomas
    wrote on last edited by
    #1

    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:

    W R H 3 Replies Last reply
    0
    • D Denver Thomas

      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:

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      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[^]

      1 Reply Last reply
      0
      • D Denver Thomas

        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:

        R Offline
        R Offline
        Reza Raad
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • D Denver Thomas

          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:

          H Offline
          H Offline
          hground
          wrote on last edited by
          #4

          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

          M M 2 Replies Last reply
          0
          • H 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

            M Offline
            M Offline
            molesworth
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • H 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

              M Offline
              M Offline
              MarqW
              wrote on last edited by
              #6

              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 Sub

              Enjoy

              M J 2 Replies Last reply
              0
              • M MarqW

                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 Sub

                Enjoy

                M Offline
                M Offline
                mycleverscreenname
                wrote on last edited by
                #7

                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 and ItemChecked events.

                modified on Monday, August 23, 2010 3:30 PM

                1 Reply Last reply
                0
                • M MarqW

                  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 Sub

                  Enjoy

                  J Offline
                  J Offline
                  jerryno6
                  wrote on last edited by
                  #8

                  It's very good guide. Can you convert it to C# ?

                  M 1 Reply Last reply
                  0
                  • J jerryno6

                    It's very good guide. Can you convert it to C# ?

                    M Offline
                    M Offline
                    MarqW
                    wrote on last edited by
                    #9

                    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( )

                    J 1 Reply Last reply
                    0
                    • M MarqW

                      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( )

                      J Offline
                      J Offline
                      jerryno6
                      wrote on last edited by
                      #10

                      Phew, i found out : this is in C#

                      if (Control.ModifierKeys == Keys.Shift || Control.ModifierKeys == Keys.Control)
                      {
                      e.NewValue = e.CurrentValue;
                      }

                      M 1 Reply Last reply
                      0
                      • J jerryno6

                        Phew, i found out : this is in C#

                        if (Control.ModifierKeys == Keys.Shift || Control.ModifierKeys == Keys.Control)
                        {
                        e.NewValue = e.CurrentValue;
                        }

                        M Offline
                        M Offline
                        MarqW
                        wrote on last edited by
                        #11

                        Just a little improvement for you. This one only performs one check condition, and handles the case if both keys are pressed (or one key with Alt or Win)

                        if( (Control.ModifierKeys & (Keys.Shift | Keys.Control)) )
                        {
                        e.NewValue = e.CurrentValue;
                        }

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups