locked
File is printed, not full pages but only 1 page. RRS feed

  • Question

  • I want to get Code Sample to print full pages. 

    The file is made by  Console Application in Visual Web Developer Express 2010.

    The file is printed only 1 page.  How to amend to get full pages(2 pages).

    Please show me

     < My mail   is  "changghil @naver.com" >

     

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

    namespace ConsoleApplication1
    {
        class Program
        {
            private static StringBuilder sbPrint = new StringBuilder();
            static void Main(string[] args)
            {
                string strTemp = " ";

     

                Console.WriteLine("  숫자를 입력하세요  ");
                string a = Console.ReadLine();

                if (a == "1")
                {
                    strTemp = "  가나다\r\n 가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n가나다\r\n    ";
                    Console.WriteLine(strTemp);
                    sbPrint.AppendLine(strTemp);
                }
     

                System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
                doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(doc_PrintPage);
                doc.Print();
            }

            static void doc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                System.Drawing.Font font = new System.Drawing.Font("MS Sans Serif", 12);
                e.Graphics.DrawString(sbPrint.ToString(), font, System.Drawing.Brushes.Black, 10, 10);
            }

        }
    }

    Friday, April 8, 2011 7:43 AM

Answers

  • Hi,

    ok, this is a different topic as the former one I think, so, woulkd you mind to open a new thread next time?

    ###############################################################

    Here's one suggestion concerning your problem:

    If you really have that lot of conditions, use one variable ("d") to store the complete and unique result of combining them to one value.

    Read about flags and combining flag-values. (bitfield, bitwise Or and bitshift)

    http://www.blackwasp.co.uk/FlagsAttribute.aspx (and more of this site, explaining the situation)

     

     

      static void Main(string[] args)
      {
       int aa = 0;
       int bb = 0;
       int cc = 0;
    
       string a = Console.ReadLine();
       Int32.TryParse(a, out aa);
    
       string b = Console.ReadLine();
       Int32.TryParse(b, out bb);
    
       string c = Console.ReadLine();
       Int32.TryParse(c, out cc);
    
       //we use an int here, because we *know* by the possible values of aa, bb, cc, 
       //that the *result fits* into an int 
       int d = 0;
    
       //chack values
       if (aa > 0 && bb > 0 && cc > 0 && aa < 256 && bb < 256 && cc < 256)
       {
        d |= aa;
        d |= (bb << 8); //bitshift 8 to left (= * 256)
        d |= (cc << 16); //bitshift 16 to left (= * 65536)
    
        Console.WriteLine(d.ToString());
       }
       else
        Console.WriteLine("Variables must be in Range from 1 to 255");
    
       Console.ReadLine();
      }
    

     

     

    Then, if you have your unique value for d, create a switch and check the states of d for the further execution-route of your code.

    http://msdn.microsoft.com/en-us/library/06tc147t.aspx

     

     

       switch (d)
       {
        case 65793: //1+256+65536 (aa = 1, bb= 1, cc = 1)
         Dosomething();
         break;
    
        case 65794: //aa = 2, bb = 1, cc = 1
         DosomethingElse();
         break;
    
        case 65796: //aa = 4, bb = 1, cc = 1
         DosomethingCompleteElse();
         break;
    
        //...
    
        default:
         break;
       }
    
    But maybe there is a more human-readable solution to your problem, that depends on the possible values of your variables and the (number of) 
    
    possible execution-ways in your program... So, mybe re-think the whole situation.
    
    Regards,
    
     Thorsten
    

     


    • Marked as answer by Lie You Wednesday, April 20, 2011 3:55 AM
    Wednesday, April 20, 2011 12:59 AM

