Hello Friends,
I am developing an application where i need to take print.
every thing is working good except that printdialog appears two times.
CODE
PrintDocument printDocument1 = new PrintDocument();
AT FORM LOAD
this.printDocument1.PrintP
age += new PrintPageEventHandler(prin
tDocument1
_PrintPage
);
void printDocument1_PrintPage(o
bject sender, PrintPageEventArgs e)
{
try
{
e.Graphics.DrawImage(memor
yImage, 0, 0);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message
, "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
Function:
private delegate void PrintBarcodeDeligate(objec
t state);
public void PrintBarcode(object state)
{
try
{
if (tableLayoutPanel1.InvokeR
equired)
{
tableLayoutPanel1.Invoke(n
ew PrintBarcodeDeligate(Print
Barcode), new object[] { state }); // Invoke the called method through Message-Loop
}
else
{
if (TextBoxBarcode.Text.Trim(
) == "" && listBoxtextfile.Items.Coun
t < 1)
{
MessageBox.Show("Sorry there is no barcode to print.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
PrintDialog myPrintDialog = new PrintDialog();
if (TextBoxBarcode.Text.Trim(
).Length < 3 && listBoxtextfile.Items.Coun
t < 1)
{
MessageBox.Show("Barcode length should be minimum 3 characters.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
if (listBoxtextfile.Items.Cou
nt < 1)
{
int count = Application.OpenForms.Coun
t;
for (int i = 0; i < count; i++)
{
memoryImage = new System.Drawing.Bitmap(pane
l1.Width, panel1.Height);
panel1.DrawToBitmap(memory
Image, panel1.ClientRectangle);
if (myPrintDialog.ShowDialog(
) == DialogResult.OK)
{
myPrintDialog.Document = printDocument1;
printDocument1.PrintContro
ller = new StandardPrintController();
printDocument1.Print();
}
printDocument1.Dispose();
}
}
else
{
//PrintDialog myPrintDialog = new PrintDialog();
int count = Application.OpenForms.Coun
t;
for (int i = 0; i < count; i++)
{
if (myPrintDialog.ShowDialog(
) == DialogResult.OK)
{
for (int k = 0; k <= listBoxtextfile.Items.Coun
t - 1; k++)
{
labelBarcode.Text = "*" + listBoxtextfile.Items[k].T
oString() + "*";
labelBarcodeTxt.Text = "DEMO VERSION";//listBoxtextfile
.Items[k].
ToString()
;
memoryImage = new System.Drawing.Bitmap(pane
l1.Width, panel1.Height);
panel1.DrawToBitmap(memory
Image, panel1.ClientRectangle);
myPrintDialog.Document = printDocument1;
printDocument1.PrintContro
ller = new StandardPrintController();
printDocument1.Print();
}
}
printDocument1.Dispose();
}
}
}
}
}
}
catch (Exception ex)
{ }
}
AT Button CLick
try
{
Thread tPrintBarcode = new Thread(PrintBarcode);
tPrintBarcode.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message
, "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
Please help me.
Start Free Trial