Link to home
Start Free TrialLog in
Avatar of luca345
luca345

asked on

VB6 Printer object conversion to VB.NET

Hi ,

I need to migrate the print modules from VB6 to VB.net.

I see there are many differences and I don't known how I can do ...

In VB6 I have a 'picture Box' as print preview and 'printer' object to print.

Attached some old VB6 routines in my previous project.

I need to to this in VB.net:

1) Find all instructions alternative / best solution to convert  all properties / methods of the OLD printer object.

2) I need a way to use a 'picture box' object (or similar) as print preview like VB6.

In short I need the best approach to convert the old printer object in VB.net.

I have find many sites but don't seem easy .....

Thank you

ModuloAnteprimaStampe.txt
Avatar of nepaluz
nepaluz
Flag of United Kingdom of Great Britain and Northern Ireland image

thisis an example of printing a datagridview, but the basics are the same:
http://www.snippetbank.net/detail/snippetdetail/2-vb/1-controls/525-Print-Preview-A-DataGridView-Control.html
Avatar of Chris Raisin
Standby Marco,

I think I have a solution for you.

Cheers
Chris
Marcos, as promised here is my answer.

You have two approaches:

   1. you can either use the VB.Net "PrintDialog" object
   2. you can use an object which inherits its values from the VB6 printer object
       
The second one is probably what you are after.

