Custom Checkboxlist control

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DragonLord
    New Member
    • Mar 2007
    • 122

    Custom Checkboxlist control

    Well this is new territory for me and my searches on google have proved fruitless.

    Here is what I am trying to do.

    I want to create on a windows from (using C#) a small area with a verticle scroll bar that will have x number of checkboxes (known only at runtime) I would like them to be 3 columns wide and just keepgoign downwards.

    I used the checkboxlist control and this idea is almost perfect except it keeps adding horizontally and i want the checkboxes added vertially.

    I am stumped as to accomplish this, please any of you guru's out there with some amazingideas please share them.
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    You're probably going to have to provide a bit of mathematical wizardry to do this. Not too hard...

    First you need to figure out how many columns you want and how far apart you want them both horizontally and vertically.

    Insert a counter and when the counter reaches the number of columns you wish, then reset the left and top positions and the counter

    Pseudo VB
    Code:
    'Assume that "CheckBoxItems" is a list containing 
    'my checkbox objects:
    ' i.e. New List(Of CheckBox)
    
    'Assume that "MyContainer" is some 
    'container control that allows you to add controls and 
    'position them using their location property.
    
    Dim NumColumns = 3 'I want 3 columns
    Dim TopOffset = 25 'I want each row 25 pixels high
    Dim LeftOffset = 100 'I want each column 100 pixels wide
    
    'Position the first checkbox: 25 pixels from top left corner
    'of the container boundary... Don't forget to import the 
    'System.Drawing namespace
    Dim StartPosition = New Point(25, 25) 
    
    'Note the use of \ rather than /, this gets the integer 
    'part of the result.
    Dim NumRows = CheckBoxItems.Count \ NumColumns
    If CheckBoxItems.Count Mod NumColumns <> 0 Then
        NumRows += 1
    End If
    
    Dim CheckBoxIter = CheckBoxItems.GetEnumerator
    
    Dim CurrentPosition _
      As New Point(StartPosition.X, StartPosition.Y)
    
    For iCrntRow = 1 To NumRows
    
        'Reset left to starting point for the new row
        CurrentPosition.X = StartPosition.X
    
        For iCrntCol = 1 To NumColumns
            If CheckBoxIter.MoveNext Then
    
                'Get the current checkbox and set its location
                Dim ThisBox = CheckBoxIter.Current
                ThisBox.Location = CurrentPosition
                
                'Don't forget to add it to the container...
                MyContainer.Controls.Add(ThisBox)
    
                'Increment left for the next checkbox...
                CurrentPosition.X += LeftOffset
    
            Else
    
                'This should only ever be reached if our last row
                'doesn't contain the same number of items as there
                'are columns...
                Exit For 
    
            End If
        Next
    
        'Increment the current top for the next checkbox row...
        CurrentPosition.Y += TopOffset
    
    Next
    Now it depends on the container object you're putting the controls into as to how successful this will be... there's a CheckBoxList and RadioButtonList in ASP.NET, both of which allow you to set the RepeatColumns property to do this automatically for you. But so far, I've been unable to find anything like that in Windows Forms, the CheckedListBox isn't the same...

    Comment

    • balabaster
      Recognized Expert Contributor
      • Mar 2007
      • 798

      #3
      This would work if your container is a Panel...

      Comment

      • DragonLord
        New Member
        • Mar 2007
        • 122

        #4
        Would the panel give you scroll bars though? What i am looking for is a view of 3 columns wide by 3 rows hide but yet you could have 60 or 90 items so they would have to scroll down to see the next set of checkboxes until they select all of them.

        Man I feel like a newbie,
        thanks in advance.

        Comment

        • DragonLord
          New Member
          • Mar 2007
          • 122

          #5
          Well Tickle me pink i should just listen to the man who has the answer, Thanks I will give it a try,i went and had a look and would you know it that I have never seen the autoscroll property lol...

          Thanks again.

          Comment

          • balabaster
            Recognized Expert Contributor
            • Mar 2007
            • 798

            #6
            Lol, yep - just turn on AutoScroll... I tested it by setting up adding my 100 checkboxes to a List(Of CheckBox) to use as the source for my panel...

            It worked like a charm. Set the background color to Window and the border to 3D and it looks like any other listbox or listview control...

            You'll have to do a little extra wrapper work if you want to find a list of checked items... my advice is to write yourself a class that allows you to pass in your list of checkboxes, allows you to set the number of columns, and the row height and column width and has a readonly property that returns a list of selected items. The same as the listbox does and then you should be able to pretty much work with it as if it were a regular listbox.

            Comment

            • DragonLord
              New Member
              • Mar 2007
              • 122

              #7
              Thanks so much that is exacly what I need, time to get coding. Much appreciated as I spent much time messing with the checklistbox and datagrid etc. etc.....

              Thanks..

              Comment

              Working...