469,647 Members | 1,669 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,647 developers. It's quick & easy.

Center a string with a trailing space

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
Nov 15 '05 #1
3 4317
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" <an*******@discussions.microsoft.com> wrote in message
news:E7**********************************@microsof t.com...
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?
thanks,
Mark Urish

Nov 15 '05 #2
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" <an*******@discussions.microsoft.com> wrote in message
news:50**********************************@microsof t.com...
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" <an*******@discussions.microsoft.com> wrote in message
news:E7**********************************@microsof t.com...
> 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? >> thanks,

> Mark Urish


Nov 15 '05 #3
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" <an*******@discussions.microsoft.com> wrote in message
news:E7**********************************@microsof t.com...
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?
thanks,
Mark Urish

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Dolphe Tilly | last post: by
1 post views Thread by Konstantinos Pachopoulos | last post: by
6 posts views Thread by =?Utf-8?B?R2Vvcmdl?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.