LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do you autosize column widths in a multicolumn listbox?

I know that it's possible to autosize row height, but I can't find anything relating to autosizing column widths in a multicolumn listbox. This makes what I'm doing difficult, because I want to be able to have dynamic content and won't know beforehand how wide to make the columns. Therefore, I need to do it programatically or find something already built in.

Thanks for your help!
0 Kudos
Message 1 of 17
(22,943 Views)
,

This can only be done programmatically. The easy part is setting the width. You would just go through each datamember, determine which is the longest, and set the width accordingly. You would do this for each column by selecting the active cell, and setting the column width. However; the real trick is knowing the width. If the user changes the font, you are in trouble. The widths are in pixels. I would recommend using a static font, measure the width of the widest character that is possible to use (build strings and display them, and use decorations or cursor movement to display the width). Do this by making strings of 10 or 100 characters. Then divide your width by the number of characters you use. Then, you would use this number as a multip
lier of the number of characters in the biggest cell in a column and set your width accordingly.

Please advise if you still have difficulty.

Good luck
Message 2 of 17
(22,941 Views)
There is a picture function "Get Text Rect.vi" that outputs the height and lenght in pixels of a text string given on input. You also specify font attributes. Assuming the same font attributes for all strings in the column, use it to calculate the largest string in the column and set the column width accordingly.


LabVIEW, C'est LabVIEW

Message 3 of 17
(22,941 Views)
Thanks! What about getting the font used for the cells and/or headers in the listbox? I don't see any properties that have a font on them the label and caption.
0 Kudos
Message 4 of 17
(22,941 Views)
This function worked great for an autosize column width in a table. By adding a width of 7 on each side of the text (left and right) to the 'right' inside the cluster, it will auto size if you use the largest value in the column for all rows. Thanks for the hints!
Kevin Shirey | CLA | Champion | Senior Project Engineer II | DMC
0 Kudos
Message 5 of 17
(22,942 Views)
JeanPierre  thanks for the rectangle trick i was scratching my head about this for a while,
0 Kudos
Message 6 of 17
(22,713 Views)
You mean you can autosize the column? Can you give us an example? Thanks. -Jim YouSmiley Mad
0 Kudos
Message 7 of 17
(22,692 Views)

I did an example along the lines of the suggestions above and it works pretty well. I'll attach it here. Note that this only works for 13pt font. If you want to scale this example to include larger font, then you can get that information from the cell height property, which does autoscale by default. The cell height is usually about 2 larger than the font size (i.e. 13pt font = 15 cell height, 24 pt font = 26 height).

This example is written in LV7.0. What would really be cool is if someone would write an XControl for LV8 that would do all this automatically and require no extra block diagram code! If only I had the time...

Jarrod S.
National Instruments
Message 8 of 17
(22,678 Views)
Thanks for your code. That works great. I changed multicolumn listbox to a table. I also added a function to autosize table column width according each individual cell font. The function only works with LabVIEW 8.
 
Jianmin (Jim) You, Ph.D.
Automation Consultant
0 Kudos
Message 9 of 17
(22,671 Views)
Thanks for the Table version.  I've modified your code to check the headers as well.  Note that the Active Cell Row property is set to -1 to select the header for a column.  The added extra space is still based on the non-header cells though instead of including checking the header font characteristics to see if they are larger than the non-header.  Probably not a big issue in many cases
 
Steve
 
Message 10 of 17
(22,116 Views)