To use the second option you simply [place at the top of any module/class in which you are using the printer object the following line:

"Imports Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6"

The following code demonstrates these two methods.

As far as the Picturebox goes, you would use a "PrintPreviewDialog" instead or perhaps even a "PrintPreviewControl" rather than a PictureBox (which still exists).

Marcos, your questions seem to be concentrating on conversion of VB6 to VB.Net, and I must advise you are really taking the "hard" path if that is what you are doing. Your employer would be better advised to get you trained in VB.Net
(there is an excellent course which I am doing online available at
http://www.learndevnow.com/Topics.aspx).
Then write your projects afresh in the VB.Net coding, rather than trying to convert VB6 code.




Imports Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6
Public Class Form1

    Private Sub PrintToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles PrintToolStripMenuItem.Click
        Dim oPrinter As New Printer      'VB6 Printer object using imports library referenced above 
        oPrinter.Orientation = vbPRORLandscape
        oPrinter.Copies = 1
        oPrinter.Print(SPC(10), "This is a test")
        oPrinter.Print("Now we will go to next page")
        oPrinter.NewPage()
        oPrinter.Print("Now we are on second page")
        oPrinter.EndDoc() 'send the output to tthe printer


        Dim oPrinter2 As New PrintDialog  'VB.Net version
        Dim oFile As New OpenFileDialog
        Dim cDoc As String

        oFile.ShowDialog()
        If oFile.FileName <> "" Then
          cDoc = oFile.Filename    
          oPrinter2.DocumentName = cDoc
          oPrinter2.PrinterSettings.DefaultPageSettings.Landscape = True
            oPrinter2.PrinterSettings.DefaultPageSettings.Margins.Left = 500 'half an inch
            oPrinter2.PrinterSettings.Copies = 1
            oPrinter2.AllowPrintToFile = True
            oPrinter2.AllowSelection = True    'allows user to print a selection
            oPrinter2.AllowSomePages = True    'allows user to select certain pages
            oPrinter2.ShowDialog()
        End If
    End Sub

Open in new window

Avatar of luca345
luca345

ASKER

Dear Chris,

All VB.net must use .NET framework.

Import the the old VB6 calls is easy:

Imports Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6

but the support is not guaranteed because it is old.

If I make an effort to convert from VB6 to VB.net I can not revert even after a few years.

I must use only new instructions not old.

I think you undestand ....

Marco
Avatar of luca345

ASKER

Dear Chris ,

Another question was post here:

https://www.experts-exchange.com/questions/27260904/VB6-execute-file-with-administrator-permission.html

I think this is easy but not too ....

Marco
Marcos, it is not a matter of using new or old.

The Imports file IS VB.Net but it simply takes in the "format" used in VB6 and returns values in the same manner as VB6. Using the includes file does NOT mean you are retaining VB6 code.

Still, it is up to you. It will mean changing code a bit more, but you will therefore have to work with the PrintDialog object which is the VB.Net replacement for the VB6 Printer object.

See the "oPrinter2" object in the code I gave as an example to see how it works.

Intellisense should then help you understand how the object is manipulated to perform the various methods and accept/return various values into it's properties.

There is no way of using aliases in this case, unfortunately. All the code regarding the printers will have to be re-written into VB.Net format line by line.

Cheers
Chris
Avatar of luca345

ASKER

Dear Chris ,

In this moment I have write:

//
Imports Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6
//

The compiler don't see this reference (PowerPacks) probably becouse is not supported in VB.Net 2010 but probably only the previous version.

Tested in VB.NET 2008 (Express) Work.

This is an example of concept (new or old) the old have short life.

To prevent situation like this that I don't want use old instructions.

Marco
No, you have to add the library to your references in your compiler.

I AM using VB.Net 2010 by the way! (I always use the latest release).

It will never happen that the "include" file will not run on future version of VB.Net

Go into VS 2010 and open your project page, then click on "Resources" then click on the "Add" button and browse to the file:
"C:\Program Files\Reference Assemblies\Microsoft\VBPowerPacks\V10.0\Microsoft.VisualBasic.PowerPacks.VS.dll"

Once that is added, the code will work. Of course this file will automatically be bundled with your final deployment of the program (automatically) and will be registered automatically as well (no need to use RegAsm.exe)

I repeat, you are NOT using old instructions. The PowerPacks are a feature of Visual Studio and the object produced by them is NOT a VB^ object. It is pure VB.Net so will always be available in future upgrades.
Sorry, I meant VB6, not VB^

Cheers
Chris
Avatar of luca345

ASKER

Dear Chris ,

Thanks for this information.

If I have undestand this object is 100% identical to VB6 printer object ?

Teorically no need conversion ? Or there are difference ?

Marco
I think it is 100% as far as I know. I have never had problems.
Avatar of luca345

ASKER

Dear Chris ,

Ok this 70 / 80% .

Now I need to 'simulate' the some instructions in to 'picturebox' like VB6.

As you known in VB6 the picturebox have the some graph methods of the printer object.

My VB6 project use this like 'print preview'

What you tell me about this ?

Marco
It looks like I was incorrect about the PrintPreview Objects.

You still use "PictureBox" but it is a little more complicated than under VB6.

Use Intellisense to discover the PictureBox.

Some simple sample code follows.



Dim pt1, pt2 As Point
  pt1.X = oPicBox.Left + 100
  pt1.Y = oPicBox.Top + 100
  oPicBox.CreateGraphics.DrawLine(Pens.AliceBlue, pt1, pt2)

Open in new window

Avatar of luca345

ASKER

Dear Chris,

In my questions there are nothing of  'not complicated'.

Can you please tell me what are the main differences ?

Marco
Well,

All I can say is that it is similar to the picturebox in VB.Net but has greater power (as you can ascertain from Intellisense).

Some simple code shows below.

As you type in the code, you will see the Intellisense show you all the available public methods and INstance variables (properties) available to you.

Of course you will have to type in the name of a file available on your system for the Image.

Cheers
Chris
Private Sub DisplayImage() 

  Dim imageControl As New PictureBox() 
  Dim Image As New Bitmap("C:\\Images\\Creek.jpg") 

  imageControl.Width = 400 
  imageControl.Height = 400 
  imageControl.Dock = DockStyle.Fill 
  imageControl.Image = Image 
  Controls.Add(imageControl) 
End Sub

Open in new window

Avatar of luca345

ASKER

I've requested that this question be deleted for the following reason:

problem solved myself
ASKER CERTIFIED SOLUTION
Avatar of Chris Raisin
Chris Raisin
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of luca345

ASKER

Very good.
Avatar of luca345

ASKER

Dear craisin ,

Excuse me for this I have lost the number of open questions ...

sorry for this.

Marco
Thanks a lot, Marco.

I thought that must have been the reason, since you are always so fair with your point allocations.

I hope your conversion project is going along well with lots of success moments.
:-)

I have been quite ill now for several weeks and am getting a little worried about my state of health. My blood pressure has been very high and I have had a constant cough now for over four weeks.

Life is quite miserable, but my helping you via EE cheers me up...

All the best

Chris