Sunday, August 23, 2009

How to set the Print Margins?


Option Strict On
Imports System.Drawing.Printing

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

With Me.PrintForm1
.PrintAction = Printing.PrintAction.PrintToPreview

For i = 0 To .PrinterSettings.PaperSizes.Count - 1
If .PrinterSettings.PaperSizes(i).Kind = PaperKind.A4 Then
Dim size As PaperSize = .PrinterSettings.PaperSizes(i)
.PrinterSettings.DefaultPageSettings.PaperSize = size
Exit For
End If
Next

Dim MyMargins As New Margins
With MyMargins
.Left = 0
.Right = 0
.Top = 0
.Bottom = 0
End With
.PrinterSettings.DefaultPageSettings.Margins = MyMargins
.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.CompatibleModeFullWindow)

End With

End Sub

End Class

...

To see Print Preview & Print a WindowsForm, Code in VB.Net


PrintDoc1.PrintPage += PDoc_PrintPage;
Private PrintDoc1 As PrintDocument = New PrintDocument
Private PrintPreviewDialog1 As PrintPreviewDialog = New PrintPreviewDialog

Private Sub btn_PrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_PreviewButton.Click 'When PrintPreview Button Clicks

PrintPreviewDialog1.Document = PrintDoc1
'To set or Get the Position of a Graphic Object
PrintDoc1.OriginAtMargins = True
'PrintDoc1.PrintPage += PDoc_PrintPage;
PrintPreviewDialog1.ShowDialog()
End Sub


Private Sub PDoc_PrintPage(ByVal sender As System.Object, ByVal e As PrintPageEventArgs)

Dim bmp As Bitmap = New Bitmap(Me.Width, Me.Height)

'this.DrawToBitmap(bmp, this.ClientRectangle);
Me.DrawToBitmap(bmp, Me.ClientRectangle)

'Takes the Snap of the Exact WindowForm size as Bitmap image
Me.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
e.Graphics.DrawImage(bmp, 0, 0)
End Sub

In C# Code

See Print Preview & Print a WindowsForm in Both C# & VB.Net



PrintDoc1.PrintPage += PDoc_PrintPage;

Here is my manual translation to Vb.Net.>>

'PrintDocument PrintDoc1 = new PrintDocument();
Private PrintDoc1 As PrintDocument = New PrintDocument
'PrintPreviewDialog PrintPreviewDialog1 = new PrintPreviewDialog();
Private PrintPreviewDialog1 As PrintPreviewDialog = New PrintPreviewDialog

'private void btn_PrintPreview_Click(object sender, EventArgs e)
Private Sub btn_PrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_PreviewButton.Click '{// When PrintPreview Button Clicks
'PrintPreviewDialog1.Document = PrintDoc1;
PrintPreviewDialog1.Document = PrintDoc1
'PrintDoc1.OriginAtMargins =true; //To set or Get the Position of a Graphic Object
PrintDoc1.OriginAtMargins = True
'PrintDoc1.PrintPage += PDoc_PrintPage;

'PrintPreviewDialog1.ShowDialog();
PrintPreviewDialog1.ShowDialog()
'}
End Sub

'private void PDoc_PrintPage(object sender, PrintPageEventArgs e)
Private Sub PDoc_PrintPage(ByVal sender As System.Object, ByVal e As PrintPageEventArgs)
'{
'Bitmap bmp = new Bitmap(this.Width, this.Height);
Dim bmp As Bitmap = New Bitmap(Me.Width, Me.Height)
'//this.DrawToBitmap(bmp, this.ClientRectangle);
Me.DrawToBitmap(bmp, Me.ClientRectangle)
'this.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height)); //Takes the Snap of the Exact WindowForm size as Bitmap image
Me.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
'e.Graphics.DrawImage(bmp, 0, 0);
e.Graphics.DrawImage(bmp, 0, 0)
'}
End Sub


Monday, April 13, 2009

Reading Excel with vb.net

Hi, I’m developing a tools that needs to read data from an excel sheet. I’ve had it working using
Microsoft.Jet.OLEDB.4.0, but now I have noticed it truncates the cell value if the character length is over 255.
I found that if you change the register value for “typeGuessRows” it seems to work, but this is not an option on some of the systems the tool will run.

Then I found that if I use “OdbcConnection” it seems to not truncate the value, but it has its own issues.
This would be if a colmun has mostly numerical data, it makes the string values “DBNULL”.

Does anyone know a way around the DBNULL issue?

[code]
Dim con As New OdbcConnection()
Dim cmd As New OdbcCommand()
Dim PrmPathExcelFile As String
PrmPathExcelFile = "Raw_Translation_300_wpsegroflashint.xls"
con.ConnectionString = "Driver={Microsoft Excel Driver (*.xls)};DriverId=790;DBQ=" + PrmPathExcelFile
con.Open()
cmd.Connection = con
cmd.CommandText = "SELECT * FROM [" & sheetName & "$]"

cmd.CommandType = CommandType.Text

Do While rdr.Read()


Form1.list.Items.Add(checkDBNULL(rdr(0)).ToString + " 1- " + checkDBNULL(rdr(1)).ToString + " 2- " + checkDBNULL(rdr(2)).ToString + " 3- " + checkDBNULL(rdr(3)).ToString + " 4- " + checkDBNULL(rdr(4)).ToString + " 15- " + checkDBNULL(rdr(5)).ToString + " 6- " + checkDBNULL(rdr(6)).ToString + " 7- " + checkDBNULL(rdr(7)).ToString)
Loop
rdr.Close()
con.Close()
[/code]