views:

57

answers:

2

i have a winform, i want to print all the available data on the winform, lets say i have form full of labels, how to print it.

A: 

Psuedo

string printString = "";    
foreach(Label lbl in this.Controls){
     printString += lbl.Text + "\n";
}

Print(printString);
Erik
dude this lags the formating :(
Junaid Saeed
+2  A: 

The following code sample is from How to: Print a Windows Form (MSDN), found in a SO question titled "Print a Winform/visual element", which is on the first page of the search results for "winforms printing":

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;

public class Form1 : Form
{
    private Button printButton = new Button();
    private PrintDocument printDocument1 = new PrintDocument();

    public Form1()
    {
        printButton.Text = "Print Form";
        printButton.Click += printButton_Click;
        printDocument1.PrintPage += printDocument1_PrintPage;
        this.Controls.Add(printButton);
    }

    void printButton_Click(object sender, EventArgs e)
    {
        CaptureScreen();
        printDocument1.Print();
    }

    Bitmap memoryImage;

    private void CaptureScreen()
    {
        Graphics myGraphics = this.CreateGraphics();
        Size s = this.Size;
        memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
        memoryGraphics.CopyFromScreen(Location.X, Location.Y, 0, 0, s);
    }

    private void printDocument1_PrintPage(System.Object sender,  
           System.Drawing.Printing.PrintPageEventArgs e)
    {
        e.Graphics.DrawImage(memoryImage, 0, 0);
    }

    public static void Main()
    {
        Application.Run(new Form1());
    }
}
Jørn Schou-Rode
printDocument1.PrintPage += printDocument1_PrintPage;could you please explaint what is this "printDocument1_PrintPage"
Junaid Saeed
It's a method declared somewhere below in the code sample. The `+=` attaches it as an event handler to the `PrintPage` event. In the original sample, more verbose syntax is used.
Jørn Schou-Rode