Results 1 to 5 of 5

Thread: [2.0] An accurate formula to measure the length of a string (drawing)

  1. #1

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    [2.0] An accurate formula to measure the length of a string (drawing)

    I am painting on my buttons, however the painting erases the text on them, so I am trying to use 'DrawString' to write it back on them...

    However, I need to accurately measure the length of what the string will be when it is drawn to the button... I also can't specifically position the text on the button because I have an array of multiple buttons, with different text...

    I currently have:
    Code:
    e.Graphics.DrawString(button.Text, new Font(button.Font.OriginalFontName, button.Font.Size), Brushes.Red, new Point(0, 0));
    I've tried font.Height works perfectly for the height of the string, but font.Width * button.Text.Length, does not work properly?

    Any idea's?

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] An accurate formula to measure the length of a string (drawing)

    From a previous thread of yours I assume that this code is being executed inside a class derived from Button. In that case there's no need to do what you're doing. Just override the OnPaintBackground method instead of OnPaint.

    That said, for future reference you have the Graphics.MeasureString and .MeasureCharacterRanges methods. I believe that MeasureCharacterRanges is more reliable in some situations.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] An accurate formula to measure the length of a string (drawing)

    Hmmm... maybe I lied about that. Just tried OnPaintBackground and the drawing didn't appear. I'll dig around a bit more and see if I'm missing something.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] An accurate formula to measure the length of a string (drawing)

    Yeah, it looks like I was lying. I looked around a bit and I don't think OnPaintBackground is for what I thought it was for. Ah well, my own GDI+ experience went up a little over b*gger all as a result, so it was worthwhile.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2.0] An accurate formula to measure the length of a string (drawing)

    Thanks alot

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width