+ Reply to Thread
Results 1 to 3 of 3

Listbox textalign

Hybrid View

Guest Listbox textalign 01-26-2005, 05:06 PM
Guest Re: Listbox textalign 01-26-2005, 05:06 PM
Guest Re: Listbox textalign 01-26-2005, 08:06 PM
  1. #1
    R4gtl
    Guest

    Listbox textalign

    It is possible to give a different textalignement for some columns of a
    listbox?
    I want that some columns have a textalignement left and some columns have a
    textalignement right. I don't know if it is possible. I have to put the
    command in a macro.


  2. #2
    Jake Marx
    Guest

    Re: Listbox textalign

    Hi R4gtl,

    It's not possible without jumping through some hoops. If you use a
    fixed-width font like Courier New, then you can pad the columns with leading
    spaces to achieve right-alignment.

    Otherwise, you could look into using the ListView control if it's available
    to you.

    --
    Regards,

    Jake Marx
    MS MVP - Excel
    www.longhead.com

    [please keep replies in the newsgroup - email address unmonitored]


    R4gtl wrote:
    > It is possible to give a different textalignement for some columns of
    > a listbox?
    > I want that some columns have a textalignement left and some columns
    > have a textalignement right. I don't know if it is possible. I have
    > to put the command in a macro.



  3. #3
    Andy Pope
    Guest

    Re: Listbox textalign

    Hi,

    This code should give you an start point. It may need tweaking for
    different fonts and possible the scrollbars.

    Create a userform and add a listbox, label and 2 commandbuttons.
    Paste this code into the userform code module.

    It uses the label to approximate the padding required to right align the
    text. If you specify column widths then make sure they are all specified.

    '-----------------Start Code
    Sub AlignListColumn(LBox As MSForms.ListBox, _
    WhichColumn As Integer, AlignRight As Boolean)
    '
    ' Align specified column
    '
    Dim vntColWidths As Variant
    Dim sngWidth As Single
    Dim strTemp As String
    Dim labTester As MSForms.Label
    Dim intItem As Integer

    If Not AlignRight Then
    ' Normal left alignment
    With LBox
    For intItem = 0 To .ListCount - 1
    .List(intItem, WhichColumn) = _
    Trim(.List(intItem, WhichColumn))
    Next
    End With
    Else
    ' Right align specified column
    If WhichColumn > LBox.ColumnCount Then Exit Sub

    Set labTester = Me.Label1
    labTester.WordWrap = False
    With LBox
    If .ColumnWidths <> "" Then
    vntColWidths = Split(.ColumnWidths, ";")
    ' fudge for gap between cols
    sngWidth = Val(vntColWidths(1)) - 5
    Else
    ' fudge for gap between cols
    sngWidth = (.Width / .ColumnCount) - _
    ((.ColumnCount - 1) * 5)
    End If
    If sngWidth <= 0 Then Exit Sub
    For intItem = 0 To .ListCount - 1
    strTemp = Trim(.List(intItem, WhichColumn))
    labTester.AutoSize = False
    labTester.Width = .Width
    labTester.Caption = strTemp
    labTester.AutoSize = True
    Do While labTester.Width <= sngWidth
    labTester.Caption = " " & labTester.Caption
    Loop
    .List(intItem, WhichColumn) = labTester.Caption
    Next
    End With
    End If
    End Sub

    Private Sub CommandButton1_Click()

    AlignListColumn ListBox1, 0, True
    AlignListColumn ListBox1, 1, False
    AlignListColumn ListBox1, 2, True

    End Sub

    Private Sub CommandButton2_Click()

    AlignListColumn ListBox1, 0, False
    AlignListColumn ListBox1, 1, True
    AlignListColumn ListBox1, 2, False


    End Sub
    Private Sub UserForm_Initialize()

    With ListBox1
    .AddItem "One"
    .AddItem "Two"
    .AddItem "Three"
    .AddItem "Four"
    .AddItem "Five"

    .List(0, 1) = "One"
    .List(1, 1) = "Two"
    .List(2, 1) = "Three"
    .List(3, 1) = "Four"
    .List(4, 1) = "Five"

    .List(0, 2) = "One"
    .List(1, 2) = "Two"
    .List(2, 2) = "Three"
    .List(3, 2) = "Four"
    .List(4, 2) = "Five"

    Label1.Font.Bold = .Font.Bold
    Label1.Font.Italic = .Font.Italic
    Label1.Font.Name = .Font.Name
    Label1.Font.Size = .Font.Size
    End With
    End Sub
    '-----------------End Code

    Cheers
    Andy

    R4gtl wrote:
    > It is possible to give a different textalignement for some columns of a
    > listbox?
    > I want that some columns have a textalignement left and some columns have a
    > textalignement right. I don't know if it is possible. I have to put the
    > command in a macro.
    >


    --

    Andy Pope, Microsoft MVP - Excel
    http://www.andypope.info

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1