1 Replies - 1618 Views - Last Post: 15 February 2011 - 02:51 PM Rate Topic: -----

#1 bigc1972  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-May 09

Multiline text box printing issue

Posted 15 February 2011 - 02:16 PM

Good afternoon...

I have a relatively basic windows form that users will input information into. When complete they send this information via print job (spool file) to a system that captures the incoming information and uses it to create forms.

Here is the code that is handling the print steps:

private void button2_Click(object sender, EventArgs e)
        {
            {
                StreamWriter sw = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "printjob.txt")); 
                //Use writeline method to write the text
                sw.WriteLine(textBox1.Text);
                sw.WriteLine(textBox2.Text);
                sw.WriteLine(textBox3.Text);
                sw.WriteLine(textBox4.Text);
                sw.WriteLine(textBox5.Text);
                sw.WriteLine(textBox6.Text);
                sw.WriteLine(textBox7.Text);
                sw.WriteLine(textBox8.Text);
                sw.WriteLine(textBox9.Text);
                sw.WriteLine(textBox10.Text);
                sw.WriteLine(textBox15.Text);
                sw.WriteLine(textBox16.Text);
                sw.WriteLine(textBox11.Text);
                sw.WriteLine(textBox14.Text);
                sw.WriteLine(textBox12.Text);
                sw.WriteLine(textBox13.Text);
                sw.WriteLine(textBox17.Text);
                sw.WriteLine(textBox18.Text);
                sw.WriteLine(textBox19.Text);
                sw.WriteLine(textBox20.Text);
                sw.WriteLine(textBox21.Text);
                sw.WriteLine(textBox22.Text);
                sw.WriteLine(textBox23.Text);
                sw.WriteLine(textBox24.Text);
                sw.WriteLine(textBox25.Text);
                sw.WriteLine(textBox26.Text);
                sw.WriteLine(textBox27.Text);
                sw.WriteLine(textBox28.Text);
                sw.WriteLine(dateTimePicker1.Text);
                sw.WriteLine(txtAcctNum.Text);
                sw.WriteLine(txtMRNnum.Text);
                sw.WriteLine(txtDOS.Text);
                sw.WriteLine(textBox29.Text);
                
                //always close your stream
                sw.Close();
            }

            try
            {

                stp = new StreamReader(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "printjob.txt")); 
                
                try
                {
                    printFont = new Font("Arial", 10);
                    PrintDocument pd = new PrintDocument();
                    pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
                    System.Windows.Forms.PrintDialog prndialog = new PrintDialog();
                    prndialog.AllowSomePages = true;
                    prndialog.ShowHelp = true;
                    prndialog.Document = pd;
                    DialogResult result = prndialog.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        pd.Print();
                    }
                }
                finally
                {
                    stp.Close();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        // The PrintPage event is raised for each page to be printed.
        private void pd_PrintPage(object sender, PrintPageEventArgs ev)
        {
            float linesPerPage = 0;
            float yPos = 0;
            int count = 0;
            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
            string line = null;

            // Calculate the number of lines per page.
            linesPerPage = ev.MarginBounds.Height /
               printFont.GetHeight(ev.Graphics);

            // Print each line of the file.
            while (count < linesPerPage &&
               ((line = stp.ReadLine()) != null))
            {
                yPos = topMargin + (count *
                   printFont.GetHeight(ev.Graphics));
                ev.Graphics.DrawString(line, printFont, Brushes.Black,
                   leftMargin, yPos, new StringFormat());
                count++;
            }

            // If more lines exist, print another page.
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;

            MessageBox.Show("Please be certain the NIPS form has been loaded into the printers paper tray." + "\n\n The form will be sent to the printer as soon as OK is clicked.",
                    "NIPS FORM PRINTED", MessageBoxButtons.OK, MessageBoxIcon.Warning); 

        }



The entire process works great with the exception of the mulitline text box (textbox29). I have word wrap enabled which allows the users to type continuously but when this is done and the form info is sent via print I only get the first 76 characters because I'm currently using WriteLine within the StreamWriter...Now I've altered this to Write only and it will send any and all characters typed which causes a problem on my adjacent system...

This "other" system needs to see it in 76 character per line chunks as if the user had manually done carriage returns at the end of each line (which works perfectly if I do that at the form and then print).

Is there any solution that I can employ in this case? My user base for this little app/form is not that big so if I need to instruct them to just utilize returns at the end of each line I can but wanted to exhaust all avenues before punting on 4th & 35...

Thank you in advance for any insight that can be provided!

BC

Is This A Good Question/Topic? 0
  • +

Replies To: Multiline text box printing issue

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Multiline text box printing issue

Posted 15 February 2011 - 02:51 PM

See the TextBoxBase.Lines Property
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1