Solved

number formatting

Posted on 2000-04-09
13
185 Views
Last Modified: 2010-05-02
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

OR..

aaa      $123.00
dasdasd    $5.00

0
Comment
Question by:naikv
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 6

Expert Comment

by:DrDelphi
ID: 2698520
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!!
0
 
LVL 4

Expert Comment

by:wileecoy
ID: 2698576
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.

0
 

Author Comment

by:naikv
ID: 2698660
does'nt work due to different sizes of characters!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:BSmiley
ID: 2698787
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,
Bob
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2699140
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, _
      List1.FontUnderline)
   strReturn = x_FormatOutput("Font", varArray)
   
   ' Set Column Widths
   varArray = Array(500, 500, 500)
   strReturn = x_FormatOutput("Width", varArray)
   
   ' Process Headings
   varArray = Array( _
      "Left", _
      "Right", _
      "Center")
   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" _
   Then
      varAlign = varData
      Exit Function
   End If
   
   If strCommand = "Font" _
   Then
      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" _
   Then
      varWidth = varData
      Exit Function
   End If
     
   If UBound(varData) <> UBound(varAlign) _
   Or UBound(varData) <> UBound(varWidth) _
   Then
      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 ----->
0
 

Expert Comment

by:AlessandroF
ID: 2699463
I use this method

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

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

zzzzzz----$8,90
x-------$565,90

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

20 is a max len of your 'yz' or    
'xxxxxx' fileds
   
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2699911
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))
   List1.Refresh


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

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


Hope this helps!



Cheers!®©
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2699918
By the way, make sure the listbox is wide enough to see the 3 column in the example I gave...


Cheers!®©
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2700968
mcrider:
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>.
0
 
LVL 14

Accepted Solution

by:
mcrider earned 10 total points
ID: 2703905
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
    Loop
    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.Refresh

    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



Cheers!®©
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2703907
Alot of work for just 10 points... ;-)


Cheers!®©
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2704601
mcrider:
<laughing>.. Yeah, ain't it a b*tch.. <smile> and a <wink>
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2706129
<VBG>!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question