PDA

View Full Version : سوال: چاپ چند صفحه با استفاده از حلقه



modirforoosh
دوشنبه 06 شهریور 1396, 08:47 صبح
با سلام
من یه مشکلی دارم که امیدوارم اساتید کمک کنند
من یک صفحه دارم که باید یک اسم و کد در آن چاپ شود . از یک لیست باکس کدهای رکوردهای دیتابیس دریافت میشود و در یک حلقه در صفحه چاپ میشود . ولی الان مشکلی که دارم اینه که میخوام هر اسم وکد مربوط به هر رکورد در یک صفحه جداگانه چاپ شود . کد زیر را نوشتم اما همه در یک صفحه چاپ میشود .

private void button9_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
printDialog1.Document = pd;
if (printDialog1.ShowDialog()==DialogResult.OK)
{

pd.PrinterSettings = printDialog1.PrinterSettings;
pd.PrintPage += new PrintPageEventHandler(prtpage);
pd.Print();
}
}


private void prtpage(object sender, PrintPageEventArgs e)
{
int i, count;
count = listBox1.Items.Count;
for (i = 0; i < count; i++)
{








strformat.FormatFlags = StringFormatFlags.DirectionRightToLeft;


CN.Open();
CM.Connection = CN;
CM.CommandText = "select * from bongah where id='" + listBox1.Items[i].ToString() + "'";
SqlDataAdapter DA2 = new SqlDataAdapter(CM.CommandText, CN);
DataTable DT2 = new DataTable();
DA2.Fill(DT2);
e.Graphics.DrawString(DT2.Rows[0][1].ToString(), f1, Brushes.Black, 750, 15, strformat);
e.Graphics.DrawString("کد : " + DT2.Rows[0][0].ToString(), f1, Brushes.Black, 750, 35, strformat);
CN.Close();
e.HasMorePages = true;




}

Mahmoud Zaad
دوشنبه 06 شهریور 1396, 09:25 صبح
سلام
این صفحه (https://stackoverflow.com/questions/25069994/use-printdocument-to-print-each-row-in-datatable-to-seperate-page) رو نگاه کنید.
ضمنا شما 3 خط زیر رو از حلقه خارج کنید و تفاوت سرعت اجرا رو ببینید. هر بار نباید کانکشن باز و بسته بشه. به قول شاعر "هر بار این درو محکم نبند نرو"!

CN.Open(); CM.Connection = CN;
...
CN.Close();

modirforoosh
دوشنبه 06 شهریور 1396, 13:16 عصر
کد زیر را دارم که اعداد را در چند صفحه چاپ میکنه و قابل تنظیم است . چند ساعته باهاش کار میکنم که تغییرش بدم اما هر کاری میکنم نمیشه لطفا اساتید کمک کنند
میخوام گزینه های یک لیست باکس را طوری که هر گزینه در یک صفحه باشد چاپ کنم . چطور باید کد را تغییر بدم:گریه:


float currentY = 10;// declare one variable for height measurement
e.Graphics.DrawString("Print in Multiple Pages", DefaultFont, Brushes.Black, 10, currentY);//this will print one heading/title in every page of the document
currentY += 15;


while (totalnumber <= 50) // check the number of items
{
e.Graphics.DrawString(totalnumber.ToString(), DefaultFont, Brushes.Black, 50, currentY);//print each item
currentY += 20; // set a gap between every item
totalnumber += 1; //increment count by 1
if (itemperpage < 20) // check whether the number of item(per page) is more than 20 or not
{
itemperpage += 1; // increment itemperpage by 1
e.HasMorePages = false; // set the HasMorePages property to false , so that no other page will not be added


}


else // if the number of item(per page) is more than 20 then add one page
{
itemperpage = 0; //initiate itemperpage to 0 .
e.HasMorePages = true; //e.HasMorePages raised the PrintPage event once per page .
return;//It will call PrintPage event again


}
}