CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 33
  1. #1
    Join Date
    Jul 2006
    Location
    The Netherlands
    Posts
    108

    ms flexgrid: single row select

    Is there a way to have only single row select in a ms flexgrid.
    Normally you can select multiple rows by mouse select or by using shift.

  2. #2
    Join Date
    Dec 2004
    Posts
    423

    Re: ms flexgrid: single row select

    The MsFlexgrid control has an event 'SelChange'
    It fires every time a user changes the selection.

    this code
    Code:
    If MsFlexGrid.Row - MsFlexGRid.RowSel <> 0 then
       'User selected more than one row
       'So Make the row and selected row the same
       MsFlexgrid.row = msflexgrid.rowsel
    
       'To get highlight you must set focus to the control then back to whatever else
       msflexgrid.SetFocus
    End if
    Hope that helps
    Even if everybody spoke the same language, nobody would be speaking the same language.

    --Daniel

  3. #3
    Join Date
    Jul 2006
    Location
    The Netherlands
    Posts
    108

    Re: ms flexgrid: single row select

    it still lets me selects the rows except for that there will be 1 selected after mouse release or key realease. But i want it so that they cant even select multiple rows

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: ms flexgrid: single row select

    You can use this flag:
    flexSelectionByRow 1 By Row.
    This forces selections to span entire rows, as in a multi-column list box or record-based display.
    If that's what you want. There is also a multiselect flag on the flexgrid property, as I recall.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jul 2006
    Location
    The Netherlands
    Posts
    108

    Re: ms flexgrid: single row select

    Edit:

    No srry i can still select more then 1 row

  6. #6
    Join Date
    Aug 2006
    Posts
    145

    Re: ms flexgrid: single row select

    Try something likw this:
    Code:
    Private bNoDrag As Boolean
    
    Private Sub Form_Load()
        MSFlexGrid1.SelectionMode = flexSelectionByRow
        MSFlexGrid1.FocusRect = flexFocusNone
    End Sub
    
    Private Sub MSFlexGrid1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
        If Button = vbLeftButton Then bNoDrag = True
    End Sub
    
    Private Sub MSFlexGrid1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
        If bNoDrag Then
            MSFlexGrid1.Row = MSFlexGrid1.MouseRow
            MSFlexGrid1.ColSel = MSFlexGrid1.Cols - 1
        End If
    End Sub
    
    Private Sub MSFlexGrid1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
        bNoDrag = False
    End Sub

  7. #7
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: ms flexgrid: single row select

    Hi !

    I dont know whih settings your MS Flexgrid has: use
    AllowBigSelection = true, FocusRect = flexFocusNone, Highlight = flexHighlightWithFocus

    Its quiet normal for a Flexgrid that it has normaly highlighted one row, but when you press the Shift button and select a row with the mouse then you will per sure get a multiselection in a flexgrid. you only can cancel that by code
    Code:
    Private Sub myGrid_Click()
    myGrid.RowSel = myGrid.Row
    End Sub
    But then, if you change Selection and press Shift together with mouse Leftclick the second line will do a short flash but the original selection will stay. This comes from that a Flexgrid normally selects all Lines between Row and RowSel. This is basically for a Flexgrid, and cannot be changed this way. If you only want to have permanent NO-Multiline-Selection you can only use: Highlight = FlexHighlightNever and then draein the selected line yourself. ( this is what in some cases I do )
    Code:
    'On the topmost of the Form write
    Option explicit
    Private m_LastSel as integer ' dont declare this variable in the function or it will not work
     
    ' then we have the function
    Private Sub SelectRow(iRow As Integer)
    Dim i As Integer
    With myGrid
    ' Firstwe unselect previous Selection (if there is one )
    If m_LastSel > 0 Then
    	 .Row = m_LastSel
    	 For i = 0 To .Cols - 1
    		.Col = i
    		.CellBackColor = RGB(255, 255, 255)
    		.CellForeColor = RGB(0, 0, 0)
    	 Next
    End If
    ' now select new selection and destroy any multiselection as input is .RowSel Value
    ' and you are setting roW = RowSel here
    .Row = iRow
    For i = 0 To .Cols - 1
    	.Col = i
    	.CellBackColor = RGB(0, 0, 255)
    	.CellForeColor = RGB(255, 255, 255)
    Next
    m_LastSel = iRow
    End With
    End Sub
     
    Private Sub myGrid_Click()
    SelectRow (myGrid.RowSel)
    End Sub
     
    Private Sub myGrid_SelChange()
    SelectRow (myGrid.RowSel)
    End Sub
    Last edited by JonnyPoet; September 8th, 2006 at 06:03 AM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  8. #8
    Join Date
    Jul 2006
    Location
    The Netherlands
    Posts
    108

    Re: ms flexgrid: single row select

    Ok and how can we do it like that?

    I am not very advanced in these things

  9. #9
    Join Date
    Dec 2004
    Posts
    423

    Re: ms flexgrid: single row select

    On the MouseDown event use this
    Code:
    MSFlexGrid.Redraw = False
    then On the mouseup event use this
    Code:
    If MsFlexGrid.Row - MsFlexGRid.RowSel <> 0 then'User selected more than one row
    'So Make the row and selected row the same
    MsFlexgrid.row = msflexgrid.rowsel
    'To get highlight you must set focus to the control then back to whatever else
    msflexgrid.SetFocus
    End if
    msflexgrid.redraw = true
    On the KeyDown event use this
    Code:
       If shift = 1 And (KeyCode = 38 Or KeyCode = 40) then
    	  msflexgrid.redraw = false
       else
    	 msflexgrid.redraw = true
       end if
    And finally on the keyup event
    Code:
    If MsFlexGrid.Row - MsFlexGRid.RowSel <> 0 then'User selected more than one row
    'So Make the row and selected row the same
    MsFlexgrid.row = msflexgrid.rowsel
    'To get highlight you must set focus to the control then back to whatever else
    msflexgrid.SetFocus
    End if
    msflexgrid.redraw = true
    Even if everybody spoke the same language, nobody would be speaking the same language.

    --Daniel

  10. #10
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: ms flexgrid: single row select

    Quote Originally Posted by Sabin_33
    On the KeyDown event use this
    Code:
    If shift = 1 And (KeyCode = 38 Or KeyCode = 40) then
    	 Debug.Print " I'm here"
    	 msflexgrid.redraw = false
    else
    	 msflexgrid.redraw = true
    end if
    Hi Sabin ! Try it with a debug Statement ( I inserted in red ) It is never reached in my Grid. And BigSelection doesn't work that way it only selects one column when I try this in my Grid. So which properties have you set to get this working?
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  11. #11
    Join Date
    Aug 2006
    Posts
    145

    Re: ms flexgrid: single row select

    i forgot about changing selection with the keys, here's my version that handles that too:
    Code:
    Private bForceKeySel As Boolean, bForceMouseSel As Boolean
    
    Private Sub MSFlexGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
        If Shift And vbShiftMask Then MSFlexGrid1.Redraw = False: bForceKeySel = True
    End Sub
    
    Private Sub MSFlexGrid1_KeyUp(KeyCode As Integer, Shift As Integer)
        If (Shift And vbShiftMask) = 0 Then MSFlexGrid1.Redraw = True: bForceKeySel = False
    End Sub
    
    Private Sub MSFlexGrid1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
        MSFlexGrid1.Redraw = False: bForceMouseSel = True
    End Sub
    
    Private Sub MSFlexGrid1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
        MSFlexGrid1.Redraw = True: bForceMouseSel = False
    End Sub
    
    Private Sub MSFlexGrid1_SelChange()
        If bForceKeySel Or bForceMouseSel Then
            MSFlexGrid1.RowSel = MSFlexGrid1.Row
            MSFlexGrid1.ColSel = MSFlexGrid1.Cols - 1
        End If
    End Sub
    
    Private Sub Form_Load()
        MSFlexGrid1.SelectionMode = flexSelectionByRow
        MSFlexGrid1.FocusRect = flexFocusNone
    End Sub

  12. #12
    Join Date
    Jul 2006
    Location
    The Netherlands
    Posts
    108

    Re: ms flexgrid: single row select

    bushmobile:
    When i use your code i see no selection even when i only click 1 row.
    JonnyPoet:
    Your code makes everything i click blue. I can make more then 1 row blue and i cant unselect em.

  13. #13
    Join Date
    Aug 2006
    Posts
    145

    Re: ms flexgrid: single row select

    Quote Originally Posted by brainsmasher
    bushmobile:
    When i use your code i see no selection even when i only click 1 row.
    strange, works absolutely fine for me.

    trying dropping the code and flexgrid on to a new form and see what happens - it should work ok

  14. #14
    Join Date
    Dec 2004
    Posts
    423

    Re: ms flexgrid: single row select

    Here is a project with Flex grid that allows only one row to be selected.

    Hey Johnny, I had to change the code a little. But ever run I did with this works fine.
    Attached Files Attached Files
    Even if everybody spoke the same language, nobody would be speaking the same language.

    --Daniel

  15. #15
    Join Date
    Jul 2006
    Location
    The Netherlands
    Posts
    108

    Re: ms flexgrid: single row select

    Nice colors :P
    Tnx this works

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured