Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 856045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:10:38+00:00 2026-05-15T08:10:38+00:00

I have panel that I have customized. I use it to display text. But

  • 0

I have panel that I have customized. I use it to display text. But sometimes that text is too long and wraps to the next line. Is there some way I can auto resize the panel to show all the text?

I am using C# and Visual Studio 2008 and the compact framework.


Here is the code I am wanting to adjust the size for:
(Note: HintBox is my own class that inherits from panel. So I can modify it as needed.)

public void DataItemClicked(ShipmentData shipmentData)
{
    // Setup the HintBox
    if (_dataItemHintBox == null)
        _dataItemHintBox = HintBox.GetHintBox(ShipmentForm.AsAnObjectThatCanOwn(),
                                             _dataShipSelectedPoint,
                                             new Size(135, 50), shipmentData.LongDesc,
                                             Color.LightSteelBlue);


    _dataItemHintBox.Location = new Point(_dataShipSelectedPoint.X - 100,
                                          _dataShipSelectedPoint.Y - 50);
    _dataItemHintBox.MessageText = shipmentData.LongDesc;
    // It would be nice to set the size right here
    _dataItemHintBox.Size = _dataItemHintBox.MethodToResizeTheHeightToShowTheWholeString()
    _dataItemHintBox.Show();

}

I am going to give the answer to Will Marcouiller because his code example was the closest to what I needed (and looks like it will work). However, this is what I think I will use:

public static class CFMeasureString
{
    private struct Rect
    {
        public readonly int Left, Top, Right, Bottom;
        public Rect(Rectangle r)
        {
            this.Left = r.Left;
            this.Top = r.Top;
            this.Bottom = r.Bottom;
            this.Right = r.Right;
        }
    }

    [DllImport("coredll.dll")]
    static extern int DrawText(IntPtr hdc, string lpStr, int nCount, 
                               ref Rect lpRect, int wFormat);
    private const int DT_CALCRECT = 0x00000400;
    private const int DT_WORDBREAK = 0x00000010;
    private const int DT_EDITCONTROL = 0x00002000;

    static public Size MeasureString(this Graphics gr, string text, Rectangle rect, 
                                     bool textboxControl)
    {
        Rect bounds = new Rect(rect);
        IntPtr hdc = gr.GetHdc();
        int flags = DT_CALCRECT | DT_WORDBREAK;
        if (textboxControl) flags |= DT_EDITCONTROL;
        DrawText(hdc, text, text.Length, ref bounds, flags);
        gr.ReleaseHdc(hdc);
        return new Size(bounds.Right - bounds.Left, bounds.Bottom - bounds.Top + 
                        (textboxControl ? 6 : 0));
    }
}

This uses the os level call to draw text. By P-Invoking it I can get the functionality I need (multi line wrapping). Note that this method does not include any margins. Just the actual space taken up by the text.

I did not write this code. I got it from http://www.mobilepractices.com/2007/12/multi-line-graphicsmeasurestring.html. That blog post had my exact problem and this fix. (Though I did make a minor tweak to make it a extension method.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:10:40+00:00Added an answer on May 15, 2026 at 8:10 am

    You could use the Graphics.MeasureString() method.

    With a code sample of your text assignment onto your panel, I could perhaps provide a code sample using the MeasureString() method, if you need it.

    I have no way to know whether the Graphics.MeasureString() method is part of the Compact Framework, as it is not said on the page I linked.

    EDIT #1

    Here’s a link where I answered to another text-size related question, while I look for writing a sample for you. =)

    EDIT #2

    Here’s another link related to your question. (Next edit is the sample code. =P)

    EDIT #3

    public void DataItemClicked(ShipmentData shipmentData) { 
        // Setup the HintBox 
        if (_dataItemHintBox == null) 
            _dataItemHintBox = HintBox.GetHintBox(ShipmentForm.AsAnObjectThatCanOwn(), 
                                                 _dataShipSelectedPoint, 
                                                 new Size(135, 50), shipmentData.LongDesc, 
                                                 Color.LightSteelBlue); 
    
        // Beginning to measure the size of the string shipmentData.LongDesc here.
    
        // Assuming that the initial font size should be 30pt.
        Single fontSize = 30.0F;
        Font f = new Font("fontFamily", fontSize, FontStyle.Regular);
    
        // The Panel.CreateGraphics method provides the instance of Graphics object 
        // that shall be used to compare the string size against.
        using (Graphics g = _dataItemHintBox.CreateGraphics()) {
            while (g.MeasureString(shipmentData.LongDesc, f).Width > _dataItemHintBox.Size.Width - 5) {
                --fontSize;
                f = new Font("fontFamily", fontSize, FontStyle.Regular);
            }
        }
    
        // Font property inherited from Panel control.
        _dataItemHintBox.Font = f;
    
        // End of font resizing to fit the HintBox panel.
    
        _dataItemHintBox.Location = new Point(_dataShipSelectedPoint.X - 100, 
                                              _dataShipSelectedPoint.Y - 50); 
        _dataItemHintBox.MessageText = shipmentData.LongDesc; 
        // It would be nice to set the size right here 
        _dataItemHintBox.Size = _dataItemHintBox.MethodToResizeTheHeightToShowTheWholeString() 
        _dataItemHintBox.Show(); 
    } 
    

    Disclaimer: This code has not been tested and is off the top of my head. Some changes might be obligatory in order for you to test it. This provides a guideline to achieve what you seem to want to accomplish. There might be a better way to do this, but I know this one works. Well, the algorithm works, as you can see in my other answers.

    Instead of the line:

    SizeF fontSize = 30.0F;
    

    You could as well do the following:

    var fontSize = _dataItemHintBox.Font.Size;
    

    Why is this?

    Because Font.Size property is readonly. So, you need to create a new instance of the System.Drawing.Font class each time the Font.Size shall change.

    In your comparison, instead of having the line:

    while (g.MeasureString(shipmentData.LongDesc, f)...)
    

    you could also have:

    while (g.MeasureString(shipmentData.LongDesc, _dataItemHintBox.Font)...)
    

    This would nullify the need for a second Font class instance, that is f.

    Please feel free to post feedbacks as I could change my sample to fit your reality upon the feedbacks received, so that it better helps you. =)

    I hope this helps! =)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a panel that is hooked up to a ModalPopupExtender but it can
I have a panel sitting in a div, and I want to use that
I have a Panel that I'm using to display information about a location. I
I have panel that is using group layout to organize some label. I want
I have a panel that draws alot of things. To make drawing effecient I
I have update panel that content check box, textbox, 3 DropDownList with CascadingDropDown extender.
I have a panel that is docked to the right side of a windows
I have a Panel that I'm setting visible=true explicitly. The debugger passes over that
I have an Panel control that I need to maintain position across postbacks. I
i have a flow panel that i'm adding extra items to it at runtime

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.