How do I print out the specific data record from a windows form?

My goal is to be able toprint out a row of data that is selected in a windows form. I am using Visual Studio 2005 Express to create the project and a SQL 2005 express database as the data source. The form displays the records in a detail view (i.e. text boxes)

I would like to create a basic "report" or other printable document from the selected record and preview it in PrintPreviewDiaologue prior to sending it to the printer. I would like to format the output data in a columnar style and not a datagrid format. I have always used Access to build similar projects and would like emulate the same functions in a windows application.

I apologize if my terminology is too poor or the question to vague but I am just teaching myself to write windows application using tutorials and help files, etc on the net. I may even be over simplifing the issue as I know Access has a lot of built in functions that make this easy for a non-programmer.

If any one can advise it would be greatly appreciated!

[1603 byte] By [waiverider] at [2008-1-7]
# 1

waiverider,

Since you would like to format the output data in a columnar style and preview in PrintPreviewDialog control, I suggest you to use Treeview control with the TreeNodes on showing the columnar style data.

The following code example demonstrates the PrintPreviewDialog setting the Document and UseAntiAlias properties. The example assumes the form contains a TreeView named TreeView1 that contains TreeNode objects. The Tag property of each TreeNode object must be set to a fully qualified document name that can be accessed by the machine running the example. Set each TreeNode.Text property to a string that identifies the file specified by the TreeNode.Tag property. For example, you could set TreeNode1.Tag to "c:\myDocuments\recipe.doc" and TreeNode1.Text to "recipe.doc". The example also assumes the form contains a PrintPreviewDialog named PrintPreviewDialog1 and a button named Button1. To run this example, call the InitializePrintPreviewDialog method in the form's constructor or Load event handler.

Code Snippet

' Declare the dialog.

Friend WithEvents PrintPreviewDialog1 As PrintPreviewDialog

' Declare a PrintDocument object named document.

Private WithEvents document As New System.Drawing.Printing.PrintDocument

' Initalize the dialog.

Private Sub InitializePrintPreviewDialog()

' Create a new PrintPreviewDialog using constructor.

Me.PrintPreviewDialog1 = New PrintPreviewDialog

'Set the size, location, and name.

Me.PrintPreviewDialog1.ClientSize = New System.Drawing.Size(400, 300)

Me.PrintPreviewDialog1.Location = New System.Drawing.Point(29, 29)

Me.PrintPreviewDialog1.Name = "PrintPreviewDialog1"

' Set the minimum size the dialog can be resized to.

Me.PrintPreviewDialog1.MinimumSize = New System.Drawing.Size(375, 250)

' Set the UseAntiAlias property to true, which will allow the

' operating system to smooth fonts.

Me.PrintPreviewDialog1.UseAntiAlias = True

End Sub

Private Sub Button1_Click(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles Button1.Click

If Not (TreeView1.SelectedNode Is Nothing) Then

' Set the PrintDocument object's name to the selectedNode

' object'stag, which in this case contains the

' fully-qualified name of the document. This value will

' show when the dialog reports progress.

document.DocumentName = TreeView1.SelectedNode.Tag

End If

' Set the PrintPreviewDialog.Document property to

' the PrintDocument object selected by the user.

PrintPreviewDialog1.Document = document

' Call the ShowDialog method. This will trigger the document's

'PrintPage event.

PrintPreviewDialog1.ShowDialog()

End Sub

Private Sub document_PrintPage(ByVal sender As Object, _

ByVal e As System.Drawing.Printing.PrintPageEventArgs) _

Handles document.PrintPage

' Insert code to render the page here.

' This code will be called when the PrintPreviewDialog.Show

' method is called.

' The following code will render a simple

' message on the document in the dialog.

Dim text As String = "In document_PrintPage method."

Dim printFont As New System.Drawing.Font _

("Arial", 35, System.Drawing.FontStyle.Regular)

e.Graphics.DrawString(text, printFont, _

System.Drawing.Brushes.Black, 0, 0)

End Sub

Hope that can help you.
BrunoYu-MSFT at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

Thank you for your response and all the code.

I am still not sure that this is the answer for the question I have. This seems to work great if i want to return file names in a tree format , however, what I am trying to do is take the data from a form that has controls such as text boxes and display the currently selected record, from a SQL database, on a printable document of form.

i.e. lets say i have a contact table and a windows form that displays each record of a the table as a columnar layout. I have fields such as first and last names, phone number, email address, address, city, state and zip. I want to print out the data from the record (dislayed in these controls on the form) in a report or text file. How do I get the data from the form controls to a printable format and then print this format?

Thanks again!

waiverider at 2007-10-2 > top of Msdn Tech,Visual Basic,Visual Basic General...