Hi all,
Left of a TextBox I have a Label and the text changes during a run so I want the text to be right aligned. So I used the TextAlign property of a Label and set it to MiddleRight. It does not work. Looked for an answer, could not find one. Don't no what I am overlooking here:-/
Any reply is more than welcome.

Recommended Answers

All 7 Replies

Check out this thread:
http://www.daniweb.com/forums/thread209516.html

I believe there is some confusion in the beginning of the thread concerning what right-to-left means, but go down toward the bottom and also sknake attached a project that demonstrates the label expansion.

Yes looked at it but I did not read it all the way down. Thanks DdoubleD!

Yeah I submitted a bug report to Microsoft and they confirmed it was a bug, but not important enough to fix. :(

Hi DdoubleD and Scott,

Think I found another solution to the problem, I forgot about Label all the way and derived a class from Panel:

class LabelPanel : Panel
    {
        public LabelPanel()
        {
            text = string.Empty;
            font = new Font("Ariel", 8f);
            fontcolor = Color.Black;
        }

        public string text { get; set; }
        public Font font { get; set; }
        public Color fontcolor { get; set; }
       //more or less properties here don't know yet

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics G = e.Graphics;
            Brush brush = new SolidBrush(fontcolor);
            Rectangle rec = new Rectangle(new Point(0, 0), this.Size);
            StringFormat strfmt = new StringFormat();
            strfmt.Alignment = StringAlignment.Far; // means Right if RightLeft is true
            G.DrawString(text, font, brush, rec, strfmt);
        }
    }

And this is how I call it in a Form:

LabelPanel LP = new LabelPanel();
        public Form1()
        {
            InitializeComponent();            
            LP.Size = new Size(290, 20);
            this.Controls.Add(LP);
        }

        private void SetLabelTexts(List<string> SL)
        {
            LP.text = SL[0];
            //Many more LabelPanels here
            Refresh();
        }

Works like a charm!

commented: Nice solution :) +2

LabelPanel huh...very interesting!:P

That is a neat approach :)

Thanks for your answers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.