GridView

Post your Gambas programming questions here.
Post Reply
User avatar
Got2BeFree
Posts: 91
Joined: Saturday 26th November 2016 2:52am
Location: Lost

GridView

Post by Got2BeFree »

In a GridView, is it possible to word-wrap long text and set the row to accommodate the added lines, all being done dynamically? I know I can explicitly set a row's height prior to accessing and loading data, but I haven't figured out how to do it while accessing and loading data from an sqlite db.

If word-wrapping isn't even possible in a GridView, then the above is a moot point and I'll go back to figuring out how to make simple things more complicated. :lol:
sholzy

I'm wondering around lost in the past, not knowing where the present is.
User avatar
cogier
Site Admin
Posts: 1124
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: GridView

Post by cogier »

I have been playing with this today. Have a look at the attached code and see if you can make sense of it.

Image
Test2.tar.gz
(70.04 KiB) Downloaded 452 times
User avatar
Got2BeFree
Posts: 91
Joined: Saturday 26th November 2016 2:52am
Location: Lost

Re: GridView

Post by Got2BeFree »

Excellent, thanks!
I haven't had a chance to look at the code yet, but a quick run gave a different output than your pic shows. No big deal since I now know word-wrap and row height can be set on the fly. A little later today I'll go through the code and see how I can adapt it to my project and post my findings.

Thanks again, Charlie!
Screenshot_20190526_123613.png
Screenshot_20190526_123613.png (56.51 KiB) Viewed 9493 times
sholzy

I'm wondering around lost in the past, not knowing where the present is.
User avatar
Got2BeFree
Posts: 91
Joined: Saturday 26th November 2016 2:52am
Location: Lost

Re: GridView

Post by Got2BeFree »

Finally had a chance to look through your code and ended up writing new code based on what your's did. Without your example, I probably would still be trying to figure this out.

Since I have only 1 field in my db table that would need wrapping, it was easy enough to set the row height for rows needing wrapped. And, since that field has a set max length of characters, the text only needed to be split into 2 lines.

This code populates the GridView. Lines 2 and 3 determine which rows need the height increased. I found on my system, 50 was the perfect row height for 2 lines. I added lines 2 - 8 and 14 to the existing code, and changed line 13.
            For Each MGlobal.hResData
                If Len(MGlobal.hResData!page_desc) > 45 Then   ' set length for wrap
                    gridviewPages.Rows[iRow].Height = 50   ' set row height for wrapped text
                    sText = MGlobal.hResData!page_desc
                    WordWrap()
                Else
                    sWordWrap = MGlobal.hResData!page_desc
                Endif
                gridviewPages[iRow, 0].Text = MGlobal.hResData!id
                gridviewPages[iRow, 1].Text = Str$(DateAdd(CDate("1/1/1970"), gb.Second, Global.hResData!page_rec_date))   ' convert from unix timestamp
                gridviewPages[iRow, 2].Text = MGlobal.hResData!page_name
                gridviewPages[iRow, 3].Text = MGlobal.hResData!sql_table_name
                gridviewPages[iRow, 4].Text = sWordWrap
                sWordWrap = ""
                Inc iRow
            Next
This routine does the wrapping. It finds the best spot to wrap so no words are split.
Public Sub WordWrap()

    Dim siPos As Short = 45    ' wrap length

    While Mid$(sText, siPos, 1) <> " "       ' find white space to make clean wrap
        Dec siPos
    Wend

    sWordWrap = Mid$(sText, 1, siPos) & Chr(10) & Mid$(sText, siPos + 1)      ' wrap

End
What the results look like....
Screenshot_20190526_221318.png
Screenshot_20190526_221318.png (30.45 KiB) Viewed 9487 times
sholzy

I'm wondering around lost in the past, not knowing where the present is.
User avatar
gbWilly
Posts: 68
Joined: Friday 23rd September 2016 11:41am
Location: Netherlands
Contact:

Re: GridView

Post by gbWilly »

Hi,

I made your WordWrap into a function that returns the 'wrapped' text.
This way it is universally and even recursively usable for all kinds of situations.
Nice one for in my library ;)
Public Function WordWrap(TextToWrap As String, LengthToWrap As Integer) As String
  
    Dim iCount As Integer
    
    iCount = LengthToWrap
    While Mid$(TextToWrap, iCount, 1) <> " "        'find first white space backward from LenghtToWrap to make the split
        Dec iCount
    Wend
 
    Return Subst("&1\n&2", Mid$(TextToWrap, 1, iCount), Mid$(TextToWrap, iCount + 1))
 
