Home > Back-end >  C# PrintPage function from another class
C# PrintPage function from another class

Time:05-24

Problem

I have an issue with moving the PrintPage function (SampleForm_PrintPage) to a new class (PrintPageDesign) also design of the PrintPage uses data from the main form and i have not been able to pull the data in to the new class.

Why?

I'm moving all PrintPage functions to individual classes as there are multiple page designs required in the application, having them all in the same main form seems hard to review and update when each page design requires any change.

Sample Code

To simplify my problem i have created a sample solution in visual basic,

Sample Print

Form1.cs (form code):

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace Sample_Print
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void BTN_Print_Click(object sender, EventArgs e)
        {
            PrintDialog PD_SamplePage = new PrintDialog();
            PrintDocument Doc_SamplePage = new PrintDocument();
            Doc_SamplePage.PrintPage  = SampleForm_PrintPage;
            PD_SamplePage.Document = Doc_SamplePage;
            Doc_SamplePage.Print();
        }

        protected void SampleForm_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            e.Graphics.CompositingMode = CompositingMode.SourceOver;
            e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
            e.Graphics.DrawString(TB_Name.Text.ToString(), new Font("Roboto Condensed",12, FontStyle.Bold), Brushes.Black, 10, 10);
        }

    }
}

Requirement

i would like to move function SampleForm_PrintPage to class PrintPageDesign , currently there is only visual studio generated code is in the class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sample_Print
{
    class PrintPageDesign
    {
    }
}

i have tried several ways to get the value from the text box outside of the main form but resulted in null.

any help is highly appreciated.

CodePudding user response:

As commented above, you can use partial classes to separate the Main Form members, methods and functionalities.

  • Press Shift Alt C to add a new class. Rename the file to PrintPageDesign and hit Add.
  • In the new class, add the partial modifier and change the name to Main (the exact name of the main Form). Note, we are creating a partial class here and not deriving from the Main form.

Now you are within the Main Form context and you can access its members.


Example

The Main Form class:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace Sample_Print
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void BTN_Print_Click(object sender, EventArgs e) => PrintJob1();
    }
}

The PrintPageDesign class:

using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.ComponentModel;

namespace Sample_Print
{
    partial class Main
    {
        private void PrintJob1(bool preview = false)
        {
            using (var doc = new PrintDocument())
            {
                doc.PrintPage  = (s, e) =>
                {
                    var g = e.Graphics;
                    var r = new Rectangle(e.MarginBounds.X, e.MarginBounds.Y, 
                        e.MarginBounds.Width, 32);

                    using (var sf = new StringFormat())
                    using (var fnt = new Font("Roboto Condensed", 12, FontStyle.Bold))
                    {
                        sf.Alignment = StringAlignment.Near;
                        sf.LineAlignment = StringAlignment.Center;

                        g.DrawString(TB_Name.Text, fnt, Brushes.Black, r, sf);
                        r.Y  = r.Height;

                        foreach (Control c in Controls)
                        {
                            g.DrawString(c.Name, fnt, Brushes.Black, r, sf);
                            r.Y  = r.Height;
                        }

                        // ...
                    }
                };

                if (preview)
                    using (var ppd = new PrintPreviewDialog() { Document = doc })
                        ppd.ShowDialog();
                else
                {
                    using (var pd = new PrintDialog() { Document = doc })
                    {
                        if (pd.ShowDialog() == DialogResult.OK)
                            pd.Document.Print();
                    }
                }
            }
        }
    }
}
  • Related