Link to home
Start Free TrialLog in
Avatar of scmmicro
scmmicro

asked on

Placing Checkboxes in flexgrid

I have a MSFlexgrid in my VB form.It has one fixed row but no fixed column.When the form loads i want to place checkboxes in the first column of all the rows.The user should be able to select multiple rows in the flexgrid .When they select multiple rows the checkboxes in those rows should also get checked.Can anyone send me a sample code for the same?

Thanks in advance
Avatar of dbrckovi
dbrckovi
Flag of Croatia image

Try this:

On an empty form create a checkbox, rename it to "Check", set its index to 0, and make it invisible.
Create Msflexgrid, and rename it to "Grid".

Paste this code, and run it:
--------------------------------------------------------------------
Dim RowSelected() As Boolean
Dim NumberOfCheckBoxes As Integer

Private Sub Check_Click(Index As Integer)
    If Check(Index).Value = 1 Then RowSelected(Index + Grid.TopRow - 1) = True
    If Check(Index).Value = 0 Then RowSelected(Index + Grid.TopRow - 1) = False
End Sub

Private Sub Form_Load()
   
   
    Grid.FixedRows = 1
    Grid.Cols = 5
    Grid.Rows = 30
    Grid.ScrollTrack = True
    ReDim RowSelected(Grid.Rows) As Boolean
   
    For X = 1 To Grid.Rows - 1
        If Grid.RowIsVisible(X) = True Then
            Load Check(X)
            NumberOfCheckBoxes = X
            Check(X).Visible = True
            Check(X).Left = Grid.Left + 200
            Check(X).Top = Grid.RowPos(X) + Grid.Top + 50
            Check(X).ZOrder (0)
         End If
   
    Next X

End Sub

Private Sub Grid_Click()
    Dim ClickedRow As Integer
   
    RowSelected(Grid.MouseRow) = True
       
    Call UpdateCheckboxes
End Sub

Sub UpdateCheckboxes()
    For X = Grid.TopRow To Grid.TopRow + NumberOfCheckBoxes - 1
        If RowSelected(X) = True Then
            Check(X - Grid.TopRow + 1).Value = 1
        Else
            Check(X - Grid.TopRow + 1).Value = 0
        End If
    Next X

End Sub

Private Sub Grid_Scroll()
    Call UpdateCheckboxes
End Sub
----------------------------------------------------------------

Theory behind this is following:

Steps:
 - Adjust the grid properties to your needs.
 - Create several checkboxes, so every visible row has one checkbox, position them where you want and make them visible.
 - When clicked on checkbox, calculate for which row it is currently assigned, and update the according array element (RowIsSelected(x))
 - When Grid is clicked, update the accordig array element (RowIsSelected), and update the checkboxes
 - When Grid is scrolled, just update the checkboxes

Now in order to see if a row is selected, use       If RowIsSelected(Desired_Row) = True Then     <do something>
Add      Grid.FixedCols = 0      to Form_Load event.
Avatar of scmmicro
scmmicro

ASKER

Hi dbrckovi
i tried the sample which you provided.The checkboxes are not getting displayed at all in the grid.
I have tried the same example I posted, step by step as I described and it works fine.
Maybe checkboxes are displayed behind flex grid.    (Although,      Check(X).ZOrder (0)      ensures that they are moved to front)

Try changing the line:                     Check(X).Left = Grid.Left + 200    
                           to:                     Check(X).Left = grid.Left - 300
in Form_Load.

This will display them on the left side of grid.

Are you using VB.NET ?
I think VB.NET doesn't support Load to create new instances of controls.

I'm using VB6 and everything works.
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial