we use Print to print text to form in vb6
and dont know how to print text to window form in vb.net
please explain how to achieve it . . .

Recommended Answers

All 21 Replies

There a couple methods you can use:

PrintForm and PrintDocument

Printform sends an image of the active form to the printer. (Simple output)

PrintDocument sends the output to the printer, but you have to specify the output.

PrintDocument sounds like what you are wanting.

i didn't mean to print the hardcopy of the form but to put text to window form as we do in vb6 using Print

We don't do print to form in VB.Net, but I can suggest some objects like ReportViewer. In my case, I prefer using CrystalReport so I'm using CrystalReportViewer quite often, it show a copy of what you are going to print and even let you save your printing to .doc,.pdf or .xls.

i want to put text to Form and for this purpose i dont want to use label . . .

i dont want to use label

I don't know why not. It's so simple. You don't even have to name the label unless you want to modify the text after you place it.

Dim lbl As New Label
lbl.Font = New Font("Arial", 12.0!, FontStyle.Regular, GraphicsUnit.Point, CType(0, Byte))
lbl.Location = New Point(40, 50)
lbl.Text = "Sample Text"
Me.Controls.Add(lbl)

thanks for providing very interesting code
first i dont understand the following statement
GraphicsUnit.Point, CType(0, Byte)

so would you like to explain it .....

And Other thing is :-------
but still i need to learn how to put text to form without using controls so is there anyway by which i can achieve that ?

Without using any control the only way would be to draw an image and place the image in the background of the form.

If you do not plan to use the Graphics library then your question has met a dead end. :(

In the Graphics class you would want to use the DrawString method. But beware, this can get heavy in the beginning. But total control on how to draw 1e77a88c98bdb49f0ede61186e84f0ae a string is your reward. If you are looking to print just plain text, I should first try the suggestion of Reverend Jim.
In the example I just did two DrawStrings.

commented: Nice example! +8

lbl.Font = New Font("Arial", 12.0!, FontStyle.Regular, GraphicsUnit.Point, CType(0, Byte))

I just copied that line from the form1.Designer.vb file. GraphicsUnit.Point just specifies the size units. You could also use GraphicsUnit.Pixel (I don't see any difference). You might encapsulate the logic in a sub as follows:

Private Sub AddText(text As String, size As Double, xpos As Integer, ypos As Integer)

    Dim lbl As New Label
    lbl.AutoSize = True
    lbl.Font = New Font("Arial", size, FontStyle.Regular, GraphicsUnit.Pixel)
    lbl.Location = New Point(xpos, ypos)
    lbl.Text = text
    Me.Controls.Add(lbl)

End Sub

If you wanted you could add other parameters such as for the font type, colour, etc.

I just copied that line from the form1.Designer.vb file

i see form1.Designer.vb file but there is not code so how did you copy that line from that file.
please tell a little bit about it . . .
thanx

The formname.Designer.vb file contains the code that creates the controls that you place on your form at design time. Here is an example:

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1))
        Me.btnPrint = New System.Windows.Forms.Button()
        Me.pd = New System.Drawing.Printing.PrintDocument()
        Me.pp = New System.Windows.Forms.PrintPreviewDialog()
        Me.SuspendLayout()
        '
        'btnPrint
        '
        Me.btnPrint.Location = New System.Drawing.Point(13, 13)
        Me.btnPrint.Name = "btnPrint"
        Me.btnPrint.Size = New System.Drawing.Size(75, 23)
        Me.btnPrint.TabIndex = 0
        Me.btnPrint.Text = "Print"
        Me.btnPrint.UseVisualStyleBackColor = True
        '
        'pd
        '
        '
        'pp
        '
        Me.pp.AutoScrollMargin = New System.Drawing.Size(0, 0)
        Me.pp.AutoScrollMinSize = New System.Drawing.Size(0, 0)
        Me.pp.ClientSize = New System.Drawing.Size(400, 300)
        Me.pp.Enabled = True
        Me.pp.Icon = CType(resources.GetObject("pp.Icon"), System.Drawing.Icon)
        Me.pp.Name = "pp"
        Me.pp.Visible = False
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(284, 262)
        Me.Controls.Add(Me.btnPrint)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub
    Friend WithEvents btnPrint As System.Windows.Forms.Button
    Friend WithEvents pd As System.Drawing.Printing.PrintDocument
    Friend WithEvents pp As System.Windows.Forms.PrintPreviewDialog

End Class

I am not able to see this code in my application

so what to do to see this auto generated code...

I also double clicked on the form and the code window appears , there i found no source code for designed controls

Try looking in the project folder via Explorer. I am using Visual Studio 2010. You?

i visit MyProject Folder through Window Explorer and i found that there are few files like

Application.Designer.vb
Resource.Designer.vb
Setting.Designer.vb etc.

but there is not seperate designer file for each Form

I am using Visual Studio 2010. You?

well i am using Visual Studio 2008 and probably thats the reason i cant see particular designer file for each form.

If you can see you code editor window, do you also see a Toolbox window Solutution explorer window and a Properties window? If not open, open them through the View menu. VS 2008 and VS 2010 make no difference. In the Solution Explorer window you shoud see the files.

i also see solution explorer but there is not a file like form.designer.vb but there are files (form files also) when i double clicked on one of these files then selected form file appear with title form1.vb[Design] (when designing the form) and when coding for that form then title form1.vb

but no file i found where i can see the source of designed controls....

You are correct.
But try the following:
In the code editor there are two drop down lists at the top. In the right list choose InitializeComponent and you should see code for your designed labels,buttons etc.

But try the following:
In the code editor there are two drop down lists at the top. In the right list choose InitializeComponent and you should see code for your designed labels,buttons etc.

this helped to see the source of designed controls.

this thread is really bizarre thread (i asked for something and i got something other(but really important for beginners like me))

thanx for giving your important time to this discussion.

We are here to help, if we can. :o)

"You can't always get what you want but sometimes you get what you need." - Mick Jagger

Public Class Form1
Dim a() = {2, 5, 7, 9, 1, 4, 3}
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Label1.Text = "2 5 7 9 1 4 3"
    Dim bb, aa, b
    For bb = 1 To 5
        For aa = 0 To 4
            If a(aa) > a(aa + 1) Then
                b = a(aa)
                a(aa) = a(aa + 1)
                a(aa + 1) = b
                Label1.Text = Label1.Text & Chr(13) & a(0) & " " & a(1) & " " & a(2) & " " & a(3) & " " & a(4) & " " & a(5)
            End If
        Next
    Next
    End Sub
End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.