Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I'm not able find any post related to my problem.

I have an application in which i have many labels and text-boxes and a menu strip which contents new,open,save,print options.

After compiling my application , when i click on print it'll show me preview and then i can make a print but in preview for first time it is showing me the active form with only labels and few text-boxes and for second time it is showing me the same if i continue like that finally it is showing me the active form with all the fields after many trials.

I'm unable to understand where is the problem occurring in the code.If anyone knows about this or have seen such problem please help me.
Posted
Updated 28-Mar-14 0:31am
v3

1 solution

Seriously? Don't use PrintForm at all: use the PrintDocument class[^] instead. It's a bit more work, but it gives a much better result, and you have total control over exactly what is printed and where. And it's pretty simple when you get the hang of it.

The link includes a simple example.

[edit]Typos - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Member 10700515 29-Mar-14 0:26am    
Hey i have used Print Document itself and the link which you have provided, in that i have some problem like i need to specify the path but i don't want that i want to print the active form. Here is my code used for print:
protected override void OnPaint(PaintEventArgs e)
{
DrawIt(e.Graphics);
}
private Bitmap _memoryImage;

private void DrawIt(Graphics G)
{
PrintDocument document = new PrintDocument();
document.PrintPage += new PrintPageEventHandler(printDocument2_PrintPage);
using (var myGraphics = CreateGraphics())
{
var s = Size;
_memoryImage = new Bitmap(2000, 1000, myGraphics);
using (var memoryGraphics = Graphics.FromImage(_memoryImage))
{
printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("PaperA4", 950, 1250);
memoryGraphics.CopyFromScreen(Location.X, Location.Y, 70, 30, s);
}
}
}

private void printDocument2_PrintPage(object sender, PrintPageEventArgs e)
{
// calculate width and height scalings taking page margins into account
var wScale = e.MarginBounds.Width / (float)_memoryImage.Width;
var hScale = e.MarginBounds.Height / (float)_memoryImage.Height;

// choose the smaller of the two scales
var scale = wScale < hScale ? wScale : hScale;

// apply scaling to the image
e.Graphics.ScaleTransform(scale, scale);

// print to default printer's page
e.Graphics.DrawImage(_memoryImage, 70, 30);
printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("PaperA4", 950, 1250);
}


private void pd_Print(object sender, PrintPageEventArgs e)
{
DrawIt(e.Graphics);
}
private void mnu_Click(object sender, EventArgs e)
{
Ps.ShowDialog();
Pd.DefaultPageSettings = Ps.PageSettings;
Pd.PrinterSettings = Ps.PrinterSettings;
}

private System.Drawing.Printing.PrintDocument document =
new System.Drawing.Printing.PrintDocument();



private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
// Create a new PrintPreviewDialog using constructor.
this.Pv = new PrintPreviewDialog();

//Set the size, location, and name.
this.Pv.ClientSize =
new System.Drawing.Size(400, 300);
this.Pv.Location =
new System.Drawing.Point(29, 29);
this.Pv.Name = "PrintPreviewDialog1";

// Associate the event-handling method with the
// document's PrintPage event.
this.document.PrintPage +=
new System.Drawing.Printing.PrintPageEventHandler
(printDocument2_PrintPage);

// Set the minimum size the dialog can be resized to.
this.Pv.MinimumSize =
new System.Drawing.Size(375, 250);



// Set the UseAntiAlias property to true, which will allow the
// operating system to smooth fonts.
this.Pv.UseAntiAlias = true;


Pv.Document = document;
Pv.ShowDialog();
//if (Pr.ShowDialog() == DialogResult.OK)
Pd.Print();
}

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{

}

please try to help me...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900