2014-09-23 59 views
1

我正在處理用戶輸入JobNumber(例如J000001)的項目,並且當用戶打印時打印作業編號。使用下面的代碼,我可以打印數字,例如(001),但我希望用戶輸入實際的JobNumber(J000001)。任何幫助,這是非常感謝。在WPF VB中打印來自用戶的輸入

當我進入jobnumber可以(J000001),我收到以下錯誤信息:

'無效CastException是未處理的' 從字符串 「J000001」
轉換爲boolean類型是無效的。

下面是我的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> 

回答

1

問題是您在Button_Click方法中的If聲明。

If txtStart.Text Then 

If需要一個布爾值,但是你傳遞一個字符串。 VB.Net試圖將字符串轉換爲布爾值。 001的作品,因爲它可以轉換。 J000001無法轉換爲布爾值。

您的意思是檢查是否有值輸入?

If !String.IsNullOrWhiteSpace(txtStart.Text) Then 

將值分配給totalnumber時,您也會遇到類似的問題。

+0

@ a_hardin-現在我只想打印用戶輸入。你能幫我一下嗎?謝謝 – LearningMacro 2014-09-23 18:30:39

0

您的文本框中的值進行比較,作爲一個布爾值,然後指定你的文本框的值到Integer類型的變量。

If txtStart.Text Then 

大概應該是這樣的:

If !String.IsNullOrEmpty(txtStart.Text) Then 

,然後如果你的工作號碼是字母數字,使用String類型,而不是整型變量。