Create a form, pull in a button control two, a preview feature, a print function, and then pull the control PrintDocument control, Printpreviewdilogs control, Pagesetupdilogs control
Background code
1 list<string> students =New list<String> () {"Tom","John doe","Harry"};2int count =0;//Subscript value for the learner name to be printed3Privatevoid Printdocument1_printpage (Objectsender, System.Drawing.Printing.PrintPageEventArgs e)4{5 Graphics g =E.graphics;6String name =Students[count];7 g.drawstring (name,New Font ("Song Body",), Brushes.black,100,100);8//G.drawline (New Pen (color.red), new Point (200,200), new Point (300,200));9G.dispose ();Ten count++;//Increment, in order to print the next name11//Determine if the print is finished12if (count = =Students. Count)13{E.hasmorepages =False//Do not print next page15}16Else17{E.hasmorepages =True//Continue to print next page19}20}21st22//Preview button23Privatevoid Btnprev_click (Objectsender, EventArgs e)24{25This.printPreviewDialog1.Document =This. printDocument1;26This. Printpreviewdialog1.showdialog ();27}2829//Print button30Privatevoid Btnprint_click (object sender, EventArgs e) 31 {32 this.pagesetupdialog1.document = this33 if ( This.pageSetupDialog1.ShowDialog () == System.Windows.Forms.DialogResult.OK) 34 {35 // print 36 }37}
Test print function