Member Avatar for Member #1094511

Hello everyone,

was looking to see what was availble in listview1.columns.add....
and found this

 ListView1.Columns.Add("ID", FontDialog, HorizontalAlignment.Center)

how would i use this to change the font in listview columns.?
the rest of the code...

      ListView1.Columns.Add("Weekday", 100, HorizontalAlignment.Left)
        ListView1.Columns.Add("Fuel Date", 80, HorizontalAlignment.Left)
        ListView1.Columns.Add("Fuel Time", 80, HorizontalAlignment.Left)
        ListView1.Columns.Add("Location", 60, HorizontalAlignment.Left)
        ListView1.Columns.Add("Pump", 50, HorizontalAlignment.Center)
        ListView1.Columns.Add("Total Liters", 80, HorizontalAlignment.Left)
        ListView1.Columns.Add("Total Dollars", 80, HorizontalAlignment.Left)

Thanks for reading. :-)

Dani AI

Generated

The constructor shown in the first post is a misread: there is no Columns.Add overload that accepts a FontDialog or a Font for a ColumnHeader. Column headers are painted by the native header control, so changing their font is not the same as setting item fonts. As suggested, FontDialog is the right way to let a user pick a font — then apply that Font to items or subitems, or use a different technique to change headers.

A simple approach for items/subitems (applies the chosen Font to the whole list and to existing rows):

Dim fd As New FontDialog()
If fd.ShowDialog() = DialogResult.OK Then
    Dim chosen As Font = fd.Font
    ListView1.BeginUpdate()
    ListView1.Font = chosen                ' default for future items
    For Each it As ListViewItem In ListView1.Items
        it.Font = chosen                   ' force existing items to use it
        For i As Integer = 0 To it.SubItems.Count - 1
            it.SubItems(i).Font = chosen   ' optional: per-subitem fonts
        Next
    Next
    ListView1.EndUpdate()
End If

To change the column header font, owner-draw is the safest cross-version method: enable OwnerDraw = True and handle DrawColumnHeader (and, in Details view, also DrawItem/DrawSubItem). Example header-draw snippet:

ListView1.OwnerDraw = True
Private headerFont As New Font("Segoe UI", 9, FontStyle.Bold)

Private Sub ListView1_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) _
    Handles ListView1.DrawColumnHeader
    e.DrawBackground()
    TextRenderer.DrawText(e.Graphics, e.Header.Text, headerFont, e.Bounds, SystemColors.ControlText, TextFormatFlags.VerticalCenter Or TextFormatFlags.Left)
End Sub

An alternative is sending WM_SETFONT to the header control with P/Invoke (LVM_GETHEADER + WM_SETFONT). That works but requires careful HFONT lifetime management (DeleteObject) to avoid GDI leaks, so owner-draw is usually preferred. Also remember to call BeginUpdate/EndUpdate when changing many items, and to refresh the control after font changes.

Hi

I haven't seen that constructor before when adding columns and did a check and still can't see it. Can you provide a screen shot or more info on this?

In terms of using a Font however, you can apply a Font style when adding items to the ListView. For example:

Dim fd As New FontDialog
Dim newFont As Font

If fd.ShowDialog = Windows.Forms.DialogResult.OK Then newFont = fd.Font

For i As Integer = 1 To 10
    ListView1.Items.Add("Value " & i.ToString).Font = newFont
Next

HTH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.