GridView

Post

Posted
Rating:
#1 (In Topic #275)
Avatar
Administrator
sholzy is in the usergroup ‘unknown’
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
Gambas One Site Director

To report bugs in the Gambas IDE:
Official Gambas Bug Tracker
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Guru
cogier is in the usergroup ‘Guru’
I have been playing with this today. Have a look at the attached code and see if you can make sense of it.

<IMG src="http://www.cogier.com/gambas/WrapText.png"> </IMG>

Attachment
Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Administrator
sholzy is in the usergroup ‘unknown’
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!

Image

(Click to enlarge)


sholzy
Gambas One Site Director

To report bugs in the Gambas IDE:
Official Gambas Bug Tracker
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Administrator
sholzy is in the usergroup ‘unknown’
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.

Code (gambas)

  1.             For Each MGlobal.hResData
  2.                 If Len(MGlobal.hResData!page_desc) > 45 Then   ' set length for wrap
  3.                     gridviewPages.Rows[iRow].Height = 50   ' set row height for wrapped text
  4.                     sText = MGlobal.hResData!page_desc
  5.                     WordWrap()
  6.                 Else
  7.                     sWordWrap = MGlobal.hResData!page_desc
  8.                 Endif
  9.                 gridviewPages[iRow, 0].Text = MGlobal.hResData!id
  10.                 gridviewPages[iRow, 1].Text = Str$(DateAdd(CDate("1/1/1970"), gb.Second, Global.hResData!page_rec_date))   ' convert from unix timestamp
  11.                 gridviewPages[iRow, 2].Text = MGlobal.hResData!page_name
  12.                 gridviewPages[iRow, 3].Text = MGlobal.hResData!sql_table_name
  13.                 gridviewPages[iRow, 4].Text = sWordWrap
  14.                 sWordWrap = ""
  15.                 Inc iRow
  16.             Next
  17.  

This routine does the wrapping. It finds the best spot to wrap so no words are split.

Code (gambas)

  1. Public Sub WordWrap()
  2.  
  3.     Dim siPos As Short = 45    ' wrap length
  4.  
  5.     While Mid$(sText, siPos, 1) <> " "       ' find white space to make clean wrap
  6.         Dec siPos
  7.     Wend
  8.  
  9.     sWordWrap = Mid$(sText, 1, siPos) & Chr(10) & Mid$(sText, siPos + 1)      ' wrap
  10.  
  11.  

What the results look like….
Image

(Click to enlarge)


sholzy
Gambas One Site Director

To report bugs in the Gambas IDE:
Official Gambas Bug Tracker
Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’
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  ;)

Code (gambas)

  1. Public Function WordWrap(TextToWrap As String, LengthToWrap As Integer) As String
  2.  
  3.     Dim iCount As Integer
  4.    
  5.     iCount = LengthToWrap
  6.     While Mid$(TextToWrap, iCount, 1) <> " "        'find first white space backward from LenghtToWrap to make the split
  7.         Dec iCount
  8.     Wend
  9.  
  10.     Return Subst("&1\n&2", Mid$(TextToWrap, 1, iCount), Mid$(TextToWrap, iCount + 1))
  11.  
  12.  

Usage in you example would be:

Code (gambas)

  1. For Each MGlobal.hResData
  2.     If Len(MGlobal.hResData!page_desc) > 45 Then   ' set length for wrap
  3.         gridviewPages.Rows[iRow].Height = 50   ' set row height for wrapped text
  4.         sText = MGlobal.hResData!page_desc
  5.         sWordWrap = WordWrap(sText, 45)
  6.     Else
  7.         sWordWrap = MGlobal.hResData!page_desc
  8.     Endif
  9.     gridviewPages[iRow, 0].Text = MGlobal.hResData!id
  10.     gridviewPages[iRow, 1].Text = Str$(DateAdd(CDate("1/1/1970"), gb.Second, Global.hResData!page_rec_date))   ' convert from unix timestamp
  11.     gridviewPages[iRow, 2].Text = MGlobal.hResData!page_name
  12.     gridviewPages[iRow, 3].Text = MGlobal.hResData!sql_table_name
  13.     gridviewPages[iRow, 4].Text = sWordWrap
  14.     sWordWrap = ""
  15.     Inc iRow
  16.  

Want to split in 3 lines say at 45 and 90?
Use it recursively

Code (gambas)

  1. sWordWrap = WordWrap(WordWrap(sText, 90), 45)
  2.  

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
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories


… there is always a Catch if things go wrong!
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Administrator
sholzy is in the usergroup ‘unknown’

gbWilly said

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
Gambas One Site Director

To report bugs in the Gambas IDE:
Official Gambas Bug Tracker
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Administrator
sholzy is in the usergroup ‘unknown’
 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
Gambas One Site Director

To report bugs in the Gambas IDE:
Official Gambas Bug Tracker
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Administrator
gbWilly is in the usergroup ‘unknown’
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.

Attachment

Example on grid height for different font sizes.


Enjoy…

gbWilly
- Gambas Dutch translator
- Gambas wiki content contributor
- Gambas debian/ubuntu package recipe contributor
- GambOS, a distro for learning Gambas and more…
- Gambas3 Debian/Ubuntu repositories


… there is always a Catch if things go wrong!
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Administrator
sholzy is in the usergroup ‘unknown’
I already knew that doing this…

Code (gambas)

  1. 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…

Code (gambas)

  1. 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
Gambas One Site Director

To report bugs in the Gambas IDE:
Official Gambas Bug Tracker
Online now: No Back to the top
1 guest and 0 members have just viewed this.