Bohn
Bohn

Reputation: 26919

Is Graphics.MeasureString dependant on the control?

In a picture like the one below I get the "same" numbers for this pieces of code, simply one of them is for the texteditor and the other one is for a button..IN the picture I see that the font size of the the things I have types in the cells of the table are bigger than the font size of the button, but why these two codes return the same number?

    Graphics graphics =  ctlTEEditor.CreateGraphics();
    int width = (int)graphics.MeasureString("FitsToooo", ctlTEEditor.Font).Width;
    MessageBox.Show(width.ToString());

    Graphics graphics2 = button1.CreateGraphics();
    int width2 = (int)graphics2.MeasureString("FitsToooo", button1.Font).Width;
    MessageBox.Show(width2.ToString());

enter image description here

Upvotes: 1

Views: 108

Answers (2)

Bohn
Bohn

Reputation: 26919

Ok, the issue is that in the code when I say "ctlTEEditor.Font", this is the font of the control itself and not the font that is used inside the table cells.

Upvotes: 0

Jason Coyne
Jason Coyne

Reputation: 6636

MeasureString is not aware of any word wrapping that the control may choose to implement.

Upvotes: 1

Related Questions