2011-05-15 36 views
1

我試圖編寫代碼,只能在窗體中打印填充的文本框,然後在附近打印標籤。 我搜索並認爲下面的代碼是我可以達到的最終結果。 我使用vb.net 2008如何僅打印填充文本框及其附近的標籤?

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage 

    '=============================================================================================== 
    Dim name As String 
    Dim cc As Integer 
    Dim f As New Font("Times New Roman", 14, FontStyle.Underline) 
    Dim b As New SolidBrush(Color.Black) 
    Dim c As Integer = 60 

    'search for filled textboxes 

    For Each ctrl In Me.TabControl1.TabPages(0).Controls 

     If (TypeOf ctrl Is TextBox) Then 

      If CType(ctrl, TextBox).Text.Length > 0 Then 
       e.Graphics.DrawString(CType(ctrl, TextBox).Text, f, b, 30, c) 
       c = c + 30 

       If c > 60 Then 
        Exit For 
       End If 
      End If 
     End If 

    Next 
    '====================================================== 
    'to know if previuos for loop actualy find any 
    If c > 60 Then 
     ' to be print over textboxes if are there 
     e.Graphics.DrawString(Label6.Text, f, b, 16, 30) 

    End If 
    '====================================================== 
    ' this one to find out the name of the control wich found filled 
    For Each ctrl In Me.TabControl1.TabPages(0).Controls 

     If (TypeOf ctrl Is TextBox) Then 

      If CType(ctrl, TextBox).Text.Length > 0 Then 
       name = ctrl.Name 
       e.Graphics.DrawString(name, f, b, 0, c) 

      End If 
     End If 
     Exit For 
    Next 
    'and this step to print the label with its textbox 
    If name = "TextBox2" Then 
     e.Graphics.DrawString(Label1.Text, f, b, 0, c) 

    ElseIf name = "TextBox3" Then 
     e.Graphics.DrawString(Label2.Text, f, b, 0, c) 

    ElseIf name = "textbox4" Then 
     e.Graphics.DrawString(Label3.Text, f, b, 0, c) 
    ElseIf name = "textbox5" Then 

     e.Graphics.DrawString(Label4.Text, f, b, 0, c) 
    End If 


End Sub  

所以我不知道爲什麼,我竟然離解,也許我需要你們的幫助不大?

回答

0

第一件事第一件事。編程是理解模式。

因此,如果您想解決您的問題,請先創建一個模式。

您的案例需要的模式是命名策略。

正確命名標籤和文本框。 例如。

lblFirstName txtFirstName 
lblLastName txtLastName 

如果你這樣做將會是代碼的改變。標籤不需要額外的循環。

Dim ControlStack As New Stack(Of TextBox) 

    For Each ctrl In Me.TabControl1.TabPages(0).Controls 'Modified to handle issue of taborder' 
     If (TypeOf ctrl Is TextBox) Then 
      ControlStack.Push(ctrl) 
     End If 
    Next 

    For Each ctrl In ControlStack 
     If (TypeOf ctrl Is TextBox) Then 

      If CType(ctrl, TextBox).Text.Length > 0 Then 
       name = ctrl.Name 
       dim labelName as string 
       labelName ="lbl" & mid(name ,4) 
       dim labelControl as Label 
       Dim controlsFound() As Control 
       controlsFound = Controls.Find(labelName,True) 
       If controlsFound.Length > 0 Then 
        labelControl = controlsFound(0) 
       End If 
       e.Graphics.DrawString(labelControl.Text , f, b, 0, c) 
       e.Graphics.DrawString(ctrl.Text, f, b, 250, c) 
      End If 
     End If 
     Exit For 
    Next 

請讓我知道你是否理解相同。正如我剛纔給出的提示代碼不完整的代碼。

+0

那就是briliant sachin我只是。 cuold'nt修復了「dim labelControl中的問題」,如Label = Controls.Find(labelName,True)「行其說法」類型'System.Windows.Forms.Control'的一維數組類型的值無法轉換爲'System。 Windows.Forms.Label'「除此之外,我認爲這很好 – 2011-05-16 10:49:02

+0

只是更新了代碼。實際上找到返回一個數組。所以你必須採取相同的第0個元素。現在就試試。 – 2011-05-16 12:17:09

+0

其工作非常感謝許多人,你只是讓我脫離深刻的問題 – 2011-05-16 13:26:59