Printing a Multi-page Listview - c#

Ok, I have got a printDocument1_PrintPage method which is supposed to print out the contents of a ListView. During execution, the method only prints out the first page. Could someone help me to printout the remaining page(s)?
I have identified the end of the first page via the variable ypos and set the property e.HasMorePages = true.
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e){
Graphics graphics = e.Graphics;
int ypos = 78;
Font f1 = new Font("Arial", 14, FontStyle.Bold, GraphicsUnit.Pixel);
Brush brush = new SolidBrush(Color.LightSlateGray);
graphics.FillRectangle(brush, new Rectangle(10, 10, 770, 50));
Pen blackpen = new Pen(Color.Black);
Pen graypen = new Pen(Color.LightGray);
Pen redpen = new Pen(Color.Red);
graphics.DrawRectangle(blackpen, new Rectangle(10, 10, 770, 50));
Brush B = new SolidBrush(listView1.ForeColor);
graphics.DrawLine(blackpen, 10, 10, 10, 1132);
graphics.DrawString("FORENAME", f1, Brushes.Black, new Point(20, 25));
graphics.DrawLine(blackpen, 130, 10, 130, 1132);
graphics.DrawString("SURNAME", f1, Brushes.Black, new Point(140, 25));
graphics.DrawLine(blackpen, 290, 10, 290, 1132);
graphics.DrawString("EXT.", f1, Brushes.Black, new Point(300, 25));
graphics.DrawLine(blackpen, 380, 10, 380, 1132);
graphics.DrawString("JOB TITLE", f1, Brushes.Black, new Point(410, 25));
graphics.DrawLine(blackpen, 780, 10, 780, 1132);
int[] X = { 15, 140, 300, 390, 720 };
int Y = 60;
f1 = listView1.Font;
for (int I = 0; I < listView1.Items.Count; I++){
for (int J = 0; J < listView1.Items[I].SubItems.Count - 1; J++){
graphics.DrawString(listView1.Items[I].SubItems[J].Text, f1, B, X[J], Y);
}
Y += f1.Height;
graphics.DrawLine(graypen, 10, ypos, 780, ypos);
ypos = ypos + 17;
if (ypos > 1132){
e.HasMorePages = true;
return;
}
}
graphics.Dispose();
}

Related

How to print data to next page in C# PrintDocument

I have written this code to print a lab report. It works fine but when I click on the print button and if the data of the report is more than one page it doesn't create the next page. Now I want the multiple-page setting I am totally confused about how to do it.
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int position = 60;
for (int j = NoOfItemsTill; j < types.Count; j++)
{
e.Graphics.DrawString(types[j], new Font("Arial", 16, FontStyle.Bold), Brushes.Black, new Point(30, yposition - 30 + position));
e.Graphics.DrawImage(imae, 33, yposition + position, imae.Width, imae.Height);
e.Graphics.DrawString("Test", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(40, yposition + 5 + position));
e.Graphics.DrawString("Normal Value", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(310, yposition + 5 + position));
e.Graphics.DrawString("Result ", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(560, yposition + 5 + position));
if (checkBox1showprise.Checked == true)
{
e.Graphics.DrawString("Price ", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(700, yposition + 5 + position));
}
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells[1].Value.ToString() == types[j])
{
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[2].Value.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(30, yposition + 50 + position));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[3].Value.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(310, yposition + 50 + position));
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[4].Value.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(560, yposition + 50 + position));
if (checkBox1showprise.Checked == true)
{
e.Graphics.DrawString(dataGridView1.Rows[i].Cells[5].Value.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(700, yposition + 50 + position));
}
position += 25;
}
}
yposition = yposition + 100;
}
// reset vaerables
// NoOfItemPerpage = 0;
NoOfItemsTill = 0;
}
I want that after every 5 elements of my array, the next page is created and the data is printed onto the next page.

Print data from several lists to several pages through PrintPage in C#

