如何仅打印填充的文本框及其附近的标签?

时间:2011-05-15 23:50:46

标签: vb.net

我正在尝试编写只能在表单中打印填充文本框的代码,然后在它们附近打印标签。 我搜索并认为下面的代码是我能达到的最终结果。 我正在使用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    

所以我不知道为什么我离解决方案这么远,也许我需要你们的帮助?

1 个答案:

答案 0 :(得分: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

如果您理解同样的情况,请告诉我。因为我刚刚给出了提示代码而不是完整的代码。