In Visual COBOL, using the .NET Framework classes, can you show me an example of how to browse for a text file by clicking a button and then print that text file to the default printer?
Attached is an example program written in Visual COBOL for Visual Studio R4 which uses the .NET Framework classes to allow the user to browse for a text file, select the file and then to print the file to the default printer.
Here is the main program logic:
$set ilusing"System.Windows.Forms" $set ilusing"System.IO" $set ilusing"System.Drawing.Printing" class-id PrintTextVC.Form1 is partial inherits type System.Windows.Forms.Form. working-storage section. 01 verdana10Font type Font. 01 reader type StreamReader. method-id NEW. procedure division. invoke self::InitializeComponent goback.
method-id btnBrowse_Click final private. 01 fdlg type OpenFileDialog. procedure division using by value sender as object e as type System.EventArgs. set fdlg to new type OpenFileDialog set fdlg::Title to "Visual COBOL Open File Dialog" set fdlg::InitialDirectory to "C:\ " set fdlg::Filter to "Text files|*.txt|All files (*.*)|*.*" set fdlg::FilterIndex to 2 set fdlg::RestoreDirectory to true if (fdlg::ShowDialog = type DialogResult::OK) set textBox1::Text to fdlg::FileName end-if
method-id btnPrint_Click final private. 01 filename string. 01 pd type PrintDocument. procedure division using by value sender as object e as type System.EventArgs. set filename to textBox1::Text::ToString *> Create a StreamReader object set reader to new StreamReader(filename) *> Create a Verdana font with size 10 set verdana10Font to new Font ("Verdana", 10) *> Create a PrintDocument object set pd to new PrintDocument *> Add PrintPage event handler invoke pd::add_PrintPage(new System.Drawing.Printing.PrintPageEventHandler(self::PrintTextHandler)) *> Call Print Method invoke pd::Print *> Close the reader if (reader not = null) invoke reader::Close end-if.
method-id PrintTextHandler final private. 01 g type Graphics. 01 linesPerPage comp-1. 01 yPos comp-1. 01 count1 binary-long. 01 leftMargin comp-1. 01 topMargin comp-1. 01 line1 string. procedure division using by value sender as object, ppeArgs as type PrintPageEventArgs. *> Get the Graphics object set g to ppeArgs::Graphics set linesPerPage to 0 set yPos to 0 set count1 to 0 *> Read margins from PrintPageEventArgs set leftMargin to ppeArgs::MarginBounds::Left set topMargin to ppeArgs::MarginBounds::Top set line1 to null *> Calculate the lines per page on the basis of the height of the page and the height of the font set linesPerPage to ppeArgs::MarginBounds::Height / verdana10Font::GetHeight (g) *> Now read lines one by one, using StreamReader perform until count1 > linesPerPage set line1 to reader::ReadLine if line1 = null exit perform end-if *> Calculate the starting position set yPos to topMargin (count1 * verdana10Font::GetHeight (g)) *> Draw text invoke g::DrawString (line1, verdana10Font, type Brushes::Black, leftMargin, yPos, new type StringFormat) *> Move to next line add 1 to count1 end-perform *> If PrintPageEventArgs has more pages to print if (line1 not = null) set ppeArgs::HasMorePages to true else set ppeArgs::HasMorePages to false end-if