Am 27.01.2011 10:20, schrieb Jason Keats:
> StrandElectric wrote:
>> This works fine aligning the figures for my financial reports (apolgies for
>> the double linespacing) BUT note the first imports line. Can anyone suggest
>> a modified coding that does not need this line(anticipating that support for
>> it may weell disappear...) it is the p.xxx lines that don't work. looks like
>> the others will.
>>
>> Imports Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6
>
> The reason the above line is required is so that you can write
> Dim p As New Printer
> instead of
> Dim p As New
> Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6.Printer
>
> You can still use the same code in .NET 4 (ie VS2010).
>
> How do you know the above won't work in .NET 5 and beyond?
>
> The only (current) cause for concern is if you want to use Linux - as
> Mono probably doesn't contain the equivalent of the above Microsoft
> assembly.
>
> And, if you REALLY want your decimal points to line up properly, then
> you should use a routine like...
>
> Public Sub PrintAmount(ByVal p As Printer, ByVal right As Double, ByVal
> amount As Double)
> p.CurrentX = right - p.TextWidth(Format(amount, "#,###,###,##0.99"))
> p.Write(Format(amount, "#,###,###,##0.00"))
> End Sub
>
> The ".99" above will eliminate the slight variation in alignment of the
> decimal points when using a proportional font.
First, there is no difference between "#,###,###,##0.00" and "#,##0.00"
because the "#" are not placeholders for leading blanks.
Second, the "99" doesn't work for me.
Format(1.23, "#,###,##0.99")
returns
"123"
without a decimal point.
Third, p.CurrentX is of type Single only. A double value doesn't fit. ;-)
Fourth, I'd prefer passing "amount As DECIMAL" instead in order to make
the _caller_ think about printing a decimal value. (even though it doesn't
prevent passing a value with more than two digits after the ".") However,
this is not so important.
5th, to me it is a contradiction if you intentinally write code to keep the
"." in line also with proportional fonts on one side, but on the other side,
you pass the 'right' value to the method. I mean, the 'right' value is the
one that is _not_ equal with proportional fonts while lining up the ".".
Hence I'd either pass the location of the ".", or use a monospace font.
So, my version is:
Public Sub PrintAmount(ByVal p As Printer, ByVal DecimalPosition As Single, ByVal amount As Decimal)
Const fmt1 As String = "#,##0"
Const fmt2 As String = fmt1 & ".00"
p.CurrentX = DecimalPosition - p.TextWidth(Format(Math.Truncate(amount), fmt1))
p.Print(Format(amount, fmt2))
End Sub
BTW, luckily I have a PDF printer. Otherwise I'd have to waste paper for this
example because I'm not able to use a Graphics object drawing on a Form
or Bitmap. (Your code is absolutely ok! I'm only mentioning the downside
of using the old Printer object.)
BTW #2, I've written a small program looking for the font with the maximum
deviation between the size of the numbers. The font found was "Nyala", giving
a maximum deviation of 1.46 between the "1" and the "6". Using my code above,
the result is:
http://www.abload.de/image.php?img=d...enyalakx9n.png
I think this is more convincing than Arial. ;-)
--
Armin