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
所以我不知道爲什麼,我竟然離解,也許我需要你們的幫助不大?
那就是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個元素。現在就試試。 – 2011-05-16 12:17:09
其工作非常感謝許多人,你只是讓我脫離深刻的問題 – 2011-05-16 13:26:59