Our World Statistics Day conversations have been a great reminder of how much statistics can inform our lives. Do you have an example of how statistics has made a difference in your life? Share your story with the Community!
Choose Language Hide Translation Bar
Highlighted
Level II

## Determining Size of a Text Box before drawing it.

Hi,

is it possible to get the Size of an Text Box before the Text Box is actually drawn?

I generate dynamically a window and need to generate spacers to allign objects. Therefore I need the Size of several Text Box elements, before I actually draw them.

Cheers

4 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
Super User

## Re: Determining Size of a Text Box before drawing it.

The crude method - create a window that contains your content - then send messages to get the sizes - then delete the window (hopefully before it actually gets rendered) - then use the sizes to render the window more elegantly - I have to use this technique sometimes to get consistent output on both Windows and Mac.

-Dave
Highlighted
Level II

## Re: Determining Size of a Text Box before drawing it.

Hi David,

basically that is what I did at the end, only that I did not delete the window but merely send the correct size messages to the text boxes with  "<< Set Width"

!BEWARE!

everybody who tries this: After drawing your initial window, you have to wait a little bit before you can take the measurements with "<< Get Size". A wait(0.0001) was sufficiant for me. If you do not do it strange things happen. (That took me half a day to figure out).

!BEWARE!

Highlighted
Super User

## Re: Determining Size of a Text Box before drawing it.

Here's the function I use to determine text box height.  The hide(1) argument prevents it from being displayed.

``````get_height = function({_text, _pixels},
{Default Local},

pname = "get_height";

if (is missing(_text),
// then
pix_height = 0;
,
// else if
_text == "",
pix_height = 0;
,
// else we've got some text to work with
tb = text box(_text, set wrap(_pixels), set width(_pixels), hide(1));
tmp_win = new window("test", tb);
// Get size info
pix_height = (tb << get size)[2];

tmp_win << close window;
);

pix_height;
);	// end get_height``````

Highlighted
Super User

## Re: Determining Size of a Text Box before drawing it.

You might want to try Wait(0).  This basically says: wait until the tasks have been completed.

-Dave
8 REPLIES 8
Highlighted
Staff

## Re: Determining Size of a Text Box before drawing it.

Did you try 'ShowProperties()' with the text box to see what messages it understands? I guess the size required depends on what text you want to put in it (and the font used). The example below shows that JMP will respect any width that you have set, but is forced to override the height when the box is drawn.

``````tb = TextBox();
tb << setWidth(20);
tb << setHeight(30);
Print(tb << getWidth, tb << getHeight, "\!n");

tb << setText("How now brown cow");
Print(tb << getWidth, tb << getHeight, "\!n");

NewWindow("Test", tb);
Print(tb << getWidth, tb << getHeight, "\!n");``````

Highlighted
Staff

## Re: Determining Size of a Text Box before drawing it.

Have you exhausted the existing display boxes that are specifically responsible for alignment and layout? Their automatic behavior might reduce or eliminate your need to 'roll your own.'

Learn it once, use it forever!
Highlighted
Level II

## Re: Determining Size of a Text Box before drawing it.

Yup, it is possible,
but I wanted to keep some gui elements which all interact with the same data table together, to make the code easier to read.

So it would be nice to have a message giving the dimensions in advance.
Chrismas is coming soon, so I put a it on my wishlist. ;)
Highlighted
Super User

## Re: Determining Size of a Text Box before drawing it.

The crude method - create a window that contains your content - then send messages to get the sizes - then delete the window (hopefully before it actually gets rendered) - then use the sizes to render the window more elegantly - I have to use this technique sometimes to get consistent output on both Windows and Mac.

-Dave
Highlighted
Level II

## Re: Determining Size of a Text Box before drawing it.

Hi David,

basically that is what I did at the end, only that I did not delete the window but merely send the correct size messages to the text boxes with  "<< Set Width"

!BEWARE!

everybody who tries this: After drawing your initial window, you have to wait a little bit before you can take the measurements with "<< Get Size". A wait(0.0001) was sufficiant for me. If you do not do it strange things happen. (That took me half a day to figure out).

!BEWARE!

Highlighted
Super User

## Re: Determining Size of a Text Box before drawing it.

You might want to try Wait(0).  This basically says: wait until the tasks have been completed.

-Dave
Highlighted
Level II

## Re: Determining Size of a Text Box before drawing it.

Yup, it is possible, but I wanted to keep some gui elements which all interact with the same data table together, to make the code easier to read. So it would be nice to have a message giving the dimensions in advance. Chrismas is coming soon, so I put a it on my wishlist. ;)
Highlighted
Super User

## Re: Determining Size of a Text Box before drawing it.

Here's the function I use to determine text box height.  The hide(1) argument prevents it from being displayed.

``````get_height = function({_text, _pixels},
{Default Local},

pname = "get_height";

if (is missing(_text),
// then
pix_height = 0;
,
// else if
_text == "",
pix_height = 0;
,
// else we've got some text to work with
tb = text box(_text, set wrap(_pixels), set width(_pixels), hide(1));
tmp_win = new window("test", tb);
// Get size info
pix_height = (tb << get size)[2];

tmp_win << close window;
);

pix_height;
);	// end get_height``````

Article Labels

There are no labels assigned to this post.