*Experts* Nerseus Posted September 16, 2003 *Experts* Posted September 16, 2003 I have a few places where I need to display text that will normally be an inch or so wide (relative of course). For some rows the text is extra long (maybe 3 or 4 inches). It's a common problem and so far I've used the common answer: use a textbox that looks like a label (no border, readonly, cursor=Default, TabStop=False). But it's kinda hokey and takes up a lot more resources to draw a textbox when I just need a read-only label. Is there, in fact, a way to get a label to have No Wrap enabled like a non-MultiLine Textbox? I've got a few inherited label controls that do custom drawing. I know I could write a NoWrapLabel control, but it seems like a lot of work when a property might work. If I *did* write the inherited label, I believe I'd have to have the following code (I don't know of any way to simplify this into a few short calls): (Also - why or HOW does the BorderStyle property still work - I have NO code below to draw it, and I'm not calling base.OnPaint) public class NoWrapLabel : Label { protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; StringFormat sf = StringFormat.GenericTypographic; sf.FormatFlags |= StringFormatFlags.NoWrap; // Set the horizontal alignment switch(this.TextAlign) { case ContentAlignment.BottomCenter: case ContentAlignment.TopCenter: case ContentAlignment.MiddleCenter: sf.Alignment = StringAlignment.Center; break; case ContentAlignment.BottomLeft: case ContentAlignment.TopLeft: case ContentAlignment.MiddleLeft: sf.Alignment = StringAlignment.Near; break; case ContentAlignment.BottomRight: case ContentAlignment.TopRight: case ContentAlignment.MiddleRight: sf.Alignment = StringAlignment.Far; break; } // Set the vertical alignment switch(this.TextAlign) { case ContentAlignment.BottomCenter: case ContentAlignment.BottomLeft: case ContentAlignment.BottomRight: sf.LineAlignment = StringAlignment.Far; break; case ContentAlignment.MiddleCenter: case ContentAlignment.MiddleLeft: case ContentAlignment.MiddleRight: sf.LineAlignment = StringAlignment.Center; break; case ContentAlignment.TopCenter: case ContentAlignment.TopLeft: case ContentAlignment.TopRight: sf.LineAlignment = StringAlignment.Near; break; } // Clear the area of the control e.Graphics.DrawRectangle(new Pen(this.BackColor), this.ClientRectangle); // Draw the text e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), this.ClientRectangle, sf); } } Thanks, Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
*Experts* Volte Posted September 16, 2003 *Experts* Posted September 16, 2003 If you set a label's size to AutoSize it won't wrap, but the label will not be a static size. Quote
*Experts* Nerseus Posted September 17, 2003 Author *Experts* Posted September 17, 2003 Yes, but I need a maximum size as well. Say the text is normally "Hello World", so I make the label 100 pixels width. There are too many other controls in the same area to make it any bigger and it works 90% of the time. But one time the text is "The quick brown fox jumped over the lazy dog biscuit" and now my label wraps, looking pretty ugly. I could "cheat" and make the height *just* tall enough to only show one line, but it's a pain to get things aligned right. If I use a textbox, the extra text is there, but you have to scroll to see it. That works, but it's not ideal. I think maybe I'll just go with the inherited label, but I wish there was a way to simplify the alignment logic. And I'm still confused as to why the border draws when I'm not doing it, and I'm supposed to be the only one drawing the control (that's my impression anyway). -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.