2017-09-14 63 views
0

我想将我的checkitems列表框显示在我的收据打印预览中,但它没有读取它,当我声明checklistboxitems1时,在我的打印预览中是我检查过的唯一的最后一个。请帮我家伙太感谢你了如何获取我在列表框中的已选项目并打印到我的收据表单C#

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

回答

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

先生您好@Abdullah Dibas太感谢你了,先生就解决 –

+0

@HeiSciff欢迎您 –

+0

我不删除点儿先生@Abdullah Dibas承诺 –

0

的代码,我想你一定的foreach checklistboxitems1和检查的条件。如果(checkboxicon.checked == true),您可以将它们写入到printview中。希望这种进步可以帮助你。 =)

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)); 
相关问题