简体   繁体   中英

How can I get my checked items in listbox and print to my receipt form C#

I want to get my checkitems to listbox and display in my receipt print preview but it didn't read it and when I declare the checklistboxitems1 the only show in my print preview is the only last I checked. Please help me guys thank you so much

在此处输入图片说明

Here's my code of cashier form

namespace Barangay_System
{
    public partial class Cashier1 : Form
    {
        public Cashier1()
        {
            InitializeComponent();
        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int sum = 0;

            listBox1.Items.Clear();
            listBox2.Items.Clear();
            label2.Text="";

            foreach (string s in checkedListBox1.CheckedItems)
                listBox1.Items.Add(s);

            foreach (int i in checkedListBox1.CheckedIndices)
            {
                if (i == 0)
                {
                    listBox2.Items.Add(300);
                    sum += 300;
                }

                if (i == 1)
                {
                    listBox2.Items.Add(100);
                    sum += 100;
                }

                if (i == 2)
                {
                    listBox2.Items.Add(200);
                    sum += 200;
                }

                label2.Text = sum.ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            printPreviewDialog1.Document = printDocument1;
            printPreviewDialog1.ShowDialog();
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Bitmap bmp = Properties.Resources.Resibo;
            Image newImage = bmp;
            e.Graphics.DrawImage(newImage, 35, 35, newImage.Width, newImage.Height);
            e.Graphics.DrawString("  Name :                     " + label3.Text, new Font("Arial", 18, FontStyle.Regular), Brushes.Black, new Point(30, 150));
            e.Graphics.DrawString("  Requested :                " + listBox1.Items.ToString(), new Font("Arial", 18, FontStyle.Regular), Brushes.Black, new Point(30, 200));
            e.Graphics.DrawString("  Total :                    " + label2.Text, new Font("Arial", 18, FontStyle.Regular), Brushes.Black, new Point(30, 250));
        }
    }
}

I think you must foreach checklistboxitems1 and check the condition. If(checkboxicon.checked == true) you get them write into the printview. Hope this advance can help you. =)

You have an issue in the line:

 e.Graphics.DrawString("  Requested :                " + listBox1.Items.ToString(), new Font("Arial", 18, FontStyle.Regular), Brushes.Black, new Point(30, 200));

The part listBox1.Items.ToString() will not get you the display string of the list's items. The following will solve this issue:

  private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  {
       List<string> values = new List<string>();

       foreach(object o in listBox1.Items)
           values.Add(o.ToString());

       string selectedItems = String.Join(",", values);

                Bitmap bmp = Properties.Resources.Resibo;
                Image newImage = bmp;
                e.Graphics.DrawImage(newImage, 35, 35, newImage.Width, newImage.Height);
                e.Graphics.DrawString("  Name :                     " + label3.Text, new Font("Arial", 18, FontStyle.Regular), Brushes.Black, new Point(30, 150));
                e.Graphics.DrawString("  Requested :                " + selectedItems , new Font("Arial", 18, FontStyle.Regular), Brushes.Black, new Point(30, 200));
                e.Graphics.DrawString("  Total :                    " + label2.Text, new Font("Arial", 18, FontStyle.Regular), Brushes.Black, new Point(30, 250));
  }
e.Graphics.DrawString("  Requested :                " + listBox1.Items.ToString(), new Font("Arial", 18, FontStyle.Regular), Brushes.Black, new Point(30, 200));

That doesn't make sense. Do this:

var itemsString = "";
foreach (var item in listBox1.Items)
{
    itemsString += item.ToString();

    if(listBox1.Items.IndexOf(item) != listBox1.Items.Count - 1)
        itemsString += ", ";//Seperator
}
e.Graphics.DrawString("  Requested :                " + itemsString, new Font("Arial", 18, FontStyle.Regular), Brushes.Black, new Point(30, 200));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM