[RESOLVED] [2005] Converting Printing Code From VB 6.0
Hello to All:
I have used Visual Basic 6.0 for a number of years now, and am working on converting some of my programs from VB 6.0 to Visual Basic 2005 Express Edition.
The only problem I am having is with the printing code.
In VB 6.0, all I had to do to print a line of text was say "Printer.Print <my text>".
I searched on the Internet and found some sample code, which explained how to print using Visual Basic 2005 Express. However, the code required the user to keep track of the current position of the text, and the "Graphics Unit" for the "PrintDocument" control does NOT have a line option.
I am trying to manually print some text from a listbox to create a print-out of a shopping list from the program. I need to print line by line and not worry about the current line position (as the number of lines for print-out varies).
I am lost and have tried multiple things to get the code working in Visual Basic 2005, but I keep getting compile errors when I use the code found on the Internet. I am looking for an easy way to convert this code, which is hundreds of lines long.
The code I am trying to convert is a bit long (and exceeded the maximum number of characters for this form), so I am posting a text document.
Any help you can provide would be greatly appreciated.
Printing is quite simple. Let's the user is going to specify a number and you want to print "Hello World" that many times, 25 lines to a page. All you need to do is keep a class-level variable that indicates the number of lines that you've already printed. You then read that variable each time the PrintPage event is raised so you know how many more lines you have to print. It's the same principle if you have a lot of text to print. You simply keep a class-level variable that indicates the index of the next line to print. When you get to the end of the PrintPage event handler you test that index against the total number of lines. If it is less then HasMorePages is set to True and the PrintPage event will be raised again. Next time the PrintPage event handler is executed your class-level variable still contains the index of the next line so you simply start printing from there.
I replied already, but I forgot to supply this additional information.....
In the code I have, I DO keep track of the current X and current Y position on the page in order to print a certain number of lines on the first page (because it contains a header) or to print text centered on one line. However, I don't know how to keep track of the current position in VB 2005, as I have never programmed in VB 2005 before (this is the first program I am trying to convert). That is why some sample code would help.
What units does VB 2005 use as default? I need to print in a two-column format on the page and have used tab indexes in VB 6.0.
Would "Tab(70)" work the same way in VB 2005 as it does in VB 6.0, provided that the font name and the font size is the same in both versions?
Would "CurrentX" and "CurrentY" have the same Integer values per line in both VB 6.0 and VB 2005 (if VB 2005 even HAS a CurrentX and CurrentY property for the printer object)?
.NET printing uses GDI+. To understand how to print in .NET you have to have at least a basic understanding of GDI+. The cornerstone of GDI+ in .NET is the Graphics class. GDI+ is used to do all drawing in .NET apps, whether that be drawing on a control for display on-screen or for printing. You use a Grahics object in both cases and you use exactly the same methods to draw. The difference is that the Graphics object is created in a different context. The e.Graphics property in the PrintPage event handler is the Graphics object you will be using when printing. I suggest that you read about the Graphics class and, in particular, its DrawString method. That will be the most used method when printing because you're normally going to want to print text, although you can print basically anything you want including images and simple or complex shapes. If you want a basic example of printing text then check out the 2003 101 Samples from Microsoft.
Most importantly, forget VB6. This is VB.NET and things have changed a lot. Printing has changed completely. My advice is to not start with VB6 code and try to convert but rather start with a firm idea of what you want to achieve as an end result and then look for the best way to achieve that result in VB.NET.
I had a feeling that I would need to spend time recoding in order to get my program printing again.....
Not a problem.....I was just hoping there was an easier way (I mean, I already HAD the code from VB 6.0 and thought there might be a way to convert it without having to recode hundreds of lines). I will take a look at the samples on Microsoft.com and try to figure out how to print what I need to from scratch (I had to change the coding somewhat for the Open File and Save File commands as well).
What I will do is print a number of lines of text on a page, not printing text, put printing the control information (current X and current Y), trying to figure out how these values increment (by what factor). I will then use that information to recode in order to print the same thing that prints in VB 6.0 in VB 2005 in the SAME locations. This shouldn't be that hard for me to figure out, as the font size and font name stays consistent throughout the entire printout.
Then, I will print out my entire "File --> Print" code, so I can refer to it when making the necessary code changes in VB 2005. Even though I will need to recode the entire sub for use in VB 2005, I will refer to my old code only so I can find the comparable code to perform the same task(s) in Visual Basic 2005.
I had no idea that the printing code changed THAT much!!!
I mean.....the version of Visual Basic I have (6.0) is over seven years old, and I haven't really seen many updates to Visual Basic over the years.
I guess that with new technology and new operating systems (VB 6.0 was most likely based on Windows 95 / 98 coding), comes new coding conventions, standards, and techniques.
Enough of my ranting!!!
Thanks for your help, "jmcilhinney"!!!
.....I have no idea how to end this thread, so to all of you fellow Visual Basic coders reading my post...this post is now RESOLVED.
Since VB6 there has been VB.NET 2002, VB.NET 2003 and now VB 2005. The watershed was the transition from VB6 to the first version of VB targetted at the .NET platform. In order for VB to fall in line with other languages that were also targetted at the .NET platform it required a major rewrite. The VB.NET syntax is based on VB6 but you should consider them different languages apart from that. C# is based on C/C++ but it is not either of them. It is a new language and should be treated as such. VB.NET requires the same respect or you will fall into the trap of trying to make it work like VB6 when it doesn't and shouldn't. Some things will be the same, which is a bonus, but make no assumptions that any particular feature is or will be the same.
I was also converting a VB6 program to 2005 and had the same problem. Thousands of lines of printing.
It works. Microsoft says not to used it on new project, to go to the vb.net new version. Like you I would like to have someone show me the sample code to print "Hello World" at a programed x,y point and draw a line under it.