Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Trying to make a simple text editor with printing capabilities using VB.net in Visual Studio 2005 .... but running into some problems. Here is the code and i will document the error in the code. The error code is in bold down bottom. For some reason it doesn't like my ''Then'' statement. It's generating an error at runtime. The error is as follows: "Object variable or With block variable not set." And it's pointing to my ''Then'' statment. I know it's a lot of code but any help would be great. I don't even need the page setup or print preview to work, i'll worry about that later, i just want to make it able to print what's on the screen.
[syntax="vbnet"]Imports System.Drawing.Printing
Public Class Form1 : Inherits System.Windows.Forms.Form
Dim TextBox1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
RichTextBox1.Text = "Testing the print phase"
'filling the richtextbox with some text that can be used read
End Sub
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MenuItem2.Click
If PrintDialog1.ShowDialog = DialogResult.OK Then
'showDialog method makes the dialog box visible at run time
PrintDocument1.Print()
End If
End Sub
Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MenuItem3.Click
Try
PrintPreviewDialog1.ShowDialog()
Catch es As Exception
MessageBox.Show(es.Message)
End Try
End Sub
Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MenuItem4.Click
With PageSetupDialog1
.PageSettings = PrintDocument1.DefaultPageSettings
End With
Try
If PageSetupDialog1.ShowDialog = DialogResult.OK Then
PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
End If
Catch es As Exception
MessageBox.Show(es.Message)
End Try
End Sub
Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MenuItem5.Click
Try
PrintPreviewControl1.Document = PrintDocument1
Catch es As Exception
MessageBox.Show(es.Message)
End Try
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Static intCurrentChar As Int32
Dim font As New Font("Verdana", 14)
Dim PrintAreaHeight, PrintAreaWidth, marginLeft, marginTop As Int32
With PrintDocument1.DefaultPageSettings
PrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
PrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right
marginLeft = .Margins.Left
marginTop = .Margins.Top
End With
If PrintDocument1.DefaultPageSettings.Landscape Then
Dim intTemp As Int32
intTemp = PrintAreaHeight
PrintAreaHeight = PrintAreaWidth
PrintAreaWidth = intTemp
End If
Dim intLineCount As Int32 = CInt(PrintAreaHeight / font.Height)
Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, PrintAreaWidth, PrintAreaHeight)
Dim fmt As New StringFormat(StringFormatFlags.LineLimit)
Dim intLinesFilled, intCharsFitted As Int32
e.Graphics.MeasureString(Mid(RichTextBox1.Text, intCurrentChar + 1), font, New SizeF(PrintAreaWidth, PrintAreaHeight), fmt, intCharsFitted, intLinesFilled)
e.Graphics.DrawString(Mid(RichTextBox1.Text, intCurrentChar + 1), font, Brushes.Black, rectPrintingArea, fmt)
intCurrentChar += intCharsFitted
If intCurrentChar < TextBox1.Text.Length Then 'ERROR ON THIS LINE!!
e.HasMorePages = True
Else
e.HasMorePages = False
intCurrentChar = 0
End If
End Sub
End Classfeyd | Please use[/syntax]
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]