使用单一类用于不同类型的组合框

时间:2011-08-03 13:08:54

标签: c# .net winforms pdf pdf-generation

我有一个组合框cbpaymenttype的值如(DD,CASH,CARD)

    I have list view  Named as listviewcashmembers

 with items lastname(xxxx),
           firstname(xxx),
           postcode(xxx),
           ddrerefernce(xxxx),
          ddprovider(xxx)
          Monthlyamount(xxx)
          MembershipType(xxxx),
           Status (xxxx)
          Enddate (xxxx)
          paymenttype(DD , CASH , CARD)

当我们点击打印按钮并选择适当的组合框(cbpaymenttype.Text)时,我已经这样做了生成pdf

主窗体中的代码是这样的,这段代码也将调用另一个类

      private void btnPrint_Click(object sender, EventArgs e)
      {
                private const string DDPROVIDERMEMBERS_PATH = @"xxxxxxxxxx_{0}";

              if (cbpaymenttype.Text == "DD ")
              {
                string path = string.Format(DDMEMBERSREPORT_PATH, DateTime.Now.ToString("ddMMyyyyHHmm"));
                List<DDmembersmonthlyreport> ddmembersreportmembers = new List<DDmembersmonthlyreport>();
                if (lstviewcashmembers.Items.Count != 0)
                {
                    foreach (ListViewItem item in lstviewcashmembers.Items)
                    {
                        DDmembersmonthlyreport ddmembers = new DDmembersmonthlyreport();
                        ddmembers.member_Lastname = item.SubItems[1].Text;
                        ddmembers.member_Firstname = item.SubItems[2].Text;
                        ddmembers.Postcode = item.SubItems[3].Text;
                        ddmembers.ddReference = item.SubItems[4].Text;
                        ddmembers.ddprovider = item.SubItems[5].Text;
                        ddmembers.Monthlyamount = Convert.ToDecimal(item.SubItems[6].Text);
                        ddmembers.MembershipType = item.SubItems[7].Text;
                        ddmembers.Status = item.SubItems[8].Text;
                        ddmembers.Enddate = Convert.ToDateTime(item.SubItems[9].Text);
                        ddmembers.paymenttype = item.SubItems[10].Text;

                        ddmembersreportmembers.Add(ddmembers);

                    }

                    new PdfDDMembersMonthlyValueReport(ddmembersreportmembers, new Company(mf.gBaseUrl).GetSingleLineCompanyDetails(), path);
                    System.Diagnostics.Process.Start("explorer.exe", path);
                }


          if (cbpaymenttype.Text == "cash")
          {   

                   xxxxxxxxxx
              new PdfCashMembersMonthlyValueReport(ddmembersreportmembers, new                 Company(mf.gBaseUrl).GetSingleLineCompanyDetails(), path);
                    System.Diagnostics.Process.Start("explorer.exe", path);



         }
}

像这样我已经为combobox(cbpaymenttype)

中的所有选项做了

我创建了这样的课程

  public class DDmembersmonthlyreport
  {
    public string member_Lastname;
    public string member_Firstname;
    public string Postcode;
    public string ddReference;
    public string ddprovider;
    public decimal Monthlyamount;
    public string MembershipType;
    public string Status;
    public DateTime Enddate;
    public string paymenttype;

}
public class "PdfDDMembersMonthlyValueReport"
{
    private Document _document;
    private Table _table;
    private List<DDmembersmonthlyreport> _array;
    private string _address;

    public PdfDDMembersMonthlyValueReport(List<DDmembersmonthlyreport> array, string address, string exportpath)
    {
        _array = array;
        _address = address;

        Document ddreportdoc = CreateDocument();
        PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(true);

        pdfRenderer.Document = ddreportdoc;

        if (!Directory.Exists(exportpath))
            Directory.CreateDirectory(exportpath);


        pdfRenderer.RenderDocument();

        pdfRenderer.Save(String.Format(@"{0}\DD members.pdf", exportpath));

    }
    public Document CreateDocument()
    {

        _document = new Document();
        _document.Info.Title = "DD MEMBERS REPORT";
        _document.Info.Author = "xxxxx";

        DefineStyles();
        CreatePage();
        FillContent();

        return _document;

    }

    private void DefineStyles()
    {
        Style style = _document.Styles["Normal"];
        style.Font.Name = "Verdana";

        style = _document.Styles[StyleNames.Header];
        style.ParagraphFormat.AddTabStop("8cm", TabAlignment.Right);

        style = _document.Styles[StyleNames.Footer];
        style.ParagraphFormat.AddTabStop("7cm", TabAlignment.Center);

        // Create a new style called Table based on style Normal
        style = _document.Styles.AddStyle("Table", "Normal");
        style.Font.Name = "Verdana";
        style.Font.Size = 6;

        // Create a new style called Reference based on style Normal
        style = _document.Styles.AddStyle("Reference", "Normal");
        style.ParagraphFormat.SpaceBefore = "6mm";
        style.ParagraphFormat.SpaceAfter = "6mm";
        style.ParagraphFormat.TabStops.AddTabStop("6cm", TabAlignment.Right);


    }

    private void CreatePage()
    {
        Section section = _document.AddSection();

        // Create footer
        Paragraph paragraph = section.Footers.Primary.AddParagraph();
        paragraph.AddText(_address);
        paragraph.Format.Font.Size = 8;
        paragraph.Format.Alignment = ParagraphAlignment.Center;



        paragraph = section.AddParagraph();
        paragraph.Format.SpaceBefore = "1.5cm";
        paragraph.Style = "Reference";
        paragraph.AddFormattedText("DD MEMBERS MONTHLY VALUE REPORT", TextFormat.Bold);
        paragraph.AddTab();
        paragraph.AddDateField("dd MMM yyyy");

        _table = section.AddTable();
        _table.Style = "Table";
        _table.Borders.Color = new Color(0, 0, 0);

        //Defining the columns

        Column column = _table.AddColumn("1.7cm"); //Lastname
        column.Format.Alignment = ParagraphAlignment.Center;

        column = _table.AddColumn("1.7cm"); // first name
        column.Format.Alignment = ParagraphAlignment.Left;

        column = _table.AddColumn("1.7cm"); // postcode
        column.Format.Alignment = ParagraphAlignment.Left;

        column = _table.AddColumn("1.7cm"); // DD ref
        column.Format.Alignment = ParagraphAlignment.Left;

        column = _table.AddColumn("1.7cm"); //DD provider Name
        column.Format.Alignment = ParagraphAlignment.Left;

        column = _table.AddColumn("1.7cm"); // Monthly Amount
        column.Format.Alignment = ParagraphAlignment.Left;

        column = _table.AddColumn("1.7cm"); // mship type
        column.Format.Alignment = ParagraphAlignment.Left;

        column = _table.AddColumn("1.7cm"); // status
        column.Format.Alignment = ParagraphAlignment.Left;

        column = _table.AddColumn("1.7cm"); // Expiry date
        column.Format.Alignment = ParagraphAlignment.Left;

        column = _table.AddColumn("1.7cm"); // payment type
        column.Format.Alignment = ParagraphAlignment.Right;


        // Create the header of the _table
        Row row = _table.AddRow();
        row.HeadingFormat = row.Format.Font.Bold = true;
        row.Format.Alignment = ParagraphAlignment.Center;

       row.Cells[0].AddParagraph("Last Name");
        row.Cells[0].Format.Alignment = ParagraphAlignment.Left;

        row.Cells[1].AddParagraph("First Name");
        row.Cells[1].Format.Alignment = ParagraphAlignment.Left;


        row.Cells[2].AddParagraph("Post Code");
        row.Cells[2].Format.Alignment = ParagraphAlignment.Left;

        row.Cells[3].AddParagraph("DD Reference");
        row.Cells[3].Format.Alignment = ParagraphAlignment.Left;

        row.Cells[4].AddParagraph("DDProvider Name");
        row.Cells[4].Format.Alignment = ParagraphAlignment.Left;

        row.Cells[5].AddParagraph("Monthly Amount");
        row.Cells[5].Format.Alignment = ParagraphAlignment.Left;


        row.Cells[6].AddParagraph("Memebrship Type");
        row.Cells[6].Format.Alignment = ParagraphAlignment.Left;


        row.Cells[7].AddParagraph("Status");
        row.Cells[7].Format.Alignment = ParagraphAlignment.Left;

        row.Cells[8].AddParagraph("ExpiryDate");
        row.Cells[8].Format.Alignment = ParagraphAlignment.Left;


        row.Cells[9].AddParagraph("payment type");
        row.Cells[9].Format.Alignment = ParagraphAlignment.Left;

    }

    private void FillContent()
    {
        foreach (var item in _array)
        {
            // Each item fills two rows
            Row row1 = _table.AddRow();
            row1.TopPadding = 1.0;
            Paragraph paragraph;

            row1.Cells[0].VerticalAlignment = VerticalAlignment.Center;
            row1.Cells[0].Format.Alignment = ParagraphAlignment.Left;
            row1.Cells[0].AddParagraph(item.member_Lastname);

            row1.Cells[1].Format.Alignment = ParagraphAlignment.Right;
            paragraph = row1.Cells[1].AddParagraph(item.member_Firstname);


            row1.Cells[2].Format.Alignment = ParagraphAlignment.Left;
            paragraph = row1.Cells[2].AddParagraph(item.Postcode);

            row1.Cells[3].Format.Alignment = ParagraphAlignment.Right;
            paragraph = row1.Cells[3].AddParagraph(item.ddReference);



            row1.Cells[4].Format.Alignment = ParagraphAlignment.Left;
            paragraph = row1.Cells[4].AddParagraph(item.ddprovider);

            row1.Cells[5].Format.Alignment = ParagraphAlignment.Right;
            paragraph = row1.Cells[5].AddParagraph(item.Monthlyamount.ToString("F2"));


            row1.Cells[6].Format.Alignment = ParagraphAlignment.Left;
            paragraph = row1.Cells[6].AddParagraph(item.MembershipType);

            row1.Cells[7].Format.Alignment = ParagraphAlignment.Right;
            paragraph = row1.Cells[7].AddParagraph(item.Status);

            row1.Cells[8].Format.Alignment = ParagraphAlignment.Left;
            paragraph = row1.Cells[8].AddParagraph(item.Enddate.ToString("dd MMM yyyy"));

            row1.Cells[9].Format.Alignment = ParagraphAlignment.Right;
            paragraph = row1.Cells[9].AddParagraph(item.paymenttype);

        }

    }
}

我为不同的组合框选项(cbpaymenttype)创建了这个类,用于打印listviewitems以选择combobxitems

我的问题是我想在“cbpaymenttype”中使用唯一的单一类用于不同类型的选项

任何人都可以使用单一类为不同类型的选项提供相关的知识 cbpaymenttype

是否可以将cbpaymenttype的值传递给此“ddmembers.paymenttype”

非常感谢

1 个答案:

答案 0 :(得分:0)

随着信息的提供,这是我能提供的最好的......

public enum PaymentType { Other, DD, Cash, Card }

public class DDmembersmonthlyreport
{
    ...
    public PaymentType paymenttype;
    // Edit
    public void UpdatePaymentType(string fromCombobox)
    {
        paymenttype = (PaymentType)Enum.Parse(typeof(PaymentType), fromCombobox);
    }
}