I have to print the data and results of my application through the PrintPage event. The problem is that I have the data and results to print on List elements, my first group of List is printed correctly through a While cycle; but when I add another While to print another group, this starts printing on the same pages printed above, not over writing the pages, but, which starts the next printing on the margins left in the first. Annex fragment of my code.
void PrintDocument1PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
owner = (MainForm)this.Owner;
pcombo = owner.stadoCarga.pcombo;
printDocument1.PrintController.IsPreview.ToString();
printDocument1.DefaultPageSettings.Margins.Left = 60;
printDocument1.DefaultPageSettings.Margins.Right = 60;
printDocument1.DefaultPageSettings.Margins.Top = 240;
printDocument1.DefaultPageSettings.Margins.Bottom = 60;
Font fte = new Font("verdana", 11, FontStyle.Regular);
Font ftti = new Font("verdana", 11, FontStyle.Bold);
Font ft1 = new Font("verdana", 10, FontStyle.Regular);
Font Fgr = new Font("Impact", 22);
SolidBrush br = new SolidBrush(Color.Black);
Single xPos = e.MarginBounds.Left;
Single yPos = 220; //e.MarginBounds.Top;
Single hight = e.MarginBounds.Height;
Single incLine = 25;
int printHeight;
printHeight = e.MarginBounds.Height - printDocument1.DefaultPageSettings.Margins.Bottom -
printDocument1.DefaultPageSettings.Margins.Top;
int r = 0;
frmDiagr MSCh = new frmDiagr();
frmInfo picLogo = new frmInfo();
LineasPagina = Convert.ToInt32(printHeight / fte.GetHeight(e.Graphics));
e.Graphics.DrawImage(picLogo.picLogo.Image, 60, 50);
e.Graphics.DrawString("ADIMA v2.0 Pro", Fgr, Brushes.Green, 230, 50);
e.Graphics.DrawString("Análisis y Diseño de Muros de Mampostería", ftti, Brushes.Green, 230, 87);
e.Graphics.DrawString("Proyecto.:", ft1, Brushes.Black, 60, 120);
e.Graphics.DrawString(owner.datos.DatosInfo[0], ft1, Brushes.Black, 140, 120);
e.Graphics.DrawString("Cliente.:", ft1, Brushes.Black, 510, 120);
e.Graphics.DrawString(owner.datos.DatosInfo[1], ft1, Brushes.Black, 575, 120);
e.Graphics.DrawString("Ingeniero.:", ft1, Brushes.Black, 60, 140);
e.Graphics.DrawString(owner.datos.DatosInfo[2], ft1, Brushes.Black, 140, 140);
e.Graphics.DrawString("Codigo.:", ft1, Brushes.Black, 510, 140);
e.Graphics.DrawString(owner.datos.DatosInfo[4], ft1, Brushes.Black, 575, 140);
// e.Graphics.DrawString("___________________________________________________________________", fte, Brushes.Black, 50, 180)
e.Graphics.DrawString("DATOS PARA EL DISEÑO", ftti, Brushes.Black, 60, 170);
e.Graphics.DrawString("- REFUERZOS UNIFORME DE LA SECCION DE MURO", ftti, Brushes.Black, 295, 170);
e.Graphics.DrawString("MURO No.", fte, Brushes.Black, 60, 210);
e.Graphics.DrawString("LONG.(m)", fte, Brushes.Black, 170, 210);
e.Graphics.DrawString("ESP.(m)", fte, Brushes.Black, 270, 210);
e.Graphics.DrawString("ALT(m)", fte, Brushes.Black, 360, 210);
e.Graphics.DrawString("f'm(kg/c2)", fte, Brushes.Black, 440, 210);
e.Graphics.DrawString("Em(kg/c2)", fte, Brushes.Black, 540, 210);
e.Graphics.DrawString("Ev(kg/c2)", fte, Brushes.Black, 640, 210);
e.Graphics.DrawString("PV(to/m3)", fte, Brushes.Black, 740, 210);
int stop = contador + LineasPagina;// Contador mas cantidad de lineas por pagina
if(stop > ak.Count){
stop = ak.Count;
}
while (contador < stop) {
int k = Convert.ToInt32(ak[contador]);
r = Convert.ToInt32(ak[contador])+1;
e.Graphics.DrawString(""+r, fte, Brushes.Black, 80, yPos + incLine);
e.Graphics.DrawString(muroToDiseno[k].ToString(), fte, Brushes.Black, 180, yPos + incLine);
e.Graphics.DrawString(owner.datosDibujo.espMuro[k].ToString(), fte, Brushes.Black, 280, yPos + incLine);
e.Graphics.DrawString(owner.datos.Datos[3], fte, Brushes.Black, 370, yPos + incLine);
e.Graphics.DrawString(owner.datos.Datos[7], fte, Brushes.Black, 460, yPos + incLine);
e.Graphics.DrawString(owner.datos.Datos[8], fte, Brushes.Black, 550, yPos + incLine);
e.Graphics.DrawString(txtEsfEv.Text, fte, Brushes.Black, 650, yPos + incLine);
e.Graphics.DrawString(owner.datos.Datos[12], fte, Brushes.Black, 750, yPos + incLine);
incLine += 25;
contador++;
}
PaginaNumero += 1;
e.HasMorePages = contador < ak.Count;
// Hasta Aqui se Imprime Correctamente....
int stopC = count + LineasPagina;// Contador mas cantidad de lineas por pagina
if(stopC > ak.Count){
stopC = ak.Count;
}
for (int i = 0; i < sumaFactorizada.Count; i++) {
while(count < stopC){
int k = Convert.ToInt32(ak[count]);
r = Convert.ToInt32(ak[count])+1;
e.Graphics.DrawString(""+r, fte, Brushes.Black, 80, yPos + incLine);
e.Graphics.DrawString(Math.Round(sumaFactorizada[i][k],3).ToString(), fte, Brushes.Black, 180, yPos + incLine);
e.Graphics.DrawString(EQFactorizada[i][k].ToString(), fte, Brushes.Black, 260, yPos + incLine);
incLine += 25;
count++;
}
PaginaNumero += 1;
e.HasMorePages = count < ak.Count;
}
}

