Am 09.07.2010 12:54, schrieb Mr. X.:
It's a datagrid, which represents rows from database.
and it is dynamic grid that can show not a specific table, but the one I
request.
Because I cannot figure out what is the exact size in pixels of each row,
I need to calculate it approximately for a specific letter multiply by the
maximum size * 80%.
(I have read this long time ago).
Would the DataGridView's AutoResizeColumns method save you some work?
Or
AutoResizeColumn(ByVal columnIndex As Integer, ByVal autoSizeColumnMode As System.Windows.Forms.DataGridViewAutoSizeColumnMode)
Besides that...:
A while ago, I wrote a FontViewer. It also displayed the max and avg
width of a char. Maybe there's an implementation in the Framework meanwhile,
but I've used an API call for this. Here's an excerpt of the original
code:
Imports System.Runtime.InteropServices
'...
'in the class:
Private Declare Function DeleteObject Lib "gdi32.dll" (ByVal objectHandle As IntPtr) As Boolean
Private Declare Function SelectObject Lib "gdi32.dll" (ByVal hdc As IntPtr, ByVal hObject As IntPtr) As IntPtr
Private Declare Auto Function GetTextMetrics Lib "gdi32" (ByVal hdc As IntPtr, ByRef lpMetrics As TEXTMETRIC) As Boolean
'...
'in a method:
Dim TM As TEXTMETRIC
Dim TMSuccess As Boolean
Using g = pic.CreateGraphics '***** use your datagridview here instead of pic *****
Dim hdc = g.GetHdc
Try
Dim hFont = f.ToHfont
Try
Dim oldobject = SelectObject(hdc, hFont)
Try
TM = Nothing 'Dummy-Zuweisung
TMSuccess = GetTextMetrics(hdc, TM)
Finally
SelectObject(hdc, oldobject)
End Try
Finally
If Not DeleteObject(hFont) Then
Throw New System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error, "DeleteObject failed")
End If
End Try
Finally
g.ReleaseHdc(hdc)
End Try
End Using
If TMSuccess Then
'***** TM.tmMaxCharWidth may be what you're looking for *****
txtFontInfo.Text = TM.tmMaxCharWidth & "/" & TM.tmAveCharWidth & "/" & TM.tmHeight
End If
End Using