An easy way to align text in your ListBox or ComboBox.
Here's an easy way to get some text aligned in a ListBox or ComboBox.
The only thing needed is a PictureBox control (named PixAlign) placed on your form.
You can of course set it visible = false since it is only needed for text alignment process.
VB Code:
'Usage
Call AlignText("Hello World", List1, vbCenter)
Call AlignText("Hello World", Combo1, vbRightJustify)'<-- modify the sub accordingly!
VB Code:
'inside a module
Public Sub AlignText(ByVal txt As String, ByVal lstBox As ListBox, ByVal Alignment As AlignmentConstants)
Dim i As Long, tmptxt As String
tmptxt = txt
With PixAlign
'Needed so the PictureBox
'is set to the required values first.
.ScaleMode = .Parent.ScaleMode
.Width = lstBox.Width
.FontName = lstBox.FontName
.FontSize = lstBox.FontSize
.FontBold = lstBox.FontBold
Do While .TextWidth(tmptxt$) < .Width
'calculates the leftover spaces.
tmptxt$ = tmptxt$ & space(1)
i = i + 1
Loop
End With
i = i - 2
Select Case Alignment
'align the text according to alignment constants.
Case vbLeftJustify
lstBox.AddItem txt
Case vbRightJustify
lstBox.AddItem space(i& - 1) & txt$
Case vbCenter
lstBox.AddItem space(i& \ 2) & txt$
End Select
End Sub
Cheers! :wave: