number formatting

I wnat to print some text and a number in 2 columns with the number aligned right. How can I print the leading/trailing spaces (instead of zeros) while printing a number so that it's aligned right.

i want the output to be something like this...

xyz      $123.00
xxxxxx   $  5.00


aaa      $123.00
dasdasd    $5.00

Who is Participating?
mcriderConnect With a Mentor Commented:
So try this example:

Start a new project, and add a listbox and a command button to Form1, the paste the following into the "Declarations Section" of Form1:

Private Declare Function SendMessageArray Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const LB_SETTABSTOPS = &H192
Function RightJustify(ByVal Source As String, Size As Long) As String
    Do While Me.TextWidth(Source) < Size
        Source = " " + Source
    RightJustify = Source
End Function
Private Sub Command1_Click()
   Dim Retcode As Long
   ReDim TabStop(1 To 3) As Long
   TabStop(1) = 50
   TabStop(2) = 100
   TabStop(3) = 150
   Retcode = SendMessageArray(List1.hwnd, LB_SETTABSTOPS, 3, TabStop(1))

    List1.AddItem "Hello" & vbTab & RightJustify("100.00", 1440) & vbTab & "Goodbye"
    List1.AddItem "Hello" & vbTab & RightJustify("1.05", 1440) & vbTab & "Goodbye"
    List1.AddItem "Hello" & vbTab & RightJustify("15.78", 1440) & vbTab & "Goodbye"
End Sub

Function padstring(str1, str2 As String, length As Integer) As String
  Numspaces = length - (Len(str1) + Len(str2))
  padstring = str1 + Space$(Numspaces) + str2
End Function
Private Sub Command1_Click()
List1.AddItem (padstring("dave was here", "5.00", 40))
List1.AddItem (padstring("So was joe, really", "23.00", 40))
End Sub

Have fun!!
DrDelphi -

Sorry - doesn't work.

Becuase of the "size" of the characters.

For example - the "S" and the "s" are different sizes, and therefore, there is an offset in the listbox.

I have tested this with both TrueType and non TT fonts with no success.

At first I wrote the exact same code, but found that my results weren't what I expected.

7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

naikvAuthor Commented:
does'nt work due to different sizes of characters!
Can you change the font to be "Courier New" or "FixedSys" so that you can use fixed spacing?  Listboxes don't have a right-align feature, so without that I suspect you won't be able to do it too easily.

Can you use the RichTextBox control (Richtx32.ocx)?  This may help you, although I never have used it for this purpose.  Check out the RightMargin and SelAlignment properties.  This should work in the following cases:
  1) you don't have anything else on the line, and
  2) you always have the same number of digits to the right of the decimal. (All MS fonts have fixed-pitch digits; if you're using a non-MS font this may still be an issue for you.)

Hope this help,
This Function will text align data into a single string that can be used for Printing or a Listbox.

1. Open a New Standard.Exe Project.
2. Add a Listbox (List1) to Form1.
3. Add a Picturebox (Picture1) to Form1.
4. Copy/Paste the following into the Form1 code window.
5. Press F5 to Run.

<----- Code Begin ----->

Option Explicit

Private Sub Form_Load()

   Form1.Move _
      Screen.Width * 0.1, _
      Screen.Height * 0.1, _
      Screen.Width * 0.8, _
      Screen.Height * 0.8
   ' Set Working Picture Box To Hidden
   Picture1.Visible = False

   Dim varArray As Variant
   Dim strReturn As String
   ' Set Column Alignment L=Left R=Right C=Center
   varArray = Array("L", "R", "C")
   strReturn = x_FormatOutput("Align", varArray)
   ' Set Font Data
   varArray = Array( _
      List1.FontBold, _
      List1.FontItalic, _
      List1.FontName, _
      List1.FontSize, _
      List1.FontStrikethru, _
   strReturn = x_FormatOutput("Font", varArray)
   ' Set Column Widths
   varArray = Array(500, 500, 500)
   strReturn = x_FormatOutput("Width", varArray)
   ' Process Headings
   varArray = Array( _
      "Left", _
      "Right", _
   List1.AddItem x_FormatOutput("Print", varArray)
   ' Process Detail
   Dim intIndex As Integer
   For intIndex = 1 To 20
      varArray = Array( _
         intIndex * 2, _
         intIndex * 30, _
         intIndex * 400)
      List1.AddItem x_FormatOutput("Print", varArray)
   Next intIndex

End Sub
Private Sub Form_Resize()
   List1.Move _
      ScaleWidth * 0.05, _
      ScaleHeight * 0.05, _
      ScaleWidth * 0.9, _
      ScaleHeight * 0.9

End Sub

Private Function x_FormatOutput _
(ByVal strCommand As String, _
ByVal varData As Variant) _
As String
   Static varAlign As Variant
   Static varWidth As Variant
   If strCommand = "Align" _
      varAlign = varData
      Exit Function
   End If
   If strCommand = "Font" _
      Picture1.FontBold = varData(0)
      Picture1.FontItalic = varData(1)
      Picture1.FontName = varData(2)
      Picture1.FontSize = varData(3)
      Picture1.FontStrikethru = varData(4)
      Picture1.FontUnderline = varData(5)
      Exit Function
   End If
   If strCommand = "Width" _
      varWidth = varData
      Exit Function
   End If
   If UBound(varData) <> UBound(varAlign) _
   Or UBound(varData) <> UBound(varWidth) _
      MsgBox "x_FormatOutput: Data Does NOT Match Formatting"
      Exit Function
   End If
   Dim lngIndex As Integer
   Dim lngLeft As Long
   Dim lngRight As Long
   Dim lngWidth As Long
   Dim strWork() As String
   ReDim strWork(UBound(varData))
   For lngIndex = 0 To UBound(varData)
      lngWidth = Picture1.TextWidth(varData(lngIndex))
      Select Case varAlign(lngIndex)
         Case "C" ' Center
            lngLeft = ((varWidth(lngIndex) - lngWidth) / 2) _
               / Picture1.TextWidth(" ")
            lngRight = ((varWidth(lngIndex) - lngWidth) - lngLeft) _
               / Picture1.TextWidth(" ")
            strWork(lngIndex) _
               = Space(lngLeft) _
               & varData(lngIndex) _
               & Space(lngRight)
         Case "R" ' Right
            lngLeft = (varWidth(lngIndex) - lngWidth) _
               / Picture1.TextWidth(" ")
            strWork(lngIndex) _
               = Space(lngLeft) _
               & varData(lngIndex)
         Case Else ' Left
            lngRight = (varWidth(lngIndex) - lngWidth) _
               / Picture1.TextWidth(" ")
            strWork(lngIndex) _
               = varData(lngIndex) _
               & Space(lngRight)
      End Select
   Next lngIndex
   x_FormatOutput = Join(strWork, " ")
End Function

<----- Code End ----->
I use this method

While RSMetodi.EOF <> True
   RSMetodi!codice &  Space(20 - Len(Str(RSMetodi!codice))) & RSMetodi!dollars

I put a number of space caracter respect of the len of value the result is:


Space(20 - Len(Str(RSMetodi!codice)))

20 is a max len of your 'yz' or    
'xxxxxx' fileds
Since you're talking about a listbox, try this...Add the following to a MODULE:

   Public Declare Function SendMessageArray Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
   Public Const LB_SETTABSTOPS = &H192

Then you can set up tabs in a listbox like this... This example sets up 3 columns:
   Dim Retcode As Long
   ReDim TabStop(1 To 3) As Long
   TabStop(1) = 100
   TabStop(2) = 200
   TabStop(3) = 300
   Retcode = SendMessageArray(List1.hwnd, LB_SETTABSTOPS, 3, TabStop(1))

Then you can add items to the listbox in columns like this:

   List1.AddItem "Hello" & vbTab & "World" & vbTab & "Goodbye"

Hope this helps!

By the way, make sure the listbox is wide enough to see the 3 column in the example I gave...

Questioner wants a right justify in one of the columns.. as such.. a TextWidth and space padding have to be added somewhere.. <sigh>. You can grab something from my code if you like.. <smile>.
Alot of work for just 10 points... ;-)

<laughing>.. Yeah, ain't it a b*tch.. <smile> and a <wink>
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.