Margins Constructor
Collapse the table of content
Expand the table of content

Margins Constructor

.NET Framework 1.1

Initializes a new instance of the Margins class.

Overload List

Initializes a new instance of the Margins class with 1-inch margins.

[Visual Basic] Public Sub New()
[C#] public Margins();
[C++] public: Margins();
[JScript] public function Margins();

Initializes a new instance of the Margins class with the specified left, right, top, and bottom margins.

[Visual Basic] Public Sub New(Integer, Integer, Integer, Integer)
[C#] public Margins(int, int, int, int);
[C++] public: Margins(int, int, int, int);
[JScript] public function Margins(int, int, int, int);

Example

[Visual Basic, C#, C++] Use the System.Drawing, System.Drawing.Printing, and System.IO namespaces for this example.

[Visual Basic, C#, C++] The following example sets the default page settings for a document to margins of 1 inch on each side.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the Margins constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Sub Printing()
    Try
        ' This assumes that a variable of type string, named filePath,
        ' has been set to the path of the file to print. 
        streamToPrint = New StreamReader(filePath)
        Try
            printFont = New Font("Arial", 10)
            Dim pd As New PrintDocument()
            ' This assumes that a method, named pd_PrintPage, has been
            ' defined. pd_PrintPage handles the PrintPage event. 
            AddHandler pd.PrintPage, AddressOf pd_PrintPage
            ' This assumes that a variable of type string, named
            ' printer, has been set to the printer's name. 
            pd.PrinterSettings.PrinterName = printer
            ' Create a new instance of Margins with one inch margins.
            Dim margins As New Margins(100, 100, 100, 100)
            pd.DefaultPageSettings.Margins = margins
            pd.Print()
        Finally
            streamToPrint.Close()
        End Try
    Catch ex As Exception
        MessageBox.Show("An error occurred printing the file - " & ex.Message)
    End Try
End Sub
   

[C#] 
public void Printing()
{
  try 
  {
    /* This assumes that a variable of type string, named filePath,
       has been set to the path of the file to print. */
    streamToPrint = new StreamReader (filePath);
    try 
    {
      printFont = new Font("Arial", 10);
      PrintDocument pd = new PrintDocument(); 
      /* This assumes that a method, named pd_PrintPage, has been
         defined. pd_PrintPage handles the PrintPage event. */
      pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
      /* This assumes that a variable of type string, named 
         printer, has been set to the printer's name. */
      pd.PrinterSettings.PrinterName = printer;
      // Create a new instance of Margins with one inch margins.
      Margins margins = new Margins(100,100,100,100);
      pd.DefaultPageSettings.Margins = margins;
      pd.Print();
    } 
    finally 
    {
      streamToPrint.Close() ;
    }
  } 
  catch(Exception ex) 
  { 
    MessageBox.Show("An error occurred printing the file - " + ex.Message);
  }
}


[C++] 
public:
 void Printing()
 {
   try 
   {
     /* This assumes that a variable of type string, named filePath,
        has been set to the path of the file to print. */
     streamToPrint = new StreamReader (filePath);
     try 
     {
       printFont = new System::Drawing::Font(S"Arial", 10);
       PrintDocument* pd = new PrintDocument(); 
       /* This assumes that a method, named pd_PrintPage, has been
          defined. pd_PrintPage handles the PrintPage event. */
       pd->PrintPage += new PrintPageEventHandler(this, &Sample::pd_PrintPage);
       /* This assumes that a variable of type string, named 
          printer, has been set to the printer's name. */
       pd->PrinterSettings->PrinterName = printer;
       // Create a new instance of Margins with one inch margins.
       Margins* margins = new Margins(100,100,100,100);
       pd->DefaultPageSettings->Margins = margins;
       pd->Print();
     } 
     __finally 
     {
       streamToPrint->Close() ;
     }
   } 
   catch(Exception* ex) 
   { 
     MessageBox::Show(String::Concat( S"An error occurred printing the file - ", ex->Message ));
   }
 }
 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Margins Class | Margins Members | System.Drawing.Printing Namespace

Show:
© 2016 Microsoft