Print data grid view data in two printer?

I want to print some data in printer 1 and some data in printer 2.
public void printkot()
{
PrintDialog pd = new PrintDialog();
PrintDocument pdoc = new PrintDocument();
PrinterSettings ps = new PrinterSettings();
Font font = new Font("Arial", 12);
PaperSize psize = new PaperSize("Custome", 314, 500);
pd.Document = pdoc;
for (int ch = 0; dataGridView1.Rows.Count > ch; ch++)
{
if (dataGridView1.Rows[ch].Cells["calkot_print"].Value.ToString() == "K1")
{
pdoc.PrinterSettings.PrinterName = "KOT";
pdoc.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
}
}
pd.Document.DefaultPageSettings.PaperSize = psize;
pdoc.DefaultPageSettings.PaperSize.Height = 500;
pdoc.DefaultPageSettings.PaperSize.Width = 314;
if (pdoc.PrinterSettings.IsValid)
{
pdoc.Print();
}
else
{
MessageBox.Show("Printer is invalid.");
}
}
private void printDocument1_PrintPage2(object sender, PrintPageEventArgs e)
{
string table_no;
table_no = lbltable.Text;
float xs = 10;
float ys = 5;
float widths = 285.0F; // max width I found through trial and error
float heights = 0F;
DataTable dtm = blu.checkbusiness();
Font drawFontArial12Bold = new Font("Arial", 12, FontStyle.Bold);
Font drawFontArial10Regular = new Font("Arial", 9, FontStyle.Regular);
Font drawFontArial10Regularsmall = new Font("Arial", 6, FontStyle.Regular);
SolidBrush drawBrush = new SolidBrush(Color.Black);
Pen drawingPen = new Pen(Color.Black, 1);
// Set format of string.
StringFormat drawFormatCenter = new StringFormat();
drawFormatCenter.Alignment = StringAlignment.Center;
StringFormat drawFormatLeft = new StringFormat();
drawFormatLeft.Alignment = StringAlignment.Near;
StringFormat drawFormatRight = new StringFormat();
drawFormatRight.Alignment = StringAlignment.Far;
StringFormat drawFormatRightlest = new StringFormat();
string business_name = dtm.Rows[0]["business_name"].ToString();
string address = dtm.Rows[0]["address"].ToString();
Graphics gra = e.Graphics;
String strDate = DateTime.Now.ToLongTimeString();
gra.DrawString(strDate, new System.Drawing.Font("Arial", 7, FontStyle.Regular), new SolidBrush(System.Drawing.Color.Black), 10, 60);
e.Graphics.DrawString(business_name, drawFontArial12Bold, drawBrush, new RectangleF(xs, ys, widths, heights), drawFormatCenter);
ys += e.Graphics.MeasureString(business_name, drawFontArial12Bold).Height;
e.Graphics.DrawString(address, drawFontArial10Regular, drawBrush, new RectangleF(xs, ys, widths, heights), drawFormatCenter);
ys += e.Graphics.MeasureString(address, drawFontArial10Regular).Height;
gra.DrawString("Table No::", new System.Drawing.Font("Arial", 9, FontStyle.Regular), new SolidBrush(System.Drawing.Color.Black), 190, 60);
gra.DrawString(table_no, new System.Drawing.Font("Arial", 9, FontStyle.Regular), new SolidBrush(System.Drawing.Color.Black), 250, 45);
gra.DrawLine(drawingPen, 10, 75, 309, 75);
gra.DrawString("Item", new System.Drawing.Font("Arial", 9, FontStyle.Bold), new SolidBrush(System.Drawing.Color.Black), 30, 75);
gra.DrawString("Qty", new System.Drawing.Font("Arial", 9, FontStyle.Bold), new SolidBrush(System.Drawing.Color.Black), 220, 75);
gra.DrawLine(drawingPen, 10, 90, 309, 90);
int y;
y = 95;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
string item_name = dataGridView1.Rows[i].Cells["cal_item_name"].Value.ToString();
string quantity = dataGridView1.Rows[i].Cells["cal_qty"].Value.ToString();
if (dataGridView1.Rows[i].Cells["calkot_print"].Value.ToString() == "K2")
{
gra.DrawString(item_name.ToString(), new System.Drawing.Font("Time New Roamn", 9, FontStyle.Regular), new SolidBrush(System.Drawing.Color.Black), 30, y);
gra.DrawString(quantity, new System.Drawing.Font("Time New Roamn", 9, FontStyle.Regular), new SolidBrush(System.Drawing.Color.Black), 220, y);
y = y + 15;
}
}
int z = y + 20;
gra.DrawLine(drawingPen, 10, z, 309, z);
gra.DrawString("Description", new System.Drawing.Font("Time New Roamn", 9, FontStyle.Bold), new SolidBrush(System.Drawing.Color.Black), 10, z + 5);
gra.DrawString(txtrichbox.Text, new System.Drawing.Font("Time New Roamn", 7, FontStyle.Regular), new SolidBrush(System.Drawing.Color.Black), 10, z + 20);
z = y + 20;
}

Mouse clicks on the created regions in Venn diagram

I have created a venn diagram which looks like this,
I have also created regions for handling clicks on each part of venn. Left / Right and common two parts. Here is the code I have,
private void panelControlVennDiagram_Paint(object sender, PaintEventArgs e)
{
Rectangle leftVenn = new Rectangle(20, 50, 130, 130);
Rectangle rightVenn = new Rectangle(60, 50, 130, 130);
commonRegion = new Region();
Pen pen = new Pen(Color.Black,3);
using (Brush brushLeft = new SolidBrush(leftVennColor))
{
ellipseLeftOnlyPath.AddEllipse(leftVenn);
leftOnlyRegion = new Region(ellipseLeftOnlyPath);
e.Graphics.FillEllipse(brushLeft, leftVenn);
e.Graphics.DrawEllipse(pen, leftVenn);
brushLeft.Dispose();
}
using (Brush brushRight = new SolidBrush(rightVennColor))
{
ellipseRightOnlyPath.AddEllipse(rightVenn);
rightOnlyRegion = new Region(rightVenn);
e.Graphics.FillEllipse(brushRight, rightVenn);
e.Graphics.DrawEllipse(pen, rightVenn);
brushRight.Dispose();
}
using (GraphicsPath circle_path = new GraphicsPath())
{
circle_path.AddEllipse(leftVenn);
commonRegion.Intersect(circle_path);
}
using (GraphicsPath circle_path = new GraphicsPath())
{
circle_path.AddEllipse(rightVenn);
commonRegion.Intersect(circle_path);
}
leftOnlyRegion.Exclude(commonRegion);
rightOnlyRegion.Exclude(commonRegion);
using (Brush brushCommon = new SolidBrush(commonColor))
{
e.Graphics.FillRegion(brushCommon, commonRegion);
brushCommon.Dispose();
}
using (Brush brushLeftOnly = new SolidBrush(leftRightCommonColor))
{
ellipseLeftRightCommonPath.AddEllipse(65, 80, 35, 40);
e.Graphics.FillEllipse(brushLeftOnly, 65, 80, 35, 40);
e.Graphics.DrawEllipse(pen, 65, 80, 35, 40);
brushLeftOnly.Dispose();
}
using (Brush brushRightOnly = new SolidBrush(rightLeftCommonColor))
{
ellipseRightLeftCommonPath.AddEllipse(105, 110, 35, 40);
e.Graphics.FillEllipse(brushRightOnly, 105, 110, 35, 40);
e.Graphics.DrawEllipse(pen, 105, 110, 35, 40);
brushRightOnly.Dispose();
}
Brush brush = new SolidBrush(Color.Black);
Font stringFont = new Font("Calibri", 9, FontStyle.Bold);
Font stringFontCommon = new Font("Calibri", 8, FontStyle.Bold);
e.Graphics.DrawString(leftValue.ToString(), stringFont, brush, 40, 90);
e.Graphics.DrawString(rightValue.ToString(), stringFont, brush, 160, 90);
e.Graphics.DrawString(leftRightValue.ToString(), stringFontCommon, brush, 70, 115);
e.Graphics.DrawString(rightLeftValue.ToString(), stringFontCommon, brush, 110, 115);
brush.Dispose();
stringFont.Dispose(); stringFontCommon.Dispose();
pen.Dispose();
}
This is to handle the mouse clicks. Basically I want to have a click handled only on left / right parts and not their intersection. But even with this I dont get the message box. Sometimes if I put a break point it does hit messagebox code but sometimes not.
private void panelControlVennDiagram_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
if (ellipseLeftRightCommonPath.IsVisible(e.Location))
MessageBox.Show("Left - Right Common");
else if (ellipseRightLeftCommonPath.IsVisible(e.Location))
MessageBox.Show("Right - Left Common");
else if (leftOnlyRegion.IsVisible(e.Location))
MessageBox.Show("Left Only");
else if (rightOnlyRegion.IsVisible(e.Location))
MessageBox.Show("Right Only");
}
}
Whats wrong here? Please help.
I used directly regions rather than using GraphicsPath.. It works .
leftOnlyRegion = new Region(leftVenn);

