printersettings papersources does not return correct values

  • Thread starter Richard Golebiowski
  • Start date
R

Richard Golebiowski

I have been trying to figure this out for quite some time and cannot
find any examples in VB.Net or in VB that work correctly. I am working
on an application where I want the user to be able to select a peinter
and printer tray to print reports out. In Crystal Reports 8.5, I can
select a printer and tray and it prints correctly.
I did a test report that I use to tell me the value that Crystal
Reports is using for the paper source. When I use those values in my
program to initialize Crystal, it will print where I expect it to
print. So I know Crystal Reports is able to select the correct printer
and paper source, there are no problems with drivers or anything else,
it works!
Next, I get the values for the paper sources from printersettings.
The values that I'm getting from printersettings are not correct. The
values are different then what I get using Crystal Reports. I have
tried this with a number of printers and I get the same result,
Crystal Reports can get the correct settings but printersettings
can't!
I have even tried using the DeviceCapabilities function in the
Windows API and it does not work either. In a lot of instances,
DeviceCapabilities returned different values then the ones I got using
PrinterSettings.
A couple of years ago I posted a message on the news groups about
this problem. At that time I was using an earlier version of VB.Net
and I could see
a private implementation field for PaperSource. This field, the "kind"
field, showed the correct value for the PaperSource. The "Kind" field,
which is what you can read, gave the wrong value. Microsoft's solution
was to fix .NET so you would not see the "kind" value. For some reason
they chose not to give the correct value in the "Kind" field.
So, I have tried just about everything I could find on the internet
and I still have not found any code that works correctly. Maybe I'll
try doing it in C next! At this point I feel I have exhausted all of
my options with VB and VB.NET.
Has anyone out there been able to get the correct values for the
paper source? There has to be a "correct" way to do this because I
know it works in Crystal Reports and in Microsoft Word.
 
T

timpub

This is a very ugly hack, but you can use reflection to access the
private "kind" field, as shown in the sample below :

For each paperSource in thePrinterSettings.PaperSources
dim rawKind as Integer
rawKind = paperSource.Kind
If paperSource.Kind = PaperSourceKind.Custom Then
rawKind = CInt(paperSource.GetType().GetField("kind",
Reflection.BindingFlags.Instance Or
Reflection.BindingFlags.NonPublic).GetValue(paperSource))
End If
debug.WriteLine(paperSource.SourceName & " = " & rawKind)
Next paperSource
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top