Ezio Farenze
Ezio Farenze

Reputation: 89

Printing items from listbox in c#

i'm trying to get all the values from listbox then print the values as a receipt, i tried the foreach loop to get all the values in the listbox but in the print preview, it has only 1 value showing.

here's my code:

foreach (object items in listBox1.Items)
        {
            Ticket tkt = new Ticket();
            tkt.orders = items.ToString();
            tkt.print();
        }

and here's my code from the Ticket Class:

PrintDocument pdoc = null;
       String Name, Order,
       public String orders
    {
        //set the person name
        set { this.Order = value; }
        //get the person name 
        get { return this.Order; }
    }public Ticket(String Orders)
    {

        this.orders = Orders;

    }
public void print()
    {
        PrintDialog pd = new PrintDialog();
        pdoc = new PrintDocument();
        PrinterSettings ps = new PrinterSettings();
        Font font = new Font("Courier New", 15);


        PaperSize psize = new PaperSize("Custom", 100, 200);
        //ps.DefaultPageSettings.PaperSize = psize;



        pd.Document = pdoc;
        pd.Document.DefaultPageSettings.PaperSize = psize;
        //pdoc.DefaultPageSettings.PaperSize.Height =320;
        pdoc.DefaultPageSettings.PaperSize.Height = 820;

        pdoc.DefaultPageSettings.PaperSize.Width = 520;

        pdoc.PrintPage += new PrintPageEventHandler(pdoc_PrintPage);

        DialogResult result = pd.ShowDialog();
        if (result == DialogResult.OK)
        {
            PrintPreviewDialog pp = new PrintPreviewDialog();
            pp.Document = pdoc;
            result = pp.ShowDialog();
            if (result == DialogResult.OK)
            {
                pdoc.Print();
            }
        }

    }
       void pdoc_PrintPage(object sender, PrintPageEventArgs e)
    {
        Graphics graphics = e.Graphics;
        Font font = new Font("Courier New", 10);
        float fontHeight = font.GetHeight();
        int startX = 50;
        int startY = 55;
        int Offset = 40;
          Offset = Offset + 20;
        String Orders = this.orders;
        graphics.DrawString("Orders :" + orders, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + Offset);
}

Thanks in advance

Upvotes: 1

Views: 2030

Answers (1)

Rostol
Rostol

Reputation: 209

That's never going to do what you want.

That code basically says

for each item in the listbox
   create a new ticket
   set the string property to item[x].Value.ToString
   print it as it is
   x++
next

you need something like

Ticket tkt = new Ticket();    
foreach (object ticItems in listBox1.Items)
            {
                var order = new order{item = ticItems.ToString}
                tkt.orders.Add(order)
            }
 tkt.print();

That:

  • Creates a new ticket
  • Adds each item to its Orders property which should be (List<string>)
  • THEN after it is all loaded prints it

Upvotes: 1

Related Questions