Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9295281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:34:09+00:00 2026-06-18T21:34:09+00:00

When I set a label’s ContentAlignment property to MiddleRight, I expect it to align

  • 0

When I set a label’s ContentAlignment property to MiddleRight, I expect it to align to the right within the panel on which I place it. Instead, it stubbornly stays put aligned left. Why is this, and how can I fix it? Here’s my code:

private void CreateNewLabel(int YPos, string DisplayStr, ContentAlignment contentAlign,
                            int FontSize)
{
    Label lbl = new Label();
    //lbl.Left = DEFAULT_XPOS;
    lbl.Font = new Font(lbl.Font.Name, FontSize, lbl.Font.Style);
    lbl.Top = YPos;
    lbl.Text = DisplayStr;
    lbl.TextAlign = contentAlign;
    lbl.AutoSize = true;
    panelFauxLabel.Controls.Add(lbl);
}

Everything is working EXCEPT horizontal placement. I don’t want to set the Left property, because I want certain alignments to take up all the “right side” space they can; calculating the XPos is possible, I’m sure, but also quite complicated, I’m even more sure.

UPDATE

Olivier’s answer worked just fine. The code is now:

    private void CreateNewLabel(int YPos, string DisplayStr, ContentAlignment contentAlign, int FontSize)
    {
        Label lbl = new Label();
        lbl.Left = DEFAULT_XPOS;
        lbl.Font = new Font(lbl.Font.Name, FontSize, lbl.Font.Style);
        lbl.Top = YPos;
        lbl.Text = DisplayStr;
        lbl.TextAlign = contentAlign;
        if (contentAlign.Equals(ContentAlignment.MiddleRight))
        {
            lbl.Anchor = AnchorStyles.Right;
        }
        else // there is no AnchorStyles.Center or AnchorStyles.Middle
        {
            lbl.Anchor = AnchorStyles.Left;
        }
        lbl.AutoSize = false;
        lbl.Width = panelFauxLabel.Width;
        panelFauxLabel.Controls.Add(lbl);
    }

UPDATE 2

I had to add a tweak to the height of the label to prevent large font sizes from being chopped off at the knees, so to speak:

    // This factor was just a guess, but it seems to work pretty well
    double down = Math.Round(FontSize*1.5);
    lbl.Height = Convert.ToInt32(down);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T21:34:10+00:00Added an answer on June 18, 2026 at 9:34 pm

    TextAlign aligns the text within the label, not the label within the panel. Consider using the Anchor property in order to align the label to the right edge of the panel.


    UPDATE

    Here’s how this can be done: Make the label the same width than the panel, anchor the label to the top, left and right

    lbl.TextAlign = contentAlign;
    lbl.AutoSize = false;
    lbl.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
    lbl.Top = YPos;
    lbl.Left = 0;
    lbl.Width = panelFauxLabel.ClientSize.Width; // ClientSize takes account of scroll
                                                 // bars, borders and padding.
    panelFauxLabel.Controls.Add(lbl);
    

    Now the label resizes together with the panel and the text aligns correctly within the label. Set lbl.AutoSize to false in order to make the label size independent of the text length.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I set label's border which is dynamically generated (Not from Interface Builder)?
I am having problem to set label at right corner in ToolBar . Hear
I want to set the background color of a label using HSV instead of
I have a custom chart here I have set label property as labels: {
Why first trace prints out not second one whereas I set label in property
I want to set a label in Tkinter using my countdown timer function. Right
Can I set label visibility in Flex ? newUser = new LinkButton(); newUser.label =
I am using client bundle in gwt.Using client bundle set image on label,open pdf
I have excluded a field like this in order to set it's label. class
If I set a sufficiently long label for any kind of form element, or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.