开发者

How can I tell when the Text of a System.Windows.Forms.GroupBox wraps to the next line?

I'm creating a GroupBox at runtime and setting its Text property. Usually, the text is only on one line, but sometimes it wraps. The problem is that the controls contained in the GroupBox cover up the GroupBox's text.

What I'd like to do is determine if and when the text wraps. Specifically, I'd like to determine how much extra height the wrapped text takes up as compared to a single line. That way, I can reposition the GroupBox's controls and adjust its height.

Initially, I thought I'd do this by calling the GroupBox's CreateGraphics() method, and using the Graphics to measure the string. Something like this:

private void SetGroupBoxText(GroupBox grp, string text)
{
    const int somePadding = 10;

    Graphics g = grp.CreateGraphics();
    SizeF textSize = g.Measure开发者_如何学GoString(text, grp.Font);
    if (textSize.Width > (grp.Width - somePadding))
    {
        // Adjust height, etc.
    }
}

The problem is that the size generated by g.MeasureString(text, grp.Font) doesn't seem to be accurate. I determined that it wasn't accurate by putting enough of a single character to cause a wrap, then measuring the resulting string.

For example, it took 86 pipes (|) to until a wrap happened. When I measured that string, its width was ~253. And it took 16 capital W's to force a wrap - its string had a width of ~164. These were the two extremes that I tested. My GroupBox's width was 189. (a's took 29 and had a width of ~180, O's took 22 and had a width of ~189)

Does anyone have any ideas? (hacks, WinAPI, etc. are welcome solutions)


This code was derived from the Reference Source, it should be very close. It returns the number of pixels added to the text height when it displays more than one line of text:

    public static int GetGroupboxTextHeightExtra(GroupBox box) {
        TextFormatFlags flags = TextFormatFlags.Default | TextFormatFlags.TextBoxControl | TextFormatFlags.WordBreak | TextFormatFlags.PreserveGraphicsTranslateTransform | TextFormatFlags.PreserveGraphicsClipping;
        Rectangle rc = new Rectangle(0, 0, box.Width - 2 * 7, box.Height);
        Size size;
        using (Graphics gr = Application.OpenForms[0].CreateGraphics()) {
            size = TextRenderer.MeasureText(gr, box.Text, box.Font, rc.Size, flags);
        }
        return size.Height - box.Font.Height;
    }


I know this doesn't directly solve your problem, but you could try setting the minimum size of the group box to some number you knew the text wouldn't wrap.

In most cases you shouldn't have a lot of text on the heading of a group box. Think about adding a label below the group box that goes into more detail. Remember, a group box is meant to "group" similar controls. So the text on the actual group box should be minimal and mimic a title.


Why not use a layout manager for your problem? A layout manager layout controls on a container in a way that frees you from doing those calculations. You can find layout managers from vendors like DevXpress and Syncfusion. I think that there are layout manager in the .Net Framework too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