943,685 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 5038
  • C# RSS
Sep 26th, 2009
0

Label alignment issue

Expand Post »
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.
Last edited by ddanbe; Sep 26th, 2009 at 4:22 pm.
Similar Threads
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Sep 26th, 2009
0

Re: Label alignment issue

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.
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Sep 26th, 2009
0

Re: Label alignment issue

Yes looked at it but I did not read it all the way down. Thanks DdoubleD!
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Sep 26th, 2009
0

Re: Label alignment issue

Yeah I submitted a bug report to Microsoft and they confirmed it was a bug, but not important enough to fix.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 27th, 2009
1

Re: Label alignment issue

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:
c# Syntax (Toggle Plain Text)
  1. class LabelPanel : Panel
  2. {
  3. public LabelPanel()
  4. {
  5. text = string.Empty;
  6. font = new Font("Ariel", 8f);
  7. fontcolor = Color.Black;
  8. }
  9.  
  10. public string text { get; set; }
  11. public Font font { get; set; }
  12. public Color fontcolor { get; set; }
  13. //more or less properties here don't know yet
  14.  
  15. protected override void OnPaint(PaintEventArgs e)
  16. {
  17. base.OnPaint(e);
  18. Graphics G = e.Graphics;
  19. Brush brush = new SolidBrush(fontcolor);
  20. Rectangle rec = new Rectangle(new Point(0, 0), this.Size);
  21. StringFormat strfmt = new StringFormat();
  22. strfmt.Alignment = StringAlignment.Far; // means Right if RightLeft is true
  23. G.DrawString(text, font, brush, rec, strfmt);
  24. }
  25. }
And this is how I call it in a Form:
c# Syntax (Toggle Plain Text)
  1. LabelPanel LP = new LabelPanel();
  2. public Form1()
  3. {
  4. InitializeComponent();
  5. LP.Size = new Size(290, 20);
  6. this.Controls.Add(LP);
  7. }
  8.  
  9. private void SetLabelTexts(List<string> SL)
  10. {
  11. LP.text = SL[0];
  12. //Many more LabelPanels here
  13. Refresh();
  14. }

Works like a charm!
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Sep 27th, 2009
0

Re: Label alignment issue

LabelPanel huh...very interesting!
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Sep 27th, 2009
0

Re: Label alignment issue

That is a neat approach
Last edited by sknake; Sep 27th, 2009 at 6:10 pm.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 27th, 2009
0

Re: Label alignment issue

Thanks for your answers!
Reputation Points: 2023
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: How to programmatically create User Profile [C#]?
Next Thread in C# Forum Timeline: auto scaling in web application





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC