Michael1977

Hi all,

Can someone give me guidance on how to create a windows form (with 2 input boxes) & the user can choose to print 'From Label Number' & 'To Label Number'. Eg. Print only Label 27 to Label 30 out of the 100 labels.

Please see below link. (hope the link works..)

http://profile.imageshack.us/user/michael1977/images/detail/#120/printedstickerlabelck1.png

Thanks!

Michael.



Re: Windows Forms General printing labels from Windows Forms..

Eli Gazit

Based on your image you gave, you are looking from some kind of reporting tool.

Check out this link:

http://www.codeproject.com/csharp/ReportPrinting.asp






Re: Windows Forms General printing labels from Windows Forms..

JohnWein

Post your PrintDocument PrintPage code.





Re: Windows Forms General printing labels from Windows Forms..

Michael1977

Hi all,

Is it possible to use GDI+ namespaces (System.Drawing, etc.) to draw out the form, use variables for the text, and print from there

Something like below..

Code Block

public Form1()

{

InitializeComponent();

Graphics dc = this.CreateGraphics();

this.Show();

Pen bluePen = new Pen(Color.Blue, 3);

dc.DrawRectangle(bluePen, 0, 0, 50, 50);

}

Thanks all,

Michael.





Re: Windows Forms General printing labels from Windows Forms..

Yu Guo – MSFT

Hi, Michael1977,

It is really a good idea to use GDI+ to draw all the Text on the form and then print them from there, because it could be efficient.

I wrote a small example for you, say we have a button on form.

Code Block

string str1 = "Hello";

Point str1Location = new Point(50, 50);

string str2 = "World";

Point str2Location = new Point(50, 100);

Font font = new Font("Tahoma", 16);

public Form1()

{

InitializeComponent();

document = new PrintDocument();

document.PrintPage += new PrintPageEventHandler(document_PrintPage);

}

void document_PrintPage(object sender, PrintPageEventArgs e)

{

e.Graphics.DrawString(str1, font, Brushes.Black, str1Location);

e.Graphics.DrawString(str2, font, Brushes.Black, str2Location);

}

PrintDocument document;

private void button1_Click(object sender, EventArgs e)

{

document.Print();

}

private void Form1_Paint(object sender, PaintEventArgs e)

{

e.Graphics.DrawString(str1, font, Brushes.Black, str1Location);

e.Graphics.DrawString(str2, font, Brushes.Black, str2Location);

}

As you can see, you can do the same thing the Paint event and the PrintPage event.

http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.paint(VS.80).aspx

http://msdn2.microsoft.com/en-us/library/system.drawing.printing.printdocument.printpage.aspx

Hope this helps,

Regards






Re: Windows Forms General printing labels from Windows Forms..

Michael1977

Thanks all!

I managed to use GDI+ to draw out a simple form.. but how to i print it (after the user click on my custom control button "Print", the page is displayed on the screen and printed) automatically using the PC's default printer and are there other methods to shorten the code below .. (sorry for long code.)

Code Block

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace WindowsApplication1

{

public partial class Label : Form

{

public Label()

{

InitializeComponent();

A1 = Image.FromFile(@"C:\Documents and Settings\administrator\Desktop\logo.jpg");

this.AutoScrollMinSize = A1.Size;

A1Bounds = new Point[3];

A1Bounds[0] = new Point(50, 30);

A1Bounds[1] = new Point(90, 30);

A1Bounds[2] = new Point(50, 60);

A2 = Image.FromFile(@"C:\Documents and Settings\administrator\Desktop\logo.jpg");

this.AutoScrollMinSize = A2.Size;

A2Bounds = new Point[3];

A2Bounds[0] = new Point(480, 30);

A2Bounds[1] = new Point(520, 30);

A2Bounds[2] = new Point(480, 60);

A3 = Image.FromFile(@"C:\Documents and Settings\administrator\Desktop\logo.jpg");

this.AutoScrollMinSize = A3.Size;

A3Bounds = new Point[3];

A3Bounds[0] = new Point(50, 310);

A3Bounds[1] = new Point(90, 310);

A3Bounds[2] = new Point(50, 340);

A4 = Image.FromFile(@"C:\Documents and Settings\administrator\Desktop\logo.jpg");

this.AutoScrollMinSize = A4.Size;

A4Bounds = new Point[3];

A4Bounds[0] = new Point(480, 310);

A4Bounds[1] = new Point(520, 310);

A4Bounds[2] = new Point(480, 340);

}

Image A1;

private Point[] A1Bounds;

Image A2;

private Point[] A2Bounds;

Image A3;

private Point[] A3Bounds;

Image A4;

private Point[] A4Bounds;

private Brush blackbrush = Brushes.Black;

private Brush redbrush = Brushes.Red;

private Font ArialBlack = new Font("Arial Black", 12, FontStyle.Bold);

private Font Arial = new Font("Arial Black", 9);

protected override void OnPaint (PaintEventArgs e)

{

base.OnPaint(e);

Graphics dc = e.Graphics;

Pen blackPen = new Pen(Color.Black, 5);

dc.DrawRectangle(blackPen, 20, 20, 400, 250);

dc.DrawLine(blackPen, 20, 70, 420, 70);

dc.DrawRectangle(blackPen, 450, 20, 400, 250);

dc.DrawLine(blackPen, 450, 70, 850, 70);

dc.DrawRectangle(blackPen, 20, 300, 400, 250);

dc.DrawLine(blackPen, 20, 350, 420, 350);

dc.DrawRectangle(blackPen, 450, 300, 400, 250);

dc.DrawLine(blackPen, 450, 350, 850, 350);

dc.ScaleTransform(1.0f, 1.0f);

dc.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);

dc.DrawImage(A1, A1Bounds);

dc.DrawImage(A2, A2Bounds);

dc.DrawImage(A3, A3Bounds);

dc.DrawImage(A4, A4Bounds);

dc.DrawString("A G PTE LTD", ArialBlack, blackbrush,

new Rectangle(new Point(120, 35), new Size(500, 50)));

dc.DrawString("A G PTE LTD", ArialBlack, blackbrush,

new Rectangle(new Point(550, 35), new Size(500, 50)));

dc.DrawString("A G PTE LTD", ArialBlack, blackbrush,

new Rectangle(new Point(120, 315), new Size(500, 50)));

dc.DrawString("A G PTE LTD", ArialBlack, blackbrush,

new Rectangle(new Point(550, 315), new Size(500, 50)));

dc.DrawString("RoHS COMPLIANT 14/12/05", Arial, redbrush,

new Rectangle(new Point(220, 80), new Size(200, 50)));

dc.DrawString("RoHS COMPLIANT 14/12/05", Arial, redbrush,

new Rectangle(new Point(650, 80), new Size(200, 50)));

dc.DrawString("RoHS COMPLIANT 14/12/05", Arial, redbrush,

new Rectangle(new Point(220, 360), new Size(200, 50)));

dc.DrawString("RoHS COMPLIANT 14/12/05", Arial, redbrush,

new Rectangle(new Point(650, 360), new Size(200, 50)));

dc.DrawString("Customer :", Arial, blackbrush,

new Rectangle(new Point(30, 70), new Size(500, 50)));

dc.DrawString("Customer :", Arial, blackbrush,

new Rectangle(new Point(460, 70), new Size(500, 50)));

dc.DrawString("Customer :", Arial, blackbrush,

new Rectangle(new Point(30, 350), new Size(500, 50)));

dc.DrawString("Customer :", Arial, blackbrush,

new Rectangle(new Point(460, 350), new Size(500, 50)));

dc.DrawString("Description :", Arial, blackbrush,

new Rectangle(new Point(30, 90), new Size(500, 50)));

dc.DrawString("Description :", Arial, blackbrush,

new Rectangle(new Point(460, 90), new Size(500, 50)));

dc.DrawString("Description :", Arial, blackbrush,

new Rectangle(new Point(30, 370), new Size(500, 50)));

dc.DrawString("Description :", Arial, blackbrush,

new Rectangle(new Point(460, 370), new Size(500, 50)));

dc.DrawString("Part Code :", Arial, blackbrush,

new Rectangle(new Point(30, 110), new Size(500, 50)));

dc.DrawString("Part Code :", Arial, blackbrush,

new Rectangle(new Point(460, 110), new Size(500, 50)));

dc.DrawString("Part Code :", Arial, blackbrush,

new Rectangle(new Point(30, 390), new Size(500, 50)));

dc.DrawString("Part Code :", Arial, blackbrush,

new Rectangle(new Point(460, 390), new Size(500, 50)));

dc.DrawString("Date :", Arial, blackbrush,

new Rectangle(new Point(30, 130), new Size(500, 50)));

dc.DrawString("Date :", Arial, blackbrush,

new Rectangle(new Point(460, 130), new Size(500, 50)));

dc.DrawString("Date :", Arial, blackbrush,

new Rectangle(new Point(30, 410), new Size(500, 50)));

dc.DrawString("Date :", Arial, blackbrush,

new Rectangle(new Point(460, 410), new Size(500, 50)));

dc.DrawString("Remarks :", Arial, blackbrush,

new Rectangle(new Point(30, 150), new Size(500, 50)));

dc.DrawString("Remarks :", Arial, blackbrush,

new Rectangle(new Point(460, 150), new Size(500, 50)));

dc.DrawString("Remarks :", Arial, blackbrush,

new Rectangle(new Point(30, 430), new Size(500, 50)));

dc.DrawString("Remarks :", Arial, blackbrush,

new Rectangle(new Point(460, 430), new Size(500, 50)));

dc.DrawString("Job Number :", Arial, blackbrush,

new Rectangle(new Point(30, 170), new Size(500, 50)));

dc.DrawString("Job Number :", Arial, blackbrush,

new Rectangle(new Point(460, 170), new Size(500, 50)));

dc.DrawString("Job Number :", Arial, blackbrush,

new Rectangle(new Point(30, 450), new Size(500, 50)));

dc.DrawString("Job Number :", Arial, blackbrush,

new Rectangle(new Point(460, 450), new Size(500, 50)));

dc.DrawString("Quantity :", Arial, blackbrush,

new Rectangle(new Point(30, 190), new Size(500, 50)));

dc.DrawString("Quantity :", Arial, blackbrush,

new Rectangle(new Point(460, 190), new Size(500, 50)));

dc.DrawString("Quantity :", Arial, blackbrush,

new Rectangle(new Point(30, 470), new Size(500, 50)));

dc.DrawString("Quantity :", Arial, blackbrush,

new Rectangle(new Point(460, 470), new Size(500, 50)));

dc.DrawString("QC Result :", Arial, blackbrush,

new Rectangle(new Point(30, 210), new Size(500, 50)));

dc.DrawString("QC Result :", Arial, blackbrush,

new Rectangle(new Point(460, 210), new Size(500, 50)));

dc.DrawString("QC Result :", Arial, blackbrush,

new Rectangle(new Point(30, 490), new Size(500, 50)));

dc.DrawString("QC Result :", Arial, blackbrush,

new Rectangle(new Point(460, 490), new Size(500, 50)));

}

private void Label_Load(object sender, EventArgs e)

{

}

}

}

Thanks.

Michael.





Re: Windows Forms General printing labels from Windows Forms..

Yu Guo – MSFT

Hi, Michael1977,

Please check my last post, and the button1 is just the "Print" button you want.

And you should draw the output in the PrintPage event just as you do in Print event.

For your second question, a loop will greatly shorten your codes.

Code Block

protected override void OnPaint(PaintEventArgs e)

{

base.OnPaint(e);

Graphics dc = e.Graphics;

Pen blackPen = new Pen(Color.Black, 5);

dc.ScaleTransform(1.0f, 1.0f);

dc.TranslateTransform(this.AutoScrollPosition.X, this.AutoScrollPosition.Y);

dc.DrawImage(A1, A1Bounds);

dc.DrawImage(A2, A2Bounds);

dc.DrawImage(A3, A3Bounds);

dc.DrawImage(A4, A4Bounds);

for (int i = 120; i <= 550; i = i + 430)

{

for (int j = 35; j <= 315; j = j + 280)

{

dc.DrawRectangle(blackPen, i - 100, j - 15, 400, 250);

dc.DrawLine(blackPen, i - 100, j + 35, i + 300, j + 35);

dc.DrawString("A G PTE LTD", ArialBlack, blackbrush, new Rectangle(new Point(i, j), new Size(500, 50)));

dc.DrawString("RoHS COMPLIANT 14/12/05", Arial, redbrush, new Rectangle(new Point(i + 100, j + 45), new Size(200, 50)));

dc.DrawString("Customer :", Arial, blackbrush, new Rectangle(new Point(i - 90, j + 35), new Size(500, 50)));

dc.DrawString("Description :", Arial, blackbrush, new Rectangle(new Point(i - 90, j + 55), new Size(500, 50)));

dc.DrawString("Part Code :", Arial, blackbrush, new Rectangle(new Point(i - 90, j + 75), new Size(500, 50)));

dc.DrawString("Date :", Arial, blackbrush, new Rectangle(new Point(i - 90, j + 95), new Size(500, 50)));

dc.DrawString("Remarks :", Arial, blackbrush, new Rectangle(new Point(i - 90, j + 115), new Size(500, 50)));

dc.DrawString("Job Number :", Arial, blackbrush, new Rectangle(new Point(i - 90, j + 135), new Size(500, 50)));

dc.DrawString("Quantity :", Arial, blackbrush, new Rectangle(new Point(i - 90, j + 155), new Size(500, 50)));

dc.DrawString("QC Result :", Arial, blackbrush, new Rectangle(new Point(i - 90, j + 175), new Size(500, 50)));

}

}

}

Hope this helps,

Regards






Re: Windows Forms General printing labels from Windows Forms..

Michael1977

It works!

Thanks Mod!

Michael.