Results 1 to 9 of 9

Thread: [RESOLVED] MSFLEXGRID - how to get column number of 1st displayed column?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Location
    Kerala, India
    Posts
    275

    Resolved [RESOLVED] MSFLEXGRID - how to get column number of 1st displayed column?

    In an MSFLEXGRID with scroll bars, how do i get column/row number of the 1st displayed column/row?

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: MSFLEXGRID - how to get column number of 1st displayed column?

    One way I can think of is to use .RowIsVisible property for every row starting with the .TopRow

    Similarly, you can use .ColIsVisible for columns
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    Addicted Member Optional's Avatar
    Join Date
    Jan 2010
    Location
    Rudimentary Space
    Posts
    214

    Re: MSFLEXGRID - how to get column number of 1st displayed column?

    All you should need is the TopRow and LeftCol properties.

    The TopRow property returns the uppermost visible row, other than the fixed row.

    The LeftCol property returns the left-most visible column, other than the fixed column.

    A few examples on using the TopRow and LeftCol properties.

    To get the column/row number of the 1st displayed column/row:
    VB Code:
    1. Private Sub uiGetFirstVisibleRowAndCol_Click()
    2.     Call MsgBox("1st visible Row is: " & MSFlexGrid1.TopRow)
    3.     Call MsgBox("1st visible Column is: " & MSFlexGrid1.LeftCol)
    4. End Sub

    To Write a value to the 1st displayed column/row:
    VB Code:
    1. Private Sub uiSetValue_Click()
    2.     MSFlexGrid1.TextMatrix(MSFlexGrid1.TopRow, MSFlexGrid1.LeftCol) = "New Value in 1st visible column/Row"
    3. End Sub

    To Get the value of the 1st displayed column/row:
    VB Code:
    1. Private Sub uiGetValue_Click()
    2.     Call MsgBox(MSFlexGrid1.TextMatrix(MSFlexGrid1.TopRow, MSFlexGrid1.LeftCol))
    3. End Sub
    Last edited by Optional; Feb 15th, 2010 at 07:38 PM.



    Kind Regards,
    Optional



    If you feel this post has helped in answering your question please return the favour and Rate this post.
    If your problem has been solved and your question has been answered mark the thread as [RESOLVED] by selecting the Thread Tools menu option at the top and clicking the Mark Thread Resolved menu item.


    VB6 - (DataGrid) Get the Row selected with the right mouse button



  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Location
    Kerala, India
    Posts
    275

    Re: MSFLEXGRID - how to get column number of 1st displayed column?

    Thanks Optional.

    Similarly, what about the last row/column.

    Another question : is it possible to hide the scroll bar (without disabling scrolling)?

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: MSFLEXGRID - how to get column number of 1st displayed column?

    There is no property for the last row/column, you need to calculate it somehow.

    The two main ways are:
    • loop from .TopRow to .Rows-1 checking .RowIsVisible() to see when it becomes False - but be careful of rows that are partly visible, as the way RowIsVisible treats it may not be what you expect.
    • write your own code to do the equivalent of .RowIsVisible, based on .RowPos and .RowHeight for each of the rows. This takes more work, but you can determine when a row is partly visible, and treat it as you want.



    You can't set a property to hide the scrollbar without disabling scrolling, but what you can do is put the grid inside a picturebox, and set the width of the picturebox to the width of the grid minus the width of the scrollbar.

  6. #6
    Addicted Member Optional's Avatar
    Join Date
    Jan 2010
    Location
    Rudimentary Space
    Posts
    214

    Re: MSFLEXGRID - how to get column number of 1st displayed column?

    I'm assuming you mean last visible column/row.

    If so you need to use the .ColIsVisible and .RowIsVisible properties of the Grid in combination with .TopRow and .LeftCol as koolsid touched on and si_the_geek mentioned above.

    The following sample code will display the number of the last visible col/row in a MSFLEXGRID.
    VB Code:
    1. Private Sub uiGetLastVisibleColRows_Click()
    2.     Dim i As Integer
    3.     Dim lastVisible As Integer
    4.    
    5.     lastVisible = 0
    6.    
    7.     For i = MSFlexGrid1.TopRow To (MSFlexGrid1.Rows - 1)
    8.         If ((Not (MSFlexGrid1.RowIsVisible(i))) Or (i = (MSFlexGrid1.Rows - 1))) Then
    9.             lastVisible = (i - MSFlexGrid1.FixedRows)
    10.             Exit For
    11.         End If
    12.     Next i
    13.    
    14.     Call MsgBox("Last visible Row is: " & lastVisible)
    15.    
    16.     lastVisible = 0
    17.    
    18.     For i = MSFlexGrid1.LeftCol To (MSFlexGrid1.Cols - 1)
    19.         If ((Not (MSFlexGrid1.ColIsVisible(i))) Or (i = (MSFlexGrid1.Cols - 1))) Then
    20.             lastVisible = (i - MSFlexGrid1.FixedCols)
    21.             Exit For
    22.         End If
    23.     Next i
    24.    
    25.     Call MsgBox("Last visible Column is: " & lastVisible)
    26. End Sub

    The "MSFlexGrid1.Cols - 1" in the for loop is because .ColIsVisible and .RowIsVisible are 0 based.

    The additional checks on "Or (i = (MSFlexGrid1.Rows - 1))" and "Or (i = (MSFlexGrid1.Cols - 1))" is used to catch the correct values even if the last visible column/row is the actual last column/row in the grid.

    Edit
    To add to si_the_geek's comment on partial rows/columns. When I tested the code any partially shown row and column would be considered visible and .RowIsVisible and .ColIsVisible returned True. I tested more by changing the height/width of the grid at runtime dynamically and it seems that even if only a single pixel is visible the row/column is deemed visible.

    Watch out for that

    Hope this Helps.
    Last edited by Optional; Feb 16th, 2010 at 09:14 AM.



    Kind Regards,
    Optional



    If you feel this post has helped in answering your question please return the favour and Rate this post.
    If your problem has been solved and your question has been answered mark the thread as [RESOLVED] by selecting the Thread Tools menu option at the top and clicking the Mark Thread Resolved menu item.


    VB6 - (DataGrid) Get the Row selected with the right mouse button



  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: MSFLEXGRID - how to get column number of 1st displayed column?

    @svg3414: Seems like you missed Post 2....
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Location
    Kerala, India
    Posts
    275

    Re: MSFLEXGRID - how to get column number of 1st displayed column?

    Thanks Optional & Si_the_Geek for the advice.

    Actually my problem is this:

    I want to make the cells editable as in Spreadsheet applications like Excel etc. For this I am diplaying a textbox at the exact position of the selected cell. This creates problems when scrolling if the selected cell goes out of the display.

    I have made some modifications based on your suggestions and now it seems to work ok.

  9. #9
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: MSFLEXGRID - how to get column number of 1st displayed column?

    Quote Originally Posted by svg3414 View Post
    Similarly, what about the last row/column.
    Something like this will give you last visible row:
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Dim lastRowVisible As Long
    
        lastRowVisible = GetLastRow(MSFlexGrid1)
        MsgBox "Last visible row Index = " & lastRowVisible
    
    End Sub
    
    Private Function GetLastRow(flx As MSFlexGrid) As Long
    Dim TotalRowCount As Integer
    
        'assuming all rows have the same row height
        TotalRowCount = flx.Height / flx.RowHeight(0) - flx.FixedRows
        GetLastRow = (flx.TopRow + TotalRowCount) - 1
        
        'check if row is visible partially
        If flx.Height <= flx.RowPos(flx.Rows - 1) + flx.RowHeight(flx.Rows - 1) Then
            GetLastRow = GetLastRow - 1
        End If
        
        'check if horizontal scrollbar is visible (there could be a better way to determine this)
        If flx.Width <= flx.ColPos(flx.Cols - 1) + flx.ColWidth(flx.Cols - 1) Then
            GetLastRow = GetLastRow - 1
        End If
    
    End Function
    Something similar to that will give you last visible column.

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