Center a string with a trailing space

G

Guest

I want to center-align the text in 2 Label controls, and I want the 1st char of their text to line up
------------------------------------------------------------------------------
label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

label1.Text = "IDLE"; //4 char strin
label2.Text = "RUN ";//4 char string,
//but trailing space ignored and it is centered as thoug
//it has only 3 chars
------------------------------------------------------------------------------

Is there a way to make the Label control center "Run " (with a trialing space) as though it is 4 chars long

thanks
Mark Urish
 
J

Josh [MS.Com]

Forgive me if I'm missing somthing, but what you seem to be asking for is
Alignment.LeftCenter... :)

Can't you align the labels so that the start on the same X?

Josh
Microsoft.com Tools


Mark Urish said:
I want to center-align the text in 2 Label controls, and I want the 1st char of their text to line up:
-------------------------------------------------------------------------- -----
label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

label1.Text = "IDLE"; //4 char string
label2.Text = "RUN ";//4 char string,
//but trailing space ignored and it is centered as though
//it has only 3 chars.
-------------------------------------------------------------------------- -----

Is there a way to make the Label control center "Run " (with a trialing
space) as though it is 4 chars long?
 
J

Josh [MS.Com]

Humm,

Well, I've been playing around and it dosn't look like the Label really
wants to do that... but you already knew that...

You could build your own label control that dosn't trim the traling spaces;

Derive from Panel, override OnPaint and use the Graphics object to measure
and draw your string in the right place... but that might be over kill...

I also tried putting each label on it's own panel, but couldn't get the
docking or anchoring scheme that worked right...

Dunno doode... :)

Josh
Microsoft.com Tools

Mark Urish said:
Thanks for the quick response.

Yes, LeftCenter would work except that I also have
BorderStyle=FixedSingle. I want to the text to be roughly centered inside
the border. I realize that I could do this by using LeftCenter and
prepending spaces so that the text is approximately centered, but the Label
is anchored in a user-control and may be resized. I don't want to have to
compute the number of spaces necessary to keep the text centereed in the
frame. That's what MiddleCenter is supposed to do :).
Any further suggestions are much appreciated!
Mark Urish

----- Josh [MS.Com] wrote: -----

Forgive me if I'm missing somthing, but what you seem to be asking for is
Alignment.LeftCenter... :)

Can't you align the labels so that the start on the same X?

Josh
Microsoft.com Tools


Mark Urish said:
I want to center-align the text in 2 Label controls, and I want the
1st
char of their text to line up:




--------------------------------------------------------------------------
-----
label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
label2.Text = "RUN ";//4 char string,
//but trailing space ignored and it
is
centered as though
//it has only 3 chars.




trialing
space) as though it is 4 chars long?
Mark Urish
 
G

Gary Morris

Hi,

OK, after reading this thread and the replies, I've got a
thought on it, though I haven't tried it. What I would do in
this situation is to (if possible) figure out what the center
would be based on the width of the control, set the
alignment to left, and add the appropriate amount of
spaces to the beginning of the text in both controls. I did
try adding the spaces, and this seems to work.

Now, you might not get it *exactly* in the center every
time, but with some fiddling I'm sure you could find a way
to calculate it so that each one had the same amount of
leading spaces when resized or whatever. That is, as
long as BOTH controls are aligned with one another. If
that's not a feasible alternative, then I'm sorry, but right
now that is the only way I can see to handle it correctly.

Beyond that, It's anybody's guess!

Mark Urish said:
I want to center-align the text in 2 Label controls, and I want the 1st char of their text to line up:
-------------------------------------------------------------------------- -----
label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;

label1.Text = "IDLE"; //4 char string
label2.Text = "RUN ";//4 char string,
//but trailing space ignored and it is centered as though
//it has only 3 chars.
-------------------------------------------------------------------------- -----

Is there a way to make the Label control center "Run " (with a trialing
space) as though it is 4 chars long?
 

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