I’m trying to write code that can print only filled text boxes in the form and then print the label near them.
I searched and thought the code below is the end result I could reach.
I am using 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
So I don’t know why I went so far away from the solution, maybe I need a little help from you guys?
First thing first. Programming is understanding patterns.
So create a pattern first if you want to solve your problem.
The pattern required in your case is the naming strategy.
Just name the label and textbox properly.
eg.
If you do so following will be the change in code. You don’t need a extra loop for Labels.
Please let me know if you understand the same. As I have just given hint code not complete code.