Userform radiobutton text length & on-the-fly placement? (Word97)

K

KR

I have an existing userform with a bunch of radiobuttons. The text for each
radiobbutton is pulled from a file, and I originally set it up so that each
radiobutton is in a fixed location with three lines of text, and that was
sufficient to hold all the text _and_ show all the items (the number of
items is flexible as well).

In my most recent source file, which I don't have any control or influence
over, some of the items were longer than my three lines, and there were a
few cases where the total number of items was greater than my maximum based
on my (fullscreen) userform size- in those cases, ususally I had several
individual items on the page that didn't take the whole three lines, so I
had wasted space above and not enough room at the bottom for all my items.

So now I want to figure out how to measure how many lines I need for each
radiobutton on the fly, and adjust the XY location of each radiobutton from
the one above to take advantage of any open space so I can fit more
radiobuttons on the page. This would also give me what I need to deal with
the items that are too long, as the next radiobutton would just be shifted
down instead of up.

So here is my question; in Word97 (and forward) is there any reliable way to
determine how many rows the text of a radiobutton will be (or already is)
based on the width of the radiobutton? If not, what is the best workaround
available?

Thanks!
Keith
 
J

Jay Freedman

Hi Keith,

The question of the number of lines is probably solvable if you can
make certain assumptions, but I think you're asking the wrong
question.

First some discussion: In full VB and other programming languages, you
can get a handle to a graphic workspace in memory, and then call a
function that takes the workspace, the string, and the font
information as parameters and returns the pixel size of the area that
would be needed to draw the string. That functionality isn't available
in VBA. But VBA does give you some tools to work with.

On your userform, you can set the .AutoSize property of each option
button to True. When you assign text to the .Caption property, the
..Height property automatically adjusts to show the whole text (but the
..Width property stays at whatever value you originally set).
~~~~~~~~~~
If you really want the number of lines, on the assumption that you
kept the default font size of 8 pt, then a good approximation is

NumberOfLines = Int(OptionButton1.Height / 10)

This gave me the right result for captions of 1 to 10 lines, but
beyond 10 it predicted one or two lines more than the correct number.
~~~~~~~~~~
But you don't need the number of lines, because the .Height property
tells you everything you need to know. You can calculate the proper
position of the second button by adding the .Top and .Height of the
first button, plus a little padding. In the UserForm_Activate
procedure, set

OptionButton2.Top = OptionButton1.Top + OptionButton1.Height + 5

and then

OptionButton3.Top = OptionButton2.Top + OptionButton2.Height + 5

and so on. You can do this in a For..Next loop by constructing the
control names with string expressions, like this:

For nBtn = 2 To 5
Me.Controls("OptionButton" & nBtn).Top = _
Me.Controls("OptionButton" & (nBtn - 1)).Top + _
Me.Controls("OptionButton" & (nBtn - 1)).Height + 5
Next

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
K

KR

Jay-

Thank you for your time and response. Looping using the height property
looks like a great way to go- I'll give it a try!

Thanks,
Keith
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top