How to give a textbox a fixed width of 17,5 cm?

Posted by Natrium on Stack Overflow See other posts from Stack Overflow or by Natrium
Published on 2010-05-18T06:32:32Z Indexed on 2010/05/18 6:41 UTC
Read the original article Hit count: 262

Filed under:
|
|

I have an application with a textbox, and the width of the textbox on the screen must always be 17,5 centimeters on the screen of the user.

This is what I tried so far:

const double centimeter = 17.5; // the width I need
const double inches = centimeter * 0.393700787; // convert centimeter to inches

float dpi = GetDpiX(); // get the dpi. 96 in my case.

var pixels = dpi*inches; // this should give me the amount of pixels
textbox1.Width = Convert.ToInt32(pixels); // set it. Done.



private float GetDpiX()
{
    floar returnValue;
    Graphics graphics = CreateGraphics();
    returnValue = graphics.DpiX;
    graphics.Dispose(); // don’t forget to release the unnecessary resources
    return returnValue;
}

But this gives me different sizes with different resolutions.

It gives me 13 cm with 1680 x 1050 and 19,5 cm with 1024 x 768.

What am I doing wrong?

© Stack Overflow or respective owner

Related posts about winforms

Related posts about c#