在WPF VB中打印用户输入

时间:2014-09-23 16:12:43

标签: wpf vb.net

我正在处理用户将输入JobNumber的项目,例如(J000001),当用户点击打印时,将打印作业编号。使用下面的代码,我可以打印数字,比如说(001),但我希望用户输入实际的JobNumber(J000001)。非常感谢任何帮助。

当我输入JobNumber(J000001)时,收到以下错误消息:

  

'无效的CastException未处理'
  从字符串“J000001”到类型布尔值的转换无效。

以下是我的VB代码:

Imports System.Globalization
Imports System.Drawing.Printing
Imports System.Drawing
Class MainWindow

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        AddHandler printDocument1.PrintPage, AddressOf printDocument1_PrintPage
    End Sub

    'Declaration  the  global  variables 
    Private paperSize As New PaperSize("papersize", 300, 500)
    'set the paper size 
    Private totalnumber As Integer = 0
    'this is for total number of items of the list or array
    Private itemperpage As Integer = 0
    'this is for no of item per page 
    Private printDocument1 As New PrintDocument()
    Private printDialog1 As New System.Windows.Forms.PrintDialog()
    Private DefaultFont As New Font("Calibri", 20)

    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
        If txtStart.Text Then
            itemperpage = 1
            totalnumber = txtStart.Text
            printDialog1.Document = printDocument1
            printDocument1.DefaultPageSettings.PaperSize = paperSize
            printDialog1.ShowDialog()

            'printDocument1.PrinterSettings.PrinterName = "";
            printDocument1.Print()
        Else
            MessageBox.Show("Invalid number")
        End If
    End Sub

    Private Function CheckNumber(str As String)
        Dim Num As Double
        Return Double.TryParse(str, Num)
    End Function

    'Define the  Printpage event of the printdocument 
    Private Sub printDocument1_PrintPage(sender As Object, e As System.Drawing.Printing.PrintPageEventArgs)
        Dim currentY As Single = 10
        While totalnumber <= CInt(txtStart.Text)
            ' check the number of items 
            e.Graphics.DrawString(totalnumber.ToString(), DefaultFont, System.Drawing.Brushes.Black, 50, currentY)


            'print each item
            currentY += 20
            ' set a gap between every item
            totalnumber += 1
            'increment count by 1
            If itemperpage < 1 Then
                ' check whether  the number of item(per page) is more than 1 or not
                itemperpage += 1
                ' increment itemperpage by 1
                ' set the HasMorePages property to false , so that no other page will not be added 
                e.HasMorePages = False
            Else

                ' if the number of item(per page) is more than 1 then add one page 
                itemperpage = 1
                'initiate itemperpage to 0 . 
                If totalnumber <= Convert.ToInt32(txtStart.Text) Then
                    e.HasMorePages = True
                End If
                'e.HasMorePages raised the PrintPage event once per page .           
                'It will call PrintPage event again
                Return
            End If
        End While
    End Sub
End Class

XAML代码

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="175" Width="303">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="1.5*" />
        </Grid.ColumnDefinitions>
        <Label Content="Start Number:" />
        <TextBox x:Name="txtStart" Grid.Column="1" />
        <Button Grid.Row="2" Grid.ColumnSpan="2" Content="Print" Click="Button_Click" />
    </Grid>
</Window>

2 个答案:

答案 0 :(得分:1)

问题在于If方法中的Button_Click声明。

If txtStart.Text Then

If接受一个布尔值,但是你传递一个字符串。 VB.Net正在尝试将字符串转换为布尔值。 001的作品,因为它可以转换它。 J000001无法转换为布尔值。

您的意思是检查是否输入了值?

If !String.IsNullOrWhiteSpace(txtStart.Text) Then

将值分配给totalnumber时,您也会遇到类似的问题。

答案 1 :(得分:0)

您正在将文本框的值作为布尔值进行比较,然后将文本框的值分配给Integer类型的变量。

If txtStart.Text Then

应该是这样的:

If !String.IsNullOrEmpty(txtStart.Text) Then

然后,如果您的作业数字是字母数字,请使用String类型的变量而不是Integer。