point to pixel conversion

K

Kirk Graves

I have an html table that I am filling with data from the database. when I
put the text in the table, I only display the number of characters that will
fit based on the size of the cell.

I know the width of the cell, I can get the length of the text, but the
cell.width is in pixels, while the text length is measured based on points
(font size).

anyone have any ideas how to get this to work? Here is the entire class I
am using.


Thank you in advance

Kirk
-------------------
Public Class TextCalc
Public Shared f As Drawing.Font
Public Shared g As Drawing.Graphics
Public Shared Ready As Boolean = False

Friend Shared Sub SetUpCalculator(ByVal FontFamily As String, ByVal
FontUnit As Web.UI.WebControls.Unit)
f = New Drawing.Font(FontFamily, Convert.ToSingle(FontUnit.Value),
CType(FontUnit.Type, Drawing.GraphicsUnit))

g = Drawing.Graphics.FromImage(New Drawing.Bitmap(100, 100))
Ready = True
End Sub
Friend Shared Function GetDisplayText(ByVal ItemText As String, ByVal
cellWidth As Single) As String
Dim l As Drawing.SizeF
Dim x As Int16
Dim s As String

l = g.MeasureString(ItemText, f)
If l.Width <= cellWidth Then ' this is the line that causes the
problem,
'l.Width is based on
points, cellWidth is based on pixels
Return ItemText
End If

For x = 1 To ItemText.Length
s = ItemText.Substring(0, x) & ".."

l = g.MeasureString(s, f)

If l.Width >= cellWidth Then
Exit For
End If
Next
Return s
End Function
End Class
 
B

bruce barker

this approach will not work, because you are calculating the pixels based on
the server's font size, not the client's. only the client knows the actual
pixels per point. this is because the client may actually use a different
font or size then the server.

this is also why grid layout does not work in the general case (switch to
large fonts and see result).

you can use a div in the table cell and have it truncate the data by
specifying the overflow style.

-- bruce (sqlwork.com)
 
K

Kirk Graves

thanks bruce.

I will try it.

bruce barker said:
this approach will not work, because you are calculating the pixels based on
the server's font size, not the client's. only the client knows the actual
pixels per point. this is because the client may actually use a different
font or size then the server.

this is also why grid layout does not work in the general case (switch to
large fonts and see result).

you can use a div in the table cell and have it truncate the data by
specifying the overflow style.

-- bruce (sqlwork.com)

when based
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top