如何在vb中创建打印按钮 [英] How to create a print button in vb

查看:158
本文介绍了如何在vb中创建打印按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助,我正在尝试创建一个按钮,从我的daatagridview打印我的信息。下面是我输入的代码,但是当我尝试它时,它给出一个n错误,错误是。 {对象引用未设置为对象的实例。} NullReferenceException未处理

 私有  Sub  PrintToolStripButton_Click(sender  As   Object ,e  As  EventArgs)句柄 PrintToolStripButton.Click 
PrintDialog1.ShowDialog()
PrintDialog1.Document = PrintDocument1
PrintDocument1.DocumentName = 出勤
PrintDocument1.Print()
结束 Sub

私人 Sub PrintDocument1_PrintPage(发件人作为 对象,e As Printing.PrintP ageEventArgs)句柄 PrintDocument1.PrintPage
Dim ColumnCount 正如 整数 = AttendanceDataGridView.ColumnCount
Dim RowCount 作为 整数 = AttendanceDataGridView.RowCount

Dim CellTopPos 作为 整数 = PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Top

对于行= 0 RowCount - < span class =code-digit> 1

Dim CellLeftPos As 整数 = PrintDocument1.PrinterSettings.DefaultPage Settings.Margins.Left

对于 Cell = 0 ColumnCount - 1

Dim CellValue As String = AttendanceDataGridView.Rows(Row).Cells(Cell).Value.ToString()
Dim CellWidth = AttendanceDataGridView.Rows(Row).Cells(Cell).Size.Width + 50
Dim CellHeight = AttendanceDataGridView.Rows(Row).Cells(Cell).Size.Height

Dim Brush As SolidBrush(Color.Black)
e.Graphics.DrawString(CellValue,字体( Century Gothic, 10 ),Brush,CellLeftPos,CellTopPos)
e.Graphics.DrawRectangle( Pens.Black,CellLeftPos,CellTopPos,CellWidth,CellHeight)

CellLeftPos + = CellWidth
Next

CellTopPos + = AttendanceDataGridView.Rows(Row).Cells( 0 )。Size.Height
Next
结束 Sub

解决方案

问题出现了,因为您尝试处理的某个单元格没有值,所以 .Value.ToString()电话是抛出异常。



为了解决这个问题,只需检查包含某些内容的单元格,然后再尝试使用。例如

 对于 Cell =  0   ColumnCount  -   1  

如果 AttendanceDataGridView.Rows(Row).Cells(Cell).Value 没有 然后
Dim CellValue 正如 String = AttendanceDataGridView.Rows(Row).Cells(Cell).Value.ToString()
Dim CellWidth = AttendanceDataGridView.Rows(Row).Cells(Cell).Size.Width + 50
Dim CellHeight = AttendanceDataGridView.Rows(Row).Cells(Cell).Size.Height

Dim Brush As SolidBrush(Color.Black)
e.Graphics.DrawString(CellValue,字体( Century Gothic 10 ),Brush,CellLeftPos,CellTopPos)
e.Graphics.DrawRectangle(Pens.Black,CellLeftPos,CellTopPos,CellWidth,CellHeight)

CellLeftPos + = CellWidth
结束 如果

下一步


hi please i need help on this , i am trying to create a button that will print my information from my daatagridview. below is the code i have inputed but when i try it it give a n error and the error is . {"Object reference not set to an instance of an object."} NullReferenceException unhandled

Private Sub PrintToolStripButton_Click(sender As Object, e As EventArgs) Handles PrintToolStripButton.Click
    PrintDialog1.ShowDialog()
    PrintDialog1.Document = PrintDocument1
    PrintDocument1.DocumentName = "Attendance"
    PrintDocument1.Print()
End Sub

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim ColumnCount As Integer = AttendanceDataGridView.ColumnCount
    Dim RowCount As Integer = AttendanceDataGridView.RowCount

    Dim CellTopPos As Integer = PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Top

    For Row = 0 To RowCount - 1

        Dim CellLeftPos As Integer = PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Left

        For Cell = 0 To ColumnCount - 1

            Dim CellValue As String = AttendanceDataGridView.Rows(Row).Cells(Cell).Value.ToString()
            Dim CellWidth = AttendanceDataGridView.Rows(Row).Cells(Cell).Size.Width + 50
            Dim CellHeight = AttendanceDataGridView.Rows(Row).Cells(Cell).Size.Height

            Dim Brush As New SolidBrush(Color.Black)
            e.Graphics.DrawString(CellValue, New Font("Century Gothic", 10), Brush, CellLeftPos, CellTopPos)
            e.Graphics.DrawRectangle(Pens.Black, CellLeftPos, CellTopPos, CellWidth, CellHeight)

            CellLeftPos += CellWidth
        Next

        CellTopPos += AttendanceDataGridView.Rows(Row).Cells(0).Size.Height
    Next
End Sub

解决方案

The problem is occurring because one of the cells you are trying to handle does not have a value so the .Value.ToString() call is throwing an exception.

To get around this just check for the cell containing something before attempting to use the Value. For example

For Cell = 0 To ColumnCount - 1

    If Not AttendanceDataGridView.Rows(Row).Cells(Cell).Value Is Nothing Then
        Dim CellValue As String = AttendanceDataGridView.Rows(Row).Cells(Cell).Value.ToString()
        Dim CellWidth = AttendanceDataGridView.Rows(Row).Cells(Cell).Size.Width + 50
        Dim CellHeight = AttendanceDataGridView.Rows(Row).Cells(Cell).Size.Height

        Dim Brush As New SolidBrush(Color.Black)
        e.Graphics.DrawString(CellValue, New Font("Century Gothic", 10), Brush, CellLeftPos, CellTopPos)
        e.Graphics.DrawRectangle(Pens.Black, CellLeftPos, CellTopPos, CellWidth, CellHeight)

        CellLeftPos += CellWidth
    End If

Next


这篇关于如何在vb中创建打印按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