Formatting an integer for a listbox 
Author Message
 Formatting an integer for a listbox

I have been trying to output an integer variable to a list box and
formatting it so that the subsequent columns are vertically aligned but I
seem unable to do this simple task. The books I have read say that the
Format function does this but it does not do it in my code:-

    For I = 1 To NumPay
        m_int = m_Interest * m_Principal
        m_totint = m_totint + m_int
        m_prin = m_Payment - m_int
        If m_prin > m_Principal Then m_prin = m_Principal
        m_Principal = m_Principal - m_prin
        AddStr = Format(I, "###") & Space(10) & Format(m_int, "#####0.00")
        AddStr = AddStr & Space(5) & Format(m_prin, "#####0.00") & Space(5)
        AddStr = AddStr & Format(m_totint, "######0.00") & Space(5)
        AddStr = AddStr & Format(m_Principal, "######0.00")
        lstAmort.AddItem AddStr
    Next I

The first format always displays the minimum number of digits even though I
want it to display the number padded to three characters.

As you may see I am a beginner with VB but I have been programming in a
different language for many years and I've never had this problem before,
not even with VAX BASIC.

Marcus



Tue, 02 Oct 2001 03:00:00 GMT  
 Formatting an integer for a listbox
Marcus,

It looks like you overlook the cause of non-alignment: Proportional fonts
use a different width for each character.

However, to use a fixed number of digits with Format, you have to use the
syntax Format(number, "0000.00"). You get leading zero's in the result, but
that's the consequence of using a fixed size.

You could use a fixed size font to display the listbox (i.e. Courier New),
then you can align by padding spaces to the length of the strings, as you
did in "the old days".

Or you could use a ListView control with multiple columns, that's more in
line with the Windows interface standards.

John Tegelaar

Marcus Lesniak heeft geschreven in bericht

Quote:
>I have been trying to output an integer variable to a list box and
>formatting it so that the subsequent columns are vertically aligned but I
>seem unable to do this simple task. The books I have read say that the
>Format function does this but it does not do it in my code:-

>    For I = 1 To NumPay
>        m_int = m_Interest * m_Principal
>        m_totint = m_totint + m_int
>        m_prin = m_Payment - m_int
>        If m_prin > m_Principal Then m_prin = m_Principal
<SNIP>
>Marcus



Tue, 02 Oct 2001 03:00:00 GMT  
 Formatting an integer for a listbox
John is right about the cause -- you need to use a monospaced font. If this
program is for distribution, don't forget to include the font (respecting
any copyrights of course) with the application.

Also, if you don't want the leading 0's from John's suggestion, you can use
the following instead:

     Right$(Format$(0, String$(X, " ") & "0.00"), X)

where X is either set in your program or the value inserted directly in the
expression. X is the maximum number of characters positions you want to
display. In the String$(X, " ") portion, that is a single blank space
character between the quote marks.

Rick

Quote:

>Marcus,

>It looks like you overlook the cause of non-alignment: Proportional fonts
>use a different width for each character.

>However, to use a fixed number of digits with Format, you have to use the
>syntax Format(number, "0000.00"). You get leading zero's in the result, but
>that's the consequence of using a fixed size.

>You could use a fixed size font to display the listbox (i.e. Courier New),
>then you can align by padding spaces to the length of the strings, as you
>did in "the old days".

>Or you could use a ListView control with multiple columns, that's more in
>line with the Windows interface standards.

>John Tegelaar

>Marcus Lesniak heeft geschreven in bericht

>>I have been trying to output an integer variable to a list box and
>>formatting it so that the subsequent columns are vertically aligned but I
>>seem unable to do this simple task. The books I have read say that the
>>Format function does this but it does not do it in my code:-

>>    For I = 1 To NumPay
>>        m_int = m_Interest * m_Principal
>>        m_totint = m_totint + m_int
>>        m_prin = m_Payment - m_int
>>        If m_prin > m_Principal Then m_prin = m_Principal
><SNIP>
>>Marcus



Tue, 02 Oct 2001 03:00:00 GMT  
 Formatting an integer for a listbox
To ease the strain, set the font of the list box to a proportional
font.  Courier, Fixedsys come to mind....

LFS

Quote:

> I have been trying to output an integer variable to a list box and
> formatting it so that the subsequent columns are vertically aligned but I
> seem unable to do this simple task. The books I have read say that the
> Format function does this but it does not do it in my code:-

>     For I = 1 To NumPay
>         m_int = m_Interest * m_Principal
>         m_totint = m_totint + m_int
>         m_prin = m_Payment - m_int
>         If m_prin > m_Principal Then m_prin = m_Principal
>         m_Principal = m_Principal - m_prin
>         AddStr = Format(I, "###") & Space(10) & Format(m_int, "#####0.00")
>         AddStr = AddStr & Space(5) & Format(m_prin, "#####0.00") & Space(5)
>         AddStr = AddStr & Format(m_totint, "######0.00") & Space(5)
>         AddStr = AddStr & Format(m_Principal, "######0.00")
>         lstAmort.AddItem AddStr
>     Next I

> The first format always displays the minimum number of digits even though I
> want it to display the number padded to three characters.

> As you may see I am a beginner with VB but I have been programming in a
> different language for many years and I've never had this problem before,
> not even with VAX BASIC.

> Marcus



Fri, 05 Oct 2001 03:00:00 GMT  
 Formatting an integer for a listbox
Check out http://www.mvps.org/vbnet/code/listapi/listboxtabs.htm for info on
setting tabstops in the listbox instead, and grab the free CoolTabs designer
from http://www.mvps.org/vbnet/res/downloads/cooltabs.htm

This will allow you to continue to use proportional fonts, provide that
ability to right-justify a tab, and should reduce your code to a more
manageable

     For I = 1 To NumPay
         m_int = m_Interest * m_Principal
         m_totint = m_totint + m_int
         m_prin = m_Payment - m_int

         If m_prin > m_Principal Then m_prin = m_Principal
         m_Principal = m_Principal - m_prin

         AddStr = Format(I, "###") & vbTab & Format(m_int, "#####0.00") &
vbTab
         AddStr = AddStr & Format(m_prin, "#####0.00") & vbTab
         AddStr = AddStr & Format(m_totint, "######0.00") & vbTab
         AddStr = AddStr & Format(m_Principal, "######0.00")

         lstAmort.AddItem AddStr

     Next I

--

Randy Birch, MVP Visual Basic

http://www.mvps.org/vbnet/
http://www.mvps.org/ccrp/


Quote:
> I have been trying to output an integer variable to a list box and
> formatting it so that the subsequent columns are vertically aligned but I
> seem unable to do this simple task. The books I have read say that the
> Format function does this but it does not do it in my code:-

>     For I = 1 To NumPay
>         m_int = m_Interest * m_Principal
>         m_totint = m_totint + m_int
>         m_prin = m_Payment - m_int
>         If m_prin > m_Principal Then m_prin = m_Principal
>         m_Principal = m_Principal - m_prin
>         AddStr = Format(I, "###") & Space(10) & Format(m_int, "#####0.00")
>         AddStr = AddStr & Space(5) & Format(m_prin, "#####0.00") &
Space(5)
>         AddStr = AddStr & Format(m_totint, "######0.00") & Space(5)
>         AddStr = AddStr & Format(m_Principal, "######0.00")
>         lstAmort.AddItem AddStr
>     Next I

> The first format always displays the minimum number of digits even though
I
> want it to display the number padded to three characters.

> As you may see I am a beginner with VB but I have been programming in a
> different language for many years and I've never had this problem before,
> not even with VAX BASIC.

> Marcus



Fri, 05 Oct 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Listbox.Text to integer value

2. ListBox input as integer ?

3. How to Sort integer numbers inside listbox

4. Listbox Index limited to Integer

5. How to sort integer numbers inside Listbox control

6. How to sort integer numbers inside ListBox

7. Formatting integers using regional settings

8. Formatting Integer Parameters in Crystal Reports/Enterprise?

9. Format Q: Integer DB field shows 2 decimals.

10. Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

11. Converting integers and long integers to byte arrays

12. Long = Integer * Integer Overflow error

 

 
Powered by phpBB® Forum Software