开发者

Is there a way to right-align a numeric column in a ListBox

开发者 https://www.devze.com 2023-01-20 11:46 出处:网络
We have a standard Access listbox with multiple columns. Is there a way to have int开发者_运维知识库eger or currency columns right aligned?No.The closest I\'ve seen is JustiCombo which is a database c

We have a standard Access listbox with multiple columns. Is there a way to have int开发者_运维知识库eger or currency columns right aligned?


No. The closest I've seen is JustiCombo which is a database containing functions to center and right justify data for List and Combo Boxes. It can use proportional fonts by analysing the font attributes and character widths. It too stuffs spaces in the front of the fields to make the data appear centre/right justified. It did the best it could but you could see a few jaggies. But then maybe I was being too critical.


Depending on your query the alignment of a column will be inherited from the underlying table. So, go to the table, select the column, center/right/left align it, and then your textbox should have the same alignment. This won't work for calculated fields, but it should for most others.


As far as I'm aware, not in the traditional sense, no. I believe there are some third-party products that might be able to do this, but there's no native ColumnAlignment properties for listboxes in any versions I've used (haven't used Access 2007, though, for what it's worth).

Depending on how you are loading the listbox, you could use a fixed-width font (such as Courier) and left-pad your numbers with the appropriate number of spaces, to emulate right-alignment. It's not ideal, but it may be worth a shot.


  1. Convert the listbox to combobox
  2. Make the combobox that you converted right alignment
  3. Convert it again to listbox


In VB it is:

Format(Format("10000", "0.00%"), "@@@@@@@@@@") where the number of "@"s are the width of the field in which to right justify the string.

In VBA you can use:

xFormat(Format("10000", "0.00%"), "@@@@@@@@@@") where

Function xFormat(ByVal s, ByVal width As String) As String

    Dim temp As String
    Dim deltaL As Integer

    deltaL = Len(width) - Len(s)
    If deltaL > 0 Then
        temp = Space(deltaL) & s
    Else
        temp = s
    End If
    xFormat = temp
End Function


The way I solved it was by doing the following:

Go to File -> options -> Client settings -> General alignment

Change the settings to Text Mode, and it worked for me.


I use the following:

Public Function Paddy_Boy(ByVal s As String) As String

    Const FillChar  As Integer = 160  ' the key to success; this is an unpaddable space
    Const MAX_CHARS As Integer = 12   ' the max # of chars in the target column; you could pass this as an arg, but obviously it depends on the data. and i can only get it to work nicely using Courier New.

    Dim i   As Integer
    Dim l   As Integer
    Dim Z   As String

    Z = "Paddy_Boy"

    Paddy_Boy = s

    On Error GoTo Err_Sub

    l = Len(s)

    i = MAX_CHARS
    While (i > l)
        Paddy_Boy = Chr(FillChar) & Paddy_Boy
        i = i - 1
    Wend

Exit_Sub:
    Exit Function

Err_Sub:
    MsgBox Err.Number & ": " & Err.Description, vbCritical, Z
    GoTo Exit_Sub

End Function
0

精彩评论

暂无评论...
验证码 换一张
取 消