Showing results for 
Search instead for 
Did you mean: 

How to well-aligned print a 2D string array to a textbox?

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.

well alignment


Or is there a way to display a table in a string box?

Any ideas ,thanks!

0 Kudos
Message 1 of 12

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.

Message 2 of 12

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" 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:

Well Aligned Text First Row.png


This snippet uses the column widths for subsequent rows to determine how many spaces to pad for each "cell" of your 2-D array:

Well Aligned Text Remaining Rows.png

Feel free. Contact me for anything more,

You too can be LabVIEW Awesome!
0 Kudos
Message 3 of 12

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.


0 Kudos
Message 4 of 12

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

0 Kudos
Message 5 of 12

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

0 Kudos
Message 6 of 12

@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.

0 Kudos
Message 7 of 12

I tend to use the .NET rich text box, handles tabs nicely (and much more).

0 Kudos
Message 8 of 12

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

0 Kudos
Message 9 of 12

Hi aveter,

If you use a constant width font, the solution is fairly simple.

align 2D array.png


Help the forum when you get help. Click the "Solution?" icon on the reply that answers your
question. Give "Kudos" to replies that help.
0 Kudos
Message 10 of 12