473,466 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Alignment problem when TextAlign = Middle in labels

When labels are vertically stacked on top of one another and have
their TextAlign property set to MiddleRight, the text is not properly
right-aligned.

You can reproduce this problem by creating a blank form with three
private members (label1, label2, and label3) and adding the following
code to the form's constructor:

// BEGIN CODE

label1 = new Label();
label2 = new Label();
label3 = new Label();

// Use a fixed size, X position and content alignment for every label.
//
Size size = new Size(256, 16);
int xPos = 104;
Content content = ContentAlignment.MiddleRight;

label1.Location = new Point(expos, 24);
label1.Size = size;
label1.Text = ":Lisa Kudrow:";
label1.TextAlign = contentAlignment;

label2.Location = new Point(xPos, 40);
label2.Size = size;
label2.Text = ":Jennifer Aniston:";
label2.TextAlign = contentAlignment;

label3.Location = new Point(xPos, 56);
label3.Size = size;
label3.Text = ":Courtney Cox:";
label3.TextAlign = contentAlignment;

Controls.Add(label1);
Controls.Add(label2);
Controls.Add(label3);

// END CODE

When the form is opened, the rightmost colons in the labels are not
vertically aligned. They are close, but the colon in ":Lisa Kudrow:"
is slightly further right than the colons is "Jennifer Aniston" and
"Courtney Cox".

Note that when the contentAlignment is set to
ContentAlignment.MiddleLeft, the leftmost colons are perfectly
vertically aligned. Therefore, the problem appears to be with the
"MiddleRight" text alignment only.

The only way I can get text to be vertically aligned on the right is
to set each label's RightToLeft property to RightToLeft.Yes and
TextAlign property to MiddleLeft. This solution has obvious
drawbacks.

Is there a better solution?
Nov 15 '05 #1
1 7467
On 16 Feb 2004 09:51:09 -0800, ab****@hotmail.com (Kyle Blaney) wrote:
When labels are vertically stacked on top of one another and have
their TextAlign property set to MiddleRight, the text is not properly
right-aligned.


I haven't used MiddleRight alignment myself but I had similar problems
with Label.AutoSize -- the bottom line in a multi-line label would be
cut off because the label was slightly too small.

What fixed the problem for me was using Graphics.MeasureString and
then manually setting the label size. (Ironically, the MSDN Library
claims that Label.AutoSize uses MeasureString internally...)

Maybe you could use the same method -- use differently sized labels to
exactly fit the text, as determined by MeasureString, then manually
right-align all the labels?

Here's the code fragment for getting the correct text size, assuming
the current object is the control that hosts your labels:

string text = /* text to display */;

Size textSize = Size.Empty;
using (Graphics paint = CreateGraphics())
textSize = Size.Ceiling(paint.MeasureString(text, Font));

Label textLabel = new Label();
textLabel.Text = text;
textLabel.UseMnemonic = false;
textLabel.Location = /* whatever */;
textLabel.Size = textSize;
Controls.Add(textLabel);
--
http://www.kynosarges.de
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jason Xu | last post by:
Hello, I am working on printing richtext with C# by sending EM_FORMATRANGE message to the underlying richedit control. I can set the text format by sending EM_SETPARAFORMAT message now. However,...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
5
by: Norm Krumpe | last post by:
When typing the name of a label, followed by a period: Label1. The dropdown list of properties and methods ALWAYS defaults to highlighting the "TextAlign" property. What's worse, if I type:...
1
by: Jay Hamilton | last post by:
Hello, I am running into an invalid address alignment error on an HPUX box when I attempt to lookup a value in a STL map. The argument being passed in is a double, which is accessed from a...
3
by: RathiSarov | last post by:
Hi Friends, I have a master page and a child page with some amount of server controls. The issue is when we resize the page, the controls are not center aligned. I tried adding aligning the...
0
by: richerTextBox | last post by:
I'm writing an application that prints cassette and cd labels. One major problem I'm running into is that I'm getting varied results on the alignment when data is sent to the printer. Shouldn't I be...
1
by: jleblanc | last post by:
I need to be able to reference the main window, from a child of a child window but the middle child is closed. Here is the scenario... Main window = a new window = b (child window of a) new...
6
by: Francois Grieu | last post by:
Hello, I'm asking myself all kind of questions on allocating an array of struct with proper alignment. Is the following code oorrect ? I'm most interested by the statement t =...
2
by: uamusa | last post by:
I am Dynamically generating a proposal(report) in MS Word. By default the Paragraph Alignment is "Left". For the First 6 Paragraphs I set the Alignment to "Center", and then when attempting to...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.