VB.NET Help!

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
erupt
Forum Commoner
Posts: 58
Joined: Sat Feb 25, 2006 10:24 am

VB.NET Help!

Post by erupt »

feyd | Please use

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 Class

feyd | 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]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

It's not the Then (a keyword cannot cause a runtime error), intCurrentChar is not an object, so it must be TextBox1.Text.Length
Is TextBox1.Text valid? (according to the error message it is not ;))

And please don't ask me how to test wether an object variable is valid or not in vb.net. My knowledge about visual basic is limited to "there is visual basic"
erupt
Forum Commoner
Posts: 58
Joined: Sat Feb 25, 2006 10:24 am

Post by erupt »

I'm such a jackass, Thanks Volka for pointing that out :D . I knew ''Then'' couldn't cause a runtime error and I can't believe i had Textbox.text instead of RichTextBox.text. I thought that would have thrown a syntax error or something. But thanks for helping me out with that. I'm sure I'll have more questions later :D
Post Reply