Skip to content

How find Pixels

Abdul Asif

Asked Nov 21, 2007, 5:40 AM EST1 answer
Share
LinkedInX

Hi All,

I am adding a string value to listbox control. I need to set the width of listbox in runtime base on how many pixels string need it to display it within listbox. Idea is to set a width of listbox exactly the number of pixels required by string value. So, I can have perfect width each time base on string value.
Any idea please help.

Thanks,

Asif.

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    Hi Asif,

     You can solve your problem by using following code:

    Code
    Graphics g = lbTest.CreateGraphics();
    Size sz = TextRenderer.MeasureText(g , lbTest.SelectedItem.ToString() ,  lbTest.Font);
    lbTest.Size = new Size(sz.Width, lbTest.Height);

    Here lbTest is the name of the ListBox, which width to be set.

    0