user1697111 user1697111 - 7 months ago 141
Vb.net Question

Changing Winforms DataGridView ToolTip font

I have DataGridView in my program on which I compose a custom ToolTip under MouseMove, like this...

Private Sub dgv_dokument_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgv_dokument.MouseMove

Dim hit As DataGridView.HitTestInfo = dgv_dokument.HitTest(e.X, e.Y)

If hit.Type = DataGridViewHitTestType.Cell Then
If hit.ColumnIndex >= 0 AndAlso hit.RowIndex >= 0 Then
Dim s As Integer = Convert.ToInt32(dgv_dokument.Item(co.GetColIndex(dgv_dokument, "myNumCol"), hit.RowIndex).Value)
Dim ttText As String = ""
If s > 0 Then
Dim sb As New StringBuilder
get_data(s, sb)
ttText = sb.ToString
End If

dgv_dokument.Item(hit.ColumnIndex, hit.RowIndex).ToolTipText = ttText
Exit Sub
End If
End If
End Sub


Under get_data(s, sb) I filled StringBuilder with strings in mean of "column" formatted data, like this...

sb.Append(code.ToString.PadLeft(5) + " ")
sb.Append(name.Trim.PadRight(27) + " ")
sb.Append(meas.Trim.PadRight(3) + " ")
sb.Append(qty.ToString("N2").PadLeft(10) + " ")
sb.Append(price.ToString("N2").PadLeft(12))
sb.Append(Environment.NewLine)


That all works good except that I would like to see my tooltip column-aligned. That may be done with using proportional font in ToolTop.

Can I somehow (and how) determine other than default font for just DataGridView's ToolTip's? For example "Courier New".

TaW TaW
Answer

You need to OwnerDraw the ToolTip.

Here is an example:

First set the property OwnerDraw = true for the ToolTip.

Then code its Draw event, maybe like this:

private void toolTip1_Draw(object sender, DrawToolTipEventArgs e)
{
    e.DrawBackground();
    e.DrawBorder();
    using (Font f = new Font("Consolas", 8f))
        e.Graphics.DrawString(e.ToolTipText, f, SystemBrushes.ControlText, e.Bounds);
}

enter image description here

Note that if your Fonts get smaller than the default (as mine) some extra free space will show, which is pretty much OK.

But if it is larger the Bounds may need to adapt, which can get somewhat tricky. You would have to pad the text with enough room to the end and bottom to enforce a sufficient Bounds size.

For this you would have to measure the space needed for the larger Font and add spaces until the Bounds have grown enough. No need to remove them as overdrawing spaces will not be a problem; but sticking with the original e.Font certainly is a lot easier.

You still may find the need to add a few spaces: Your fixed font will, on average, take a little more space so the Bounds provided may turn out to be a little tight anyway..

Comments