All replies

  • To get 2 pages; count the pages printed and set e.HasMorePages true after printing the first page.  Leave it false after printing the second page.
    Friday, April 8, 2011 9:47 AM
  • I don't know how to amend the file with "count the pages printed and set e.HasMorePages true after printing the first page. Leave it false printing the second page."  Please , give me the file amended with it. 

    Saturday, April 9, 2011 1:39 AM
  • Hi,

    see this example:

    http://msdn.microsoft.com/en-us/library/cwbe712d.aspx

    Regards,

      Thorsten

    Saturday, April 9, 2011 3:19 AM
  • ... so for your code (here as a WindowsForms-Program, because of the PrintPreviewControl):

      public partial class Form1 : Form
      {
        string stringToPrint = "";
    
        public Form1()
        {
          InitializeComponent();
    
          this.ClientSize = new System.Drawing.Size(839, 621);
    
          stringToPrint = "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
            "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
            "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
            "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
            "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
            "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
            "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
            "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
            "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
            "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
            "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
            "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n";
    
          System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
          doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
    
          PrintPreviewControl printPreviewControl1 = new PrintPreviewControl();
          printPreviewControl1.Columns = 2;
          printPreviewControl1.Location = new System.Drawing.Point(12, 12);
          printPreviewControl1.Name = "printPreviewControl1";
          printPreviewControl1.Rows = 1;
          printPreviewControl1.Size = new System.Drawing.Size(800, 597);
          printPreviewControl1.TabIndex = 0;
          this.Controls.Add(printPreviewControl1);
          printPreviewControl1.Document = doc;
    
          //I commented this out for testing only with the preview
          //doc.Print();
    
        }
    
        //example from http://msdn.microsoft.com/en-us/library/cwbe712d.aspx
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
          int charactersOnPage = 0;
          int linesPerPage = 0;
    
          // Sets the value of charactersOnPage to the number of characters 
          // of stringToPrint that will fit within the bounds of the page.
          e.Graphics.MeasureString(stringToPrint, this.Font,
            e.MarginBounds.Size, StringFormat.GenericTypographic,
            out charactersOnPage, out linesPerPage);
    
          // Draws the string within the bounds of the page
          e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
            e.MarginBounds, StringFormat.GenericTypographic);
    
          // Remove the portion of the string that has been printed.
          stringToPrint = stringToPrint.Substring(charactersOnPage);
    
          // Check to see if more pages are to be printed.
          e.HasMorePages = (stringToPrint.Length > 0);
        }
    
      }
    

    Regards,

      Thorsten

    Saturday, April 9, 2011 3:33 AM
  • Thanks, for kindness

    My file is worked on ConsoleApplication

    When I used the method you gave me in my file,  error arose.

    I think what you gave me was the infomation on FomeApplication.

    My file is very simple, such as I showed, but I must work on ConsoleApplication.

    I want to get the method in ConsoleApplication, Please help me. 


    • Merged by Lie You Monday, April 11, 2011 7:31 AM
    • Marked as answer by Lie You Thursday, April 21, 2011 7:27 AM
    • Unmarked as answer by Lie You Friday, April 22, 2011 1:25 AM
    Saturday, April 9, 2011 2:57 PM
  • Thanks, for kindness

    My file is worked on ConsoleApplication

    When I used the method you gave me in my file,  error arose.

    I think what you gave me was the infomation on FomeApplication.

    My file is very simple, such as I showed, but I must work on ConsoleApplication.

    I want to get the method in ConsoleApplication, Please help me. 

    Saturday, April 9, 2011 3:13 PM
  • Hi,

    here's a (untested) Console-Version of the above but without the PrintPreview. Referenc the System.Drawing.dll in your project:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static string stringToPrint = "";
    
        static void Main(string[] args)
        {
          stringToPrint = "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
             "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
             "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
             "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
             "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
             "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
             "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
             "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
             "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
             "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
             "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
             "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n";
    
          Console.WriteLine(stringToPrint);
    
          System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
          doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
    
          doc.Print();
    
        }
    
        //example from http://msdn.microsoft.com/en-us/library/cwbe712d.aspx, changed
        private static void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
          int charactersOnPage = 0;
          int linesPerPage = 0;
    
          using (Font font = new Font("MS Sans Serif", 12))
          {
            // Sets the value of charactersOnPage to the number of characters 
            // of stringToPrint that will fit within the bounds of the page.
            e.Graphics.MeasureString(stringToPrint, font,
             e.MarginBounds.Size, StringFormat.GenericTypographic,
             out charactersOnPage, out linesPerPage);
    
            // Draws the string within the bounds of the page
            e.Graphics.DrawString(stringToPrint, font, Brushes.Black,
             e.MarginBounds, StringFormat.GenericTypographic);
          }
    
          // Remove the portion of the string that has been printed.
          stringToPrint = stringToPrint.Substring(charactersOnPage);
    
          // Check to see if more pages are to be printed.
          e.HasMorePages = (stringToPrint.Length > 0);
        }
    
      }
    }
    

     

    Regards,

      Thorsten

    Sunday, April 10, 2011 3:49 AM
  • Your answer ! ,very,very, thank you , Pile is printed full pages.

    But, when I insert your file in my file, my file is printed half pages.

    As follow

                Console.WriteLine(" enter  number about a");
                string a = Console.ReadLine();
                Console.WriteLine(" enter  number about b");
                string b = Console.ReadLine();
                if (a == "1")
                {
                    stringToPrint = "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n";
                    Console.WriteLine(stringToPrint);
                }
                if (b == "1")
                {
                    stringToPrint = "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
       "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n";
                    Console.WriteLine(stringToPrint);
                }

    The problem is that "XYZ ---- XYZ" of "a" is printed fully, but "XYZ ---- XYZ" of "b" is never printed. 

    How to amend the file for printting all contents of  "a" and "b" ?

    There are many  "string @ = Console.ReadLine();" and  " if (@ == "1")" in my file.

     

    Sunday, April 10, 2011 5:37 AM
  • Hi, what exactly should be done? I don't understand your code/concept here.

    You ask the user to input two numbers, but you only check, if they are equal to 1. And if "a" is 1 and "b" is 1, the string to print will onyl have the value of the part if(b=="1")... It will be set by the a = 1 part but overwritten again by the b-part....

    If you want to make a decision by the users input use *one* variable with *two* (or more) different value-possibilities like:

        static string stringToPrint = "";
    
        static void Main(string[] args)
        {
          Console.WriteLine("enter either 1 (to print string XYZ...) or 2 (to print string ABC...)");
          string a = Console.ReadLine();
    
          if (a == "1")
          {
            stringToPrint = "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
              "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
              "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
              "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
              "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
              "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
              "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
              "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
              "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
              "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
              "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n" +
              "XYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\nXYZ\r\n";
            Console.WriteLine(stringToPrint);
          }
          else if (a == "2")
          {
            stringToPrint = "ABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\n" +
              "ABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\n" +
              "ABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\n" +
              "ABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\n" +
              "ABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\n" +
              "ABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\n" +
              "ABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\n" +
              "ABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\n" +
              "ABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\n" +
              "ABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\n" +
              "ABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\n" +
              "ABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\nABC\r\n";
            Console.WriteLine(stringToPrint);
          }
    
          //...
        }
    

     

    Regards,

      Thorsten

    Sunday, April 10, 2011 8:55 AM
  • Thank you for your kindness

    1) My file has many conditions which contained many "string @=ConsoleReadLine" and "if(@=="number"),

             and  "else if" is not used. 

       such as, follows

          string a = ConsoleReadLine()

          string b = ConsoleReadLine()

          string c = ConsoleReadLine()

          string d = ConsoleReadLine()

          string e = ConsoleReadLine()

                if (a == "1")

                    {

                        stringToPrint = " ABC " ;

                        Console.WriteLine(stringToPrint);

                    }          

                same kinds of  others   about a   have many lines       

     

                if (a == "2")

                    {

                        stringToPrint = " CDE " ;

                        Console.WriteLine(stringToPrint);

                    }

               

                if (a == "3")

                    {

                        stringToPrint = " RGH " ;

                        Console.WriteLine(stringToPrint);

                    }

                

                if (a == "4")

                    {

                        stringToPrint = " IJK " ;

                        Console.WriteLine(stringToPrint);

                    } 

                if (b == "1")

                    {

                        stringToPrint = " LMN " ;

                        Console.WriteLine(stringToPrint);

                    }       

                if (b== "2")

                    {

                        stringToPrint = " OPQ " ;

                        Console.WriteLine(stringToPrint);

                    }          

                if (b == "3")

                    {

                        stringToPrint = " RST " ;

                        Console.WriteLine(stringToPrint);

                    }

               same kinds of  others  about b   have many lines

                same kinds of  others  about c   have many lines

                same kinds of  others  about d   have many lines

                same kinds of  others  about e   have many lines

         so, conditions are many, many

    2) After debug, resultant lines which is writed in ConsoleBox are many.

      Therefore, all lines of dopted condition, that is gadered, need several pages which should be printed.  

     3) My file which had already made   used  

           """   private static StringBuilder sbPrint = new StringBuilder();
                static void Main(string[] args)
                {
                      string strTemp = " ";    """

      If I can use 'strTemp', it's best,   if I have to use 'stringToPrint' for getting full pages,

      I must change all lines in file, from 'strTemp'  to 'stringToPrint'.

      But,  to get full pages is imortant.  I need your Help.

               

     

     

     

     



    Sunday, April 10, 2011 11:18 AM
  • Hi,

    ok, what we have to clear first is what you want to be printed out by the printer. so if you have in variable a "ABC" and in Variable b "FGH" and in varibale c "XYZ", how should it be printed? First the part "ABC", then immediately after "FGH" and then "XYZ", or "ABC" -> new page -> "FGH" -> new page -> "XYZ"?

    For the first, simply concatenate the strings, either directly by using stringToPrint += ..., or , better, use a StringBuilder and its Append-Method. For the second the easiest way is simply to cretae more than one print-job, by setting the string to print with one value, print, set the string to the next value, print again and so on.

    Regards,

      Thorsten

    Monday, April 11, 2011 2:55 AM
  • Thank you, very much

    My printting problem is solved by "StringToPrint +=".

    The side blank of printed paper is too wide, how to adjust the blank,

    but the size of printed letter remains in the same size,

    I know  the size of letter can be adjusted  with "Font("MS Sans Serif", 12)".

    Can you show me the changed file that is able to adjust the blank.


    Monday, April 11, 2011 4:28 AM
  • Hi,

    set the Margins of the page to your desired value.

    In your code,

    Change:

          System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
          doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
    
          doc.Print();
    

     

    To:

          System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
          doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
    
          //margins, in hundredths of an inch,
          System.Drawing.Printing.Margins margins = new System.Drawing.Printing.Margins(10, 10, 10, 10);
          doc.DefaultPageSettings.Margins = margins;
    
          doc.Print();
    

    Adjust the margins to your needs.

    Regards,

      Thorsten

    Monday, April 11, 2011 5:09 AM
  • Hi,

    Please do not post the same issue twice.

    thanks.


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, April 11, 2011 7:32 AM
  • Thank you, you are my hero.       Please, teach me somthing more.

    I will make several fills which is the first file,  second file,  third file, and fourth file.

    1) In the first file, there are          

               string a = ConsoleReadLine()

               string b = ConsoleReadLine()

               string c = ConsoleReadLine()

               string d = ConsoleReadLine()

               string e = ConsoleReadLine()

    2) In the second file, third file, fourth file,  there are follows, and there are not above string a, b, c, d, e .

                if (a == "1")

                    {

                        stringToPrint = " ABC " ;

                        Console.WriteLine(stringToPrint);

                    }          

                same kinds of  others   about a   have many lines        

                if (a == "2")

                    {

                        stringToPrint = " CDE " ;

                        Console.WriteLine(stringToPrint);

                    }          

                if (a == "3")

                    {

                        stringToPrint = " RGH " ;

                        Console.WriteLine(stringToPrint);

                    }           

                if (a == "4")

                    {

                        stringToPrint = " IJK " ;

                        Console.WriteLine(stringToPrint);

                    } 

                if (b == "1")

                    {

                        stringToPrint = " LMN " ;

                        Console.WriteLine(stringToPrint);

                    }       

                if (b== "2")

                    {

                        stringToPrint = " OPQ " ;

                        Console.WriteLine(stringToPrint);

                    }          

                if (b == "3")

                    {

                        stringToPrint = " RST " ;

                        Console.WriteLine(stringToPrint);

                    }

               same kinds of  others  about b   have many lines

                same kinds of  others  about c   have many lines

                same kinds of  others  about d   have many lines

                same kinds of  others  about e   have many lines

               so, conditions are many, many

    3) I want to get the files (=2nd, 3rd, 4th file) can perceive the 1st file entered numbers about a,b,c,d,e.

      For operating the  files (=2nd, 3rd, 4th file) without entering numbers

      about a,b,c,d,e.      
    Monday, April 11, 2011 7:58 AM
  • Hi,

    what do you mean with "second file" ... ? A second file to be printed, a second method or block in your application or a second complete program?

    If you want to print more than one file in one program and the values of "a", "b" ... should be kept throughout the whole process, make the variables scope so, that all parts of your program have access to them.

    Schematic:

    _Define variables

    _block_1

        _check variables state

        _setup String

        _print

    _block_2

        _check variables state

        _setup String

        _print

    Regards,

      Thorsten

    Monday, April 11, 2011 8:16 AM
  • Here's an example for that first scenario. The user has to input values for the variables "a" and "b" and then two documents are printed based on the program's decision made by the values of these two variables:

    Note: Insert the code for printDocument_PrintPage from an example above to get actual printed results.

     

        static string stringToPrint = "";
    
        static void Main(string[] args)
        {
          string a = Console.ReadLine();
          string b = Console.ReadLine();
    
          PrintFile1(a, b);
          PrintFile2(a, b);
    
          //...
        }
    
        private static void PrintFile1(string a, string b)
        {
          if (a == "1")
            stringToPrint = "ABC...";
          if (a == "2")
            stringToPrint = "XYZ---";
    
          if (b == "1")
            stringToPrint += "DEF...";
          if (b == "2")
            stringToPrint += "UVW---";
    
          //print the complete first string
          DoTheJob();
        }
    
        private static void PrintFile2(string a, string b)
        {
          if (a == "1")
            stringToPrint = "123...";
          if (a == "2")
            stringToPrint = "890---";
    
          if (b == "1")
            stringToPrint += "456...";
          if (b == "2")
            stringToPrint += "567---";
    
          //print the complete second string
          DoTheJob();
        }
    
        private static void DoTheJob()
        {
          System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
          doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
    
          doc.Print();
        }
    
        private static void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
          //code like in the other examples
        }
    

    Regards,

      Thorsten

    Monday, April 11, 2011 8:31 AM
  • Thank you

    The files which I make have to be independentlly sepeated.

    If I'd like to insert my clause to the file you gave me.

    How about as follows. I tink it will be better, if errors will be corrected ,

    1) 1st File

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;

    namespace ConsoleApplication1
    {
        class Program
        {
            static string stringToPrint = "";
     
            static void Main(string[] args)
            {

         // -- - - - - - - - -- - - - - - - - - - --

               string a = ConsoleReadLine()

               string b = ConsoleReadLine()

               string c = ConsoleReadLine()

               string d = ConsoleReadLine()

               string e = ConsoleReadLine()

        // - - - - - - - - - - - - - - - - - - -- - -

                System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
                doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);

                doc.Print();
            }
            //example from http://msdn.microsoft.com/en-us/library/cwbe712d.aspx, changed
            private static void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                int charactersOnPage = 0;
                int linesPerPage = 0;

                using (Font font = new Font("MS Sans Serif", 12))
                {
                    // Sets the value of charactersOnPage to the number of characters
                    // of stringToPrint that will fit within the bounds of the page.
                    e.Graphics.MeasureString(stringToPrint, font,
                     e.MarginBounds.Size, StringFormat.GenericTypographic,
                     out charactersOnPage, out linesPerPage);

                    // Draws the string within the bounds of the page
                    e.Graphics.DrawString(stringToPrint, font, Brushes.Black,
                     e.MarginBounds, StringFormat.GenericTypographic);
                }

                // Remove the portion of the string that has been printed.
                stringToPrint = stringToPrint.Substring(charactersOnPage);

                // Check to see if more pages are to be printed.
                e.HasMorePages = (stringToPrint.Length > 0);
            }

        }
    }

    2) other Files

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;

    namespace ConsoleApplication1
    {
        class Program
        {
            static string stringToPrint = "";
     
            static void Main(string[] args)
            {

         // -- - - - - - - - -- - - - - - - - - - --

                if (a == "1")

                    {

                        stringToPrint = " ABC " ;

                        Console.WriteLine(stringToPrint);

                    }          

                same kinds of  others   about a   have many lines        

                if (a == "2")

                    {

                        stringToPrint = " CDE " ;

                        Console.WriteLine(stringToPrint);

                    }          

                if (a == "3")

                    {

                        stringToPrint = " RGH " ;

                        Console.WriteLine(stringToPrint);

                    }           

                if (a == "4")

                    {

                        stringToPrint = " IJK " ;

                        Console.WriteLine(stringToPrint);

                    } 

                if (b == "1")

                    {

                        stringToPrint = " LMN " ;

                        Console.WriteLine(stringToPrint);

                    }       

                if (b== "2")

                    {

                        stringToPrint = " OPQ " ;

                        Console.WriteLine(stringToPrint);

                    }          

                if (b == "3")

                    {

                        stringToPrint = " RST " ;

                        Console.WriteLine(stringToPrint);

                    }

               // same kinds of  others  about b   have many lines

               // same kinds of  others  about c   have many lines

               // same kinds of  others  about d   have many lines

               // same kinds of  others  about e   have many lines

               // so, conditions are many, many

     

                System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
                doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);

                doc.Print();
            }
            //example from http://msdn.microsoft.com/en-us/library/cwbe712d.aspx, changed
            private static void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                int charactersOnPage = 0;
                int linesPerPage = 0;

                using (Font font = new Font("MS Sans Serif", 12))
                {
                    // Sets the value of charactersOnPage to the number of characters
                    // of stringToPrint that will fit within the bounds of the page.
                    e.Graphics.MeasureString(stringToPrint, font,
                     e.MarginBounds.Size, StringFormat.GenericTypographic,
                     out charactersOnPage, out linesPerPage);

                    // Draws the string within the bounds of the page
                    e.Graphics.DrawString(stringToPrint, font, Brushes.Black,
                     e.MarginBounds, StringFormat.GenericTypographic);
                }

                // Remove the portion of the string that has been printed.
                stringToPrint = stringToPrint.Substring(charactersOnPage);

                // Check to see if more pages are to be printed.
                e.HasMorePages = (stringToPrint.Length > 0);
            }

        }
    }

    For the files (=2nd, 3rd, 4th file) can perceive simultaneously numbers entered about a,b,c,d,and e in the 1st file

        // - - - - - - - - - - - - - - - - - - -- - -
    Monday, April 11, 2011 9:43 AM
  • Ok,

    so you want to make different programs. One application for one printJob. But why, if you want to start the next one automatically?

    Of course you could (start the next program with Process.Start() with the variables-values as parameter), but if anyway *all*  printing should be done, you could do everything in *one* application.

    Regards,

      Thorsten

    Monday, April 11, 2011 10:13 AM
  • If you really want to make more than one program, you could do something like:

    Note:Change the path for the FileName of the Process' StartInfo and Change also its Arguments by joining the variables (a, b ...) with a seperator ";". Remove the stuff concerning the stopProgram-variable. I needed that because I restart here the same program and I only want to restart it one time.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          string a = "";
          string b = "";
    
          //stop the program after it reopens itself once
          //remove that, because you will not start the *same* program again
          string stopProgam = "";
    
          if (args.Length > 0)
          {
            //split the string at each semicolon
            string[] par = args[0].Split(';');
    
            if (par.Length > 0 && par[0].Length > 0)
              a = par[0];
            else
            {
              Console.WriteLine("input number");
              a = Console.ReadLine();
            }
    
            if (par.Length > 1 && par[1].Length > 0)
              b = par[1];
            else
            {
              Console.WriteLine("input number");
              b = Console.ReadLine();
            }
    
            //set this indicator when the program restarts itself
            if(par.Length > 2)
              stopProgam = par[2];
          }
          else
          {
            Console.WriteLine("input number");
            a = Console.ReadLine();
            Console.WriteLine("input number");
            b = Console.ReadLine();
          }
    
          //test
          Console.WriteLine(a);
          Console.WriteLine(b);
    
          //...
          //setup stringToPrint
    
          //setup printdocument
          //print
          //...
    
          //start the next program like this
          //here I restart the same program again
          System.Diagnostics.Process p = new System.Diagnostics.Process();
    
          //###################################################
          //here set the path to the next program to be startet, I do restart the same for demo here
          p.StartInfo.FileName = System.Reflection.Assembly.GetExecutingAssembly().Location;
    
          //here set the arguments
          p.StartInfo.Arguments = a + ";" + b + ";stop";
          //###################################################
    
          //you dont need this if-statement, remove it
          if(stopProgam != "stop")
            p.Start();
          //just call
          //p.Start();
    
          //test, not needed
          Console.ReadLine();
        }
      }
    }
    
    Regards,
     
      Thorsten
    Monday, April 11, 2011 10:47 AM
  • I need your Help more

    1) My fisrt  File is

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;

    namespace ConsoleApplication1
    {
        class Program
        {
            static string stringToPrint = "";
            static string strTemp = " ";

            private static StringBuilder sbPrint = new StringBuilder();     
            static void Main(string[] args)
            {

       // ----------------------------------------------------------
                Console.WriteLine(" input number ");
                string a = Console.ReadLine();
        //------------------------------------------------------------

                System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
                doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
     
                //margins, in hundredths of an inch,
                System.Drawing.Printing.Margins margins = new System.Drawing.Printing.Margins(50, 50, 50, 30);
                doc.DefaultPageSettings.Margins = margins;
             //... additionally:
             // http://msdn.microsoft.com/en-us/library/system.drawing.printing.pagesettings.margins(v=VS.100).aspx

                doc.Print();
            }
            //example from http://msdn.microsoft.com/en-us/library/cwbe712d.aspx, changed
            private static void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                int charactersOnPage = 0;
                int linesPerPage = 0;

                using (Font font = new Font("MS Sans Serif", 10))
                {
                    e.Graphics.MeasureString(stringToPrint, font,
                     e.MarginBounds.Size, StringFormat.GenericTypographic,
                     out charactersOnPage, out linesPerPage);
                    e.Graphics.DrawString(stringToPrint, font, Brushes.Black,
                     e.MarginBounds, StringFormat.GenericTypographic);
                }

                // Remove the portion of the string that has been printed.
                stringToPrint = stringToPrint.Substring(charactersOnPage);
                // Check to see if more pages are to be printed.
                e.HasMorePages = (stringToPrint.Length > 0);
            }

        }
    }

    2) My second  File is

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;

    namespace ConsoleApplication1
    {
        class Program
        {
            static string stringToPrint = "";
            static string strTemp = " ";

            private static StringBuilder sbPrint = new StringBuilder();     
            static void Main(string[] args)
            {

       // ----------------------------------------------------------
                if (a == "1")
                {
                    stringToPrint += "XYZ\r\n";
                    Console.WriteLine(stringToPrint);
                }
     
                if (a == "2")
                {
                    stringToPrint += "ABC \r\n";
                    Console.WriteLine(stringToPrint);
                }  

      //------------------------------------------------------------

                System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
                doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
     
                //margins, in hundredths of an inch,
                System.Drawing.Printing.Margins margins = new System.Drawing.Printing.Margins(50, 50, 50, 30);
                doc.DefaultPageSettings.Margins = margins;
             //... additionally:
             // http://msdn.microsoft.com/en-us/library/system.drawing.printing.pagesettings.margins(v=VS.100).aspx

                doc.Print();
            }
            //example from http://msdn.microsoft.com/en-us/library/cwbe712d.aspx, changed
            private static void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                int charactersOnPage = 0;
                int linesPerPage = 0;

                using (Font font = new Font("MS Sans Serif", 10))
                {
                    e.Graphics.MeasureString(stringToPrint, font,
                     e.MarginBounds.Size, StringFormat.GenericTypographic,
                     out charactersOnPage, out linesPerPage);
                    e.Graphics.DrawString(stringToPrint, font, Brushes.Black,
                     e.MarginBounds, StringFormat.GenericTypographic);
                }

                // Remove the portion of the string that has been printed.
                stringToPrint = stringToPrint.Substring(charactersOnPage);
                // Check to see if more pages are to be printed.
                e.HasMorePages = (stringToPrint.Length > 0);
            }

        }
    }

    3)  The "string a = Console.ReadLine();" is not inserted in 2nd File.
     I want to  operate the second File normaly without  "string a = Console.ReadLine();"

    Can you give me the 1st and 2nd Files amended ?




    Tuesday, April 12, 2011 9:26 AM
  • Hi,

    first program: (Change the path to the exe in the process to start, see comments)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static string stringToPrint = "";
        static string strTemp = " ";
    
        private static StringBuilder sbPrint = new StringBuilder();
        static void Main(string[] args)
        {
    
          // ----------------------------------------------------------
          Console.WriteLine(" input number ");
          string a = Console.ReadLine();
          Console.WriteLine(" input number ");
          string b = Console.ReadLine();
          //------------------------------------------------------------
    
          System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
          doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
    
          //margins, in hundredths of an inch,
          System.Drawing.Printing.Margins margins = new System.Drawing.Printing.Margins(50, 50, 50, 30);
          doc.DefaultPageSettings.Margins = margins;
          //... additionally:
          // http://msdn.microsoft.com/en-us/library/system.drawing.printing.pagesettings.margins(v=VS.100).aspx
    
          doc.Print();
    
          //start the next program like this
          System.Diagnostics.Process p = new System.Diagnostics.Process();
    
          //###################################################
          //here set the path to the next program to be startet
          p.StartInfo.FileName = @"C:\Users\...\Testversionen\ConsoleApplication2\ConsoleApplication2\bin\Debug\ConsoleApplication2.exe"; //Here put the path to the exe-file of your second program
    
          //here set the arguments
          p.StartInfo.Arguments = a + ";" + b;
    
          p.Start();
          //###################################################
        }
        //example from http://msdn.microsoft.com/en-us/library/cwbe712d.aspx, changed
        private static void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
          int charactersOnPage = 0;
          int linesPerPage = 0;
    
          using (Font font = new Font("MS Sans Serif", 10))
          {
            e.Graphics.MeasureString(stringToPrint, font,
             e.MarginBounds.Size, StringFormat.GenericTypographic,
             out charactersOnPage, out linesPerPage);
            e.Graphics.DrawString(stringToPrint, font, Brushes.Black,
             e.MarginBounds, StringFormat.GenericTypographic);
          }
    
          // Remove the portion of the string that has been printed.
          stringToPrint = stringToPrint.Substring(charactersOnPage);
          // Check to see if more pages are to be printed.
          e.HasMorePages = (stringToPrint.Length > 0);
        }
    
      }
    }
    

     

     Second program:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static string stringToPrint = "";
        static string strTemp = " ";
    
        private static StringBuilder sbPrint = new StringBuilder();
        static void Main(string[] args)
        {
    
          string a = "";
          string b = "";
    
          //you'll get the variables out of the args[0]-string
          //but because in the first program we added the variable-values
          //delimited by a semicolon, we have to split the string into
          //its parts
    
          //if arguments are passed to this file
          if (args.Length > 0)
          {
            //split the string at each semicolon
            string[] par = args[0].Split(';');
    
            //check the parts and set the variable a
            if (par.Length > 0 && par[0].Length > 0)
              a = par[0];
            else
            {
              Console.WriteLine("input number");
              a = Console.ReadLine();
            }
    
            //check the parts and set the variable b
            if (par.Length > 1 && par[1].Length > 0)
              b = par[1];
            else
            {
              Console.WriteLine("input number");
              b = Console.ReadLine();
            }
          }
          else
          {
            Console.WriteLine("input number");
            a = Console.ReadLine();
            Console.WriteLine("input number");
            b = Console.ReadLine();
          }
    
          Console.WriteLine(a); 
          Console.WriteLine(b);
    
          Console.ReadLine();
          return;
    
          // ----------------------------------------------------------
          if (a == "1")
          {
            stringToPrint += "XYZ\r\n";
            Console.WriteLine(stringToPrint);
          }
    
          if (a == "2")
          {
            stringToPrint += "ABC \r\n";
            Console.WriteLine(stringToPrint);
          }
    
          //------------------------------------------------------------
    
          System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument();
          doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
    
          //margins, in hundredths of an inch,
          System.Drawing.Printing.Margins margins = new System.Drawing.Printing.Margins(50, 50, 50, 30);
          doc.DefaultPageSettings.Margins = margins;
          //... additionally:
          // http://msdn.microsoft.com/en-us/library/system.drawing.printing.pagesettings.margins(v=VS.100).aspx
    
          doc.Print();
        }
        //example from http://msdn.microsoft.com/en-us/library/cwbe712d.aspx, changed
        private static void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
          int charactersOnPage = 0;
          int linesPerPage = 0;
    
          using (Font font = new Font("MS Sans Serif", 10))
          {
            e.Graphics.MeasureString(stringToPrint, font,
             e.MarginBounds.Size, StringFormat.GenericTypographic,
             out charactersOnPage, out linesPerPage);
            e.Graphics.DrawString(stringToPrint, font, Brushes.Black,
             e.MarginBounds, StringFormat.GenericTypographic);
          }
    
          // Remove the portion of the string that has been printed.
          stringToPrint = stringToPrint.Substring(charactersOnPage);
          // Check to see if more pages are to be printed.
          e.HasMorePages = (stringToPrint.Length > 0);
        }
    
      }
    }
    
    Regards,
      Thorsten
    Tuesday, April 12, 2011 12:00 PM
  •  

     

    How are you ?     I have not solved the problem of connecting the Files. I am still studing .

    I have another question. will you help me?

         string a = Console.ReadLine();  // input numbers are 4 cases(from 1 to 4)
         string b = Console.ReadLine();  // input numbers are 5 cases(from 1 to 5)
         string c = Console.ReadLine();  // input numbers are 3 cases(from 1 to 3)


                if (a == "1")
                {
                    if (b == "1")
                    {
                          d = 1
                    }
                }
                if (a == "1")
                {
                    if (b == "2")
                    {
                           d = 2
                    }
                }
                if (a == "1")
                {
                    if (b == "3")
                    {
                         d = 3
                    }
                }

                = = = = = = = = =

                = = = = = = = =

                = = = = = = = =

                if (a == "4")
                {
                    if (b == "4")
                    {
                          d = 19
                    }
                }
                if (a == "4")
                {
                    if (b == "5")
                    {
                          d = 20
                    }
                }
                 //    The cases of combination of a & b are 20.
                 //    This form arise error about d.


                if (c == "1")
                {
                    if (d == "1")
                    {
                        stringToPrint += "  ABC ";
                        Console.WriteLine(stringToPrint);
                    }
                }
                if (c == "1")
                {
                    if (d == "2")
                    {
                        stringToPrint += " DEF ";
                        Console.WriteLine(stringToPrint);
                    }
                }

                = = = = = = = = = = = = = = = = = = =

                = = = = = = = = = = = = = = = = = = =

                if (c == "3")
                {
                    if (d == "19")
                    {
                        stringToPrint += " UVW ";
                        Console.WriteLine(stringToPrint);
                    }
                }
                if (c == "3")
                {
                    if (d == "20")
                    {
                        stringToPrint += " XYZ ";
                        Console.WriteLine(stringToPrint);
                    }
                }
      // Is there any method that the d of the combination is automtically perceived,

       // and I can use  terms from "if (d == "1") to " if (d == "20")",

        // and errors are not arised ?

     




    Tuesday, April 19, 2011 5:06 AM
  • Hi,

    ok, this is a different topic as the former one I think, so, woulkd you mind to open a new thread next time?

    ###############################################################

    Here's one suggestion concerning your problem:

    If you really have that lot of conditions, use one variable ("d") to store the complete and unique result of combining them to one value.

    Read about flags and combining flag-values. (bitfield, bitwise Or and bitshift)

    http://www.blackwasp.co.uk/FlagsAttribute.aspx (and more of this site, explaining the situation)

     

     

      static void Main(string[] args)
      {
       int aa = 0;
       int bb = 0;
       int cc = 0;
    
       string a = Console.ReadLine();
       Int32.TryParse(a, out aa);
    
       string b = Console.ReadLine();
       Int32.TryParse(b, out bb);
    
       string c = Console.ReadLine();
       Int32.TryParse(c, out cc);
    
       //we use an int here, because we *know* by the possible values of aa, bb, cc, 
       //that the *result fits* into an int 
       int d = 0;
    
       //chack values
       if (aa > 0 && bb > 0 && cc > 0 && aa < 256 && bb < 256 && cc < 256)
       {
        d |= aa;
        d |= (bb << 8); //bitshift 8 to left (= * 256)
        d |= (cc << 16); //bitshift 16 to left (= * 65536)
    
        Console.WriteLine(d.ToString());
       }
       else
        Console.WriteLine("Variables must be in Range from 1 to 255");
    
       Console.ReadLine();
      }
    

     

     

    Then, if you have your unique value for d, create a switch and check the states of d for the further execution-route of your code.

    http://msdn.microsoft.com/en-us/library/06tc147t.aspx

     

     

       switch (d)
       {
        case 65793: //1+256+65536 (aa = 1, bb= 1, cc = 1)
         Dosomething();
         break;
    
        case 65794: //aa = 2, bb = 1, cc = 1
         DosomethingElse();
         break;
    
        case 65796: //aa = 4, bb = 1, cc = 1
         DosomethingCompleteElse();
         break;
    
        //...
    
        default:
         break;
       }
    
    But maybe there is a more human-readable solution to your problem, that depends on the possible values of your variables and the (number of) 
    
    possible execution-ways in your program... So, mybe re-think the whole situation.
    
    Regards,
    
     Thorsten
    

     


    • Marked as answer by Lie You Wednesday, April 20, 2011 3:55 AM
    Wednesday, April 20, 2011 12:59 AM
  • Thank you for your kindness.

    1) problem 1

    " switch (d) , Dosomthing, and break " in your reply have red underline.

    2) problem 2

    " d " have to combine with " c "

    So, I have to use " string d " instead of " int d ",  and " int d " must be changed  to " string d ""

    I 'd like to use next form

       if (c == "1")

       {

             if(d == "1")      // instead of " if(d == 1)

            {

                    stringToPrint += "  XYZ";

                   Console.WriteLine(stringToPrint);

             }

        }

     

    Wednesday, April 20, 2011 5:54 AM
  •  

       // and I can use  terms from "if (d == "1") to " if (d == "20")",

        // and errors are not arised ?




    Hi,

    use d as a string-type then... but really, overthink the whole process again, you will end up with a lot of if-conditions this way... maybe read a c#-book...

    string d = "0";

        if (a == "4")
        {
         if (b == "4")
         {
          d = "19" ;
        }
         if (b == "5")
         {
          d = "20"; 
        }
        }
    
     

     

    Regards,

      Thorsten


    Wednesday, April 20, 2011 6:56 AM
  • Thank you.  Your help very useful to me.

    1) I know the method that " string g " is changed to " int f "

         string g = Console.ReadLine();

             int f = Convert.ToInt32(g);

    2) I don't know the method that " int g " is changed to " string f "

       How to do?

    Thursday, April 21, 2011 7:04 AM
  • Hi,

    string f = g.ToString();

    Generally, I suggest to buy a book about c# and programming. There are lots of books available, some even free in the www. This could be a great help and a good way to start into writing your own programs.

    Regards,

      Thorsten

    Thursday, April 21, 2011 12:56 PM
  • Thank you very, very much.

    With your help, I have solved all my program problems,

    except 1 problem connecting files.  That is ---- .

    error message is " can not find file " in clause of

    "p.StartInfo.FileName = @"C:\Documents and Settings\권창길\바탕화면\파일2\ConsoleApplication2\bin\Debug\ConsoleApplication2.exe"; "

    I am finding the reason why can't find.


    Thursday, April 21, 2011 5:38 PM
  • Thank you very, very much.

    With your help, I have solved all my program problems,

    except 1 problem connecting files.  That is ---- .

    error message is " can not find file " in clause of

    "p.StartInfo.FileName = @"C:\Documents and Settings\권창길\바탕화면\파일2\ConsoleApplication2\bin\Debug\ConsoleApplication2.exe"; "

    I am finding the reason why can't find.



    Hi,

    for that; I only can say, make sure the file exists at the specified location. If the file exists and you still get that error, make sure that there's no hidden/unprintable character in the string you define. Sometimes it is the best to *copy* the path to the directory where the file resides to the string, input a backslash manually to the string and then again *copy* the filename (plus extension).

    Regards,

      Thorsten


    • Proposed as answer by Lie You Friday, April 22, 2011 1:29 AM
    Thursday, April 21, 2011 11:30 PM
  •  

    Thank you.

    I hardly understand how to change the string and how to do,  as you mean.

    Would you show me as " @"C:\Documents and Settings\권창길\바탕화면\파일2\ConsoleApplication2\bin\Debug\ConsoleApplication2.exe" " as what you mean ,  or other method ?

    '권창길' is my name,  '파일 2' is file2 and icon name.  

     

    Friday, April 22, 2011 4:25 PM
  • Ok,

    - In your File-System - navigate to the Location of the file. (= Open the folder that contains the file "ConsoleApplication2.exe")

    - Click into the Adressbar and copy the string showing up there.

    - Paste that to the codefile right after p.StartInfo.FileName = @"

    - Insert a Backslash manually to the string in your codefile

    - Go back to the open folder, that contains the file and select the file by clicking once onto its Icon. Right click and select - maybe in different language "rename". Copy the entire filename.

    - Paste this to the string in your codefile

    - If you supress Extensions of files in your System, write the files extension (= ".exe") to the string in your code file.

    So it is ensured that:

     a) The file to run is existent.

     b) the string in your codefile has no hidden characters that maybe lead to the error you mentioned.

    1.:

    http://cid-d5e5bd21dbf5e4e9.office.live.com/self.aspx/ng^_demo/ng^_Fragen/Images/FileFileSystem.png

    Paste this to the string in your codefile. Add a "\" (Backslash) to the string and

    2.:

    http://cid-d5e5bd21dbf5e4e9.office.live.com/self.aspx/ng^_demo/ng^_Fragen/Images/FileFileSystem2.png

    Now paste that to the string...

     

    Regards,

      Thgorsten

     

     

    Friday, April 22, 2011 10:39 PM