10-26-2016 11:45 AM
I have a 2D string array ,the elements are random but with a maximum lenght.I want to print the array to a string box but ,unable to get a output of well-alignment.
Here is the code.
I want to output aligned like this.Although maybe it is not able to align it precisely,but more or less is OK.
Or is there a way to display a table in a string box?
Any ideas ,thanks!
10-26-2016 12:34 PM
Does it have to be a string textbox?
What if you just wrote it to a table indicator? You can make the gridlines transparent if you want to.
The problem with the strings not aligning is that with most fonts, every character is a different width. Try makin your font for that string a monospaced font like Courier. In your image, it looks like the label "spreadsheet string" is not the default font but something monospaced.
10-26-2016 02:38 PM
You can (and probably should) follow what RavensFan recommends, especially using a monospace font for your string indicator, such as Courier.
However, if you really want to use the default font, you can use "Get Text Rect.vi" to determine the size in pixels that your text will produce. You can then use the widths to determine how many spaces to add to subsequent text that are in each column.
Here's code that'll get you started. This snippet gets the column widths in pixels for the first row:
This snippet uses the column widths for subsequent rows to determine how many spaces to pad for each "cell" of your 2-D array:
10-29-2016 12:41 AM
Thanks for your kind reply.
Actually in my question on the first floor,you can see that I already pad every element to be the same length of 20,this way doesn't work as expected either.
10-29-2016 10:03 AM
This problem has been present "from the beginning", and the solutions are very awkward and difficult. The suggestion to use "Get Text Rect" is awkward, and is not accurate (somewhere I have a post showing how to make a text box exactly fit a single line of text, regardless of Font, Size, or Attribute, but it is also messy).
When you write to a Spreadsheet String, columns are (by default) separated by <tab> characters and lines by <eol>, which in many text viewers makes nice columns as the display driver interprets <tab> and <eol> -- some even let you set "tab stops". LabVIEW doesn't handle <tab>, so you have to pad with spaces. How many? Well, that depends on the width of your characters and the width of the space, hence the suggestion to use a monospaced Font where everything is "one unit of screen space".
The only other suggestion I can envision is for NI to provide an additional String Array Indicator that has "invisible" row and column separators. Maybe this is worth a suggestion on the Ideas Page (I'll look to see if this has been proposed, and if not, I'll propose it ...).
Bob Schor
10-29-2016 10:22 AM
I just posted "Provide String Array Indicator with "invisible" row and column dividers" in the LabVIEW Idea Exchange. If you think this is something NI could do (which would definitely solve the question raised in this post), find the Idea and Kudo it. Ideas are chosen for implementation based on the Kudo Count (something like a Cootie Count I remember from elementary school, but more positive).
Bob Schor
10-29-2016 02:07 PM
@avater wrote:Thanks for your kind reply.
Actually in my question on the first floor,you can see that I already pad every element to be the same length of 20,this way doesn't work as expected either.
But you still haven't changed the font of that control to be a monospaced font such as Courier. Your solution will never work with a font that each character can take up a different amount of space.
10-29-2016 02:26 PM
I tend to use the .NET rich text box, handles tabs nicely (and much more).
10-29-2016 02:39 PM
Cool! Until NI decides that my "Idea" is worthy, your method seems like it would work, and right now! Can you post either a demo or a link to a "how to" article? [I've done one or two things with .NET, but nothing recently, and I don't really know what I'm doing ...].
Bob Schor
10-29-2016 02:46 PM
Hi aveter,
If you use a constant width font, the solution is fairly simple.
steve