|
Is there a way to prevent text wrapping when assigning long strings to the Button.Text (Control.Text) property ? I know how to enforce word wrapping but I haven't found any mechanisms to prevent it. I tried searching for a solution but almost everything I find is related to web applications and not to property assignment in winforms. At first I thought Button.AutoEllipsis would help out but it didn't change anything (where's the sense behind that?). I remember there was some way to calculate the length of any string (given the font details) so I could enlarge the relevant buttons but I really should try to avoid this.
| | Amnu Tuesday, April 28, 2009 10:42 AM | Hi Amnu,
A solution to your question is to draw the text on the Button by yourself. To do this,handle the Paint event of the Button. The following is a sample:
void button1_Paint(object sender, PaintEventArgs e) { TextRenderer.DrawText(e.Graphics, "this is a long text", this.button1.Font, this.button1.ClientRectangle, TextFormatFlags.VerticalCenter); }
Hope this helps. If you have any question, please feel free to let me know.
Sincerely, Linda Liu - Marked As Answer byLinda LiuMSFT, ModeratorMonday, May 11, 2009 3:44 AM
-
| | Linda Liu Tuesday, May 05, 2009 10:10 AM | No, Button doesn't let you tweak this, it tries hard to show as much of the text as possible. AutoEllipsis takes effect when the string is too long to fit the button, even after wrapping. The solution is pretty straight-forward: just set the Text property to a short string.
Hans Passant. | | nobugz Tuesday, April 28, 2009 11:31 AM | Well, that's not possible all the time. I am not assigning these values by hand. These values are set depending on environment variables, user actions and so forth. Basically, these are set automatically - either read from a db or calculated depending on the environment. Kind of strange it's not possible to change this ... Anyway, thank you for your help! | | Amnu Tuesday, April 28, 2009 12:15 PM | Hi Amnu,
A solution to your question is to draw the text on the Button by yourself. To do this,handle the Paint event of the Button. The following is a sample:
void button1_Paint(object sender, PaintEventArgs e) { TextRenderer.DrawText(e.Graphics, "this is a long text", this.button1.Font, this.button1.ClientRectangle, TextFormatFlags.VerticalCenter); }
Hope this helps. If you have any question, please feel free to let me know.
Sincerely, Linda Liu - Marked As Answer byLinda LiuMSFT, ModeratorMonday, May 11, 2009 3:44 AM
-
| | Linda Liu Tuesday, May 05, 2009 10:10 AM |
|