如何在列表框中获取已检查的项目并打印到我的收据表格C#

时间:2017-09-14 08:35:51

标签: c# forms listbox

我想将我的checkitems收到列表框并显示在我的收据打印预览中,但它没有读取它,当我声明checklistboxitems1时,我的打印预览中唯一的节目是我检查的唯一一个。请帮帮我们,非常感谢你

enter image description here

这是我的收银员代码

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));
        }
    }
}

3 个答案:

答案 0 :(得分:0)

我认为你必须预先检查checklistboxitems1并检查条件。如果(checkboxicon.checked == true),您可以将它们写入printview。希望这个进步可以帮助你。 =)

答案 1 :(得分:0)

您遇到问题:

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

部分listBox1.Items.ToString()无法获取列表项目的显示字符串。以下将解决此问题:

  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));
  }

答案 2 :(得分:0)

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

这没有意义。这样做:

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));