Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5847001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:39:23+00:00 2026-05-22T12:39:23+00:00

I’m trying to write code that can print only filled text boxes in the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:39:24+00:00Added an answer on May 22, 2026 at 12:39 pm

    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.

    lblFirstName txtFirstName 
    lblLastName txtLastName
    

    If you do so following will be the change in code. You don’t need a extra loop for Labels.

        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
    

    Please let me know if you understand the same. As I have just given hint code not complete code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.