Page 1 of 1
GridView
Posted: Sunday 26th May 2019 6:03am
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.
Re: GridView
Posted: Sunday 26th May 2019 3:34pm
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.
Re: GridView
Posted: Sunday 26th May 2019 4:52pm
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 (56.51 KiB) Viewed 9540 times
Re: GridView
Posted: Monday 27th May 2019 2:51am
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 (30.45 KiB) Viewed 9534 times
Re: GridView
Posted: Monday 27th May 2019 3:37pm
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
Re: GridView
Posted: Monday 27th May 2019 10:26pm
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.
Re: GridView
Posted: Monday 27th May 2019 10:56pm
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.
Re: GridView
Posted: Wednesday 29th May 2019 12:06am
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.
Enjoy...
Re: GridView
Posted: Friday 31st May 2019 6:06am
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.