C# how to paint User Control with background Transparency

I'd like to know how to paint User Control with background Transparency even if the user control is raised or moved at runtime.
my code is
private void UserControl1_Paint(object sender, PaintEventArgs e)
{
var g = e.Graphics;
g.Clear(Color.White);
g.SmoothingMode = SmoothingMode.HighQuality;//可以反锯齿
var rectBound = new Rectangle(0, 0, Width-1, Height-1);
var b = new SolidBrush(Color.FromArgb(0, 122, 204));
var rect = new Rectangle(2, 2, Width - 4, Height - 4);
if(!_isSelected)//FillRectangle
g.FillEllipse(b, rectBound);
else
g.FillEllipse(b, rect);
var pen = new Pen(Color.Yellow);
pen.DashStyle = DashStyle.DashDot;
g.DrawLine(pen,10,10,100,10);
pen.DashStyle = DashStyle.Dash;
g.DrawLine(pen, 10, 15, 100, 15);
pen.DashStyle = DashStyle.DashDotDot;
g.DrawLine(pen, 10, 20, 100, 20);
pen.DashStyle = DashStyle.Dot;
g.DrawLine(pen, 10, 25, 100, 25);
pen.DashStyle = DashStyle.Solid;
g.DrawLine(pen, 10, 30, 100, 30);
if (_isSelected)
{
pen = new Pen(Color.Black) { DashStyle = DashStyle.Dot, Width = 1 };
g.DrawRectangle(pen, rectBound);
}
}
and demo picture here
there will be a lot of User Controls Created at runtime,some of them maybe overlapped,then it should be clear the user control's background.how to do that?

Categories