End
Usage in you example would be:
For Each MGlobal.hResData
    If Len(MGlobal.hResData!page_desc) > 45 Then   ' set length for wrap
        gridviewPages.Rows[iRow].Height = 50   ' set row height for wrapped text
        sText = MGlobal.hResData!page_desc
        sWordWrap = WordWrap(sText, 45)
    Else
        sWordWrap = MGlobal.hResData!page_desc
    Endif
    gridviewPages[iRow, 0].Text = MGlobal.hResData!id
    gridviewPages[iRow, 1].Text = Str$(DateAdd(CDate("1/1/1970"), gb.Second, Global.hResData!page_rec_date))   ' convert from unix timestamp
    gridviewPages[iRow, 2].Text = MGlobal.hResData!page_name
    gridviewPages[iRow, 3].Text = MGlobal.hResData!sql_table_name
    gridviewPages[iRow, 4].Text = sWordWrap
    sWordWrap = ""
    Inc iRow
Next
Want to split in 3 lines say at 45 and 90?
Use it recursively
sWordWrap = WordWrap(WordWrap(sText, 90), 45)
And you can go on and on and on....

P.S. Wrote this code right here in editor window, so might have typos and such :shock:
gbWilly
- Dutch translation for Gambas3
- Gambas wiki content contributer


... there is always a Catch if things go wrong!
User avatar
Got2BeFree
Posts: 91
Joined: Saturday 26th November 2016 2:52am
Location: Lost

Re: GridView

Post by Got2BeFree »

gbWilly wrote: Monday 27th May 2019 3:37pm Hi,

I made your WordWrap into a function that returns the 'wrapped' text.
This way it is universally and even recursively usable for all kinds of situations.
Nice one for in my library ;)
Nice! Glad my code was found useful and made even better with only slight modifications. :)

I like having code posted this way since it shows how it's applied in real-world usage.
sholzy

I'm wondering around lost in the past, not knowing where the present is.
User avatar
Got2BeFree
Posts: 91
Joined: Saturday 26th November 2016 2:52am
Location: Lost

Re: GridView

Post by Got2BeFree »

Another GridView question...

Anyone know what the default height of a row is? When I ran cogier's example, my output was much worse than his. I'm sure my font size had much to do with my results, but still, even when changing font size, a GridView row height should change accordingly, but not if you set the row height manually.

When we set row height manually, like what I needed to do (word wrapping) in my original post, font height no longer has a say in the height. If we set row height at say, 50, which works great on my system for 2 lines wrapped, it may not work at all on your system and may be too much or too little.

So if, for example, Gambas takes font height + padding above and below the font to determine row height, we would need to do the same manually with some code. But how do we know what the font size (in points?) a system is using? For the above code example to be truly useful, we would need to dynamically adjust row height based on the number of wrap lines being pushed to a cell * font points + padding above and below. I seem to recall a thread on the old Gambas Guru forum on how to find font size. But unfortunately, all that historical info died with a recent hard drive death.
sholzy

I'm wondering around lost in the past, not knowing where the present is.
User avatar
gbWilly
Posts: 68
Joined: Friday 23rd September 2016 11:41am
Location: Netherlands
Contact:

Re: GridView

Post by gbWilly »

Hi Sholzy,

You want to know the default gridview height.
Check the example attached on how to get it.

This default height could be used as a unit for multiplying in case of multiple lines.
The value is given according to the font type and size used.

Feel free to experiment, I used Sans Serif in the attached example.
GridTest-0.0.1.tar.gz
Example on grid height for different font sizes.
(12.28 KiB) Downloaded 380 times
Enjoy...
gbWilly
- Dutch translation for Gambas3
- Gambas wiki content contributer


... there is always a Catch if things go wrong!
User avatar
Got2BeFree
Posts: 91
Joined: Saturday 26th November 2016 2:52am
Location: Lost

Re: GridView

Post by Got2BeFree »

I already knew that doing this...
Print gridviewPages.Rows[iRow].Height
would tell me a row's height, but it escaped me at the time I posted the question above. It actually doesn't matter because it really wasn't what I was after.

What I was after was a formula to automagically set row height when word wrapping, which I found out how. After a little bit of reading, I realized Desktop.Scale returns half the height of the system font currently in use. So doing this...
gridviewPages.Rows[iRow].Height = Desktop.Scale * 2 * 2 + 8
would set a GridView's row height when word wrapping.
In the above code sample, if you take Desktop.Scale and double it (*2), then multiply that by the number of wrapped lines (*2), and then add some padding (+8), the result is near perfect padding (on my system). The padding remains constant with several wrapped lines.

@gbWilly...
Your code had no spelling errors and worked on the first run. :)
sholzy

I'm wondering around lost in the past, not knowing where the present is.
Post Reply