Dan
Posts: 4529
Joined: Wed Apr 24, 2013 3:51 pm

Aligning Columns in ListBox, ComboBox or CheckListView

Hello everyone,

Recently, I had to display a list of running Windows services inside a custom Advanced Installer dialog.

My goal was simple:
  • Show the service name in the first column (fixed width, 25 characters).
  • Append a second column with "Owner:LocalHost" (later updated to show the real Log On As account)
My First Attempt

In PowerShell, it’s easy to align columns using PadRight():

Code: Select all

$COL = 25
$items = Get-Service | Where-Object Status -eq 'Running' |
    ForEach-Object { $_.Name.PadRight($COL) + 'Owner:localHost' }

In the PowerShell console (which uses a monospaced font), this looked perfect.
powershell-console.png
powershell-console.png (97.18 KiB) Viewed 588 times

But once I fed the results into my Advanced Installer ComboBox/ListBox, everything broke — the second column was jagged and misaligned.
advinst-dlg.png
advinst-dlg.png (61.67 KiB) Viewed 588 times

The Hidden Problem

The issue wasn’t with PowerShell at all. The problem was fonts:

The PowerShell console uses a monospaced font (every character is the same width).

Advanced Installer dialogs use Segoe UI, a proportional font (characters have different widths).
  • "MMMMM" is much wider than "iiiii".
  • So "PadRight(25)" ensures character count, but not visual alignment.
That’s why the column alignment failed, no matter how many spaces (or even *) I added.

The Fix

The cleanest solution turned out to be very simple:
Switch the control’s Text Style to a monospaced font (Consolas or Courier New) in Advanced Installer.
define-textstyle.png
define-textstyle.png (58.66 KiB) Viewed 588 times
Reference the new text style for my controls:
control-textstyle.png
control-textstyle.png (71.66 KiB) Viewed 588 times

At runtime, the columns get properly alligned:
textstyle-applied.png
textstyle-applied.png (256.96 KiB) Viewed 561 times

Takeaway

If you’re working with ComboBoxes/ListBoxes in Advanced Installer and need aligned columns:
  • Don’t rely on PadRight() alone, Windows Installer measures text in dialog units, not raw characters or pixels.
  • With proportional fonts (Segoe UI), dialog units don’t map cleanly to character positions, so columns won’t align.
  • The simplest fix is to switch the control to a monospaced font (Consolas, Courier New). That way, each dialog unit equals one character width, and your columns line up perfectly.
Sometimes the bug isn’t in your script , it’s in the way Windows Installer implementation.

Sample project is attached, feel free to download and run.
Attachments
GetRunningServicesSample V2.aip
(31.49 KiB) Downloaded 27 times
Dan Ghiorghita - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Sample Projects”