Discussion:
ListBox tabstops
(too old to reply)
Bob
2009-11-15 18:46:00 UTC
Permalink
I am trying to make a two column listbox by setting the tabstop to slightly
larger than the longest string in the first column. By using the graphics
context of the listbox and MeasureString(), I know that the longest string
in the first column is 93 pixels (for instance). However, I can't figure
out the correct value for the ListBox.IntegerCollection.Add() parameter. If
I use the pixel value, the spacing is always about 40% too large (no matter
what strings are in the first column).

Note: I am not using the MultiColumn function of the ListBox. The ListBox
is an a custom control and I am using the ListBox Format() method to
intercept the ListBox.Items.Add() calls to insert a tab ("\t") between the
two strings in the columns.

What is the relationship between pixels and the TabStop values?

Thanks
Bob
2009-11-15 18:57:30 UTC
Permalink
Also, I tried dividing this pixel value by the calculated average number of
pixels per character and using this as the tabStop value. The result is a
value that is way too small.

// Average character width.
Graphics gc = lstBox.CreateGraphics();
string testText = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
SizeF textSize = gc.MeasureString( testText, lstBox.Font);
float AvgPixelWidthPerChar = textSize.Width / (float)testText.Length;
Post by Bob
I am trying to make a two column listbox by setting the tabstop to slightly
larger than the longest string in the first column. By using the graphics
context of the listbox and MeasureString(), I know that the longest string
in the first column is 93 pixels (for instance). However, I can't figure
out the correct value for the ListBox.IntegerCollection.Add() parameter.
If I use the pixel value, the spacing is always about 40% too large (no
matter what strings are in the first column).
Note: I am not using the MultiColumn function of the ListBox. The ListBox
is an a custom control and I am using the ListBox Format() method to
intercept the ListBox.Items.Add() calls to insert a tab ("\t") between the
two strings in the columns.
What is the relationship between pixels and the TabStop values?
Thanks
Herfried K. Wagner [MVP]
2009-11-16 02:23:51 UTC
Permalink
The ListBox is an a custom control and I am using the ListBox Format()
method to intercept the ListBox.Items.Add() calls to insert a tab ("\t")
between the two strings in the columns.
What is the relationship between pixels and the TabStop values?
If you are using 'LB_SETTABSTOPS', the column widths are measured in DLU
(dialog box units). More information on DLU can be found here:

How to calculate dialog box units based on the current font in Visual C++
<URL:http://support.microsoft.com/kb/145994/en-us>
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Loading...