search c#.net

Chat With Me

คลิ๊กๆๆ...

Monday, March 16, 2009

Choosing the Printers Attached to a User's Machine in Windows Forms (c#.net)

Often, users want to choose a printer other than the default printer to print to. Choosing a printer from among those currently installed is done using the PrintDialog component. The DialogResult of the PrintDialog component is then captured and used to select the printer. For more information on capturing and using the DialogResult property of a dialog box

In the following example, a text file is selected to be printed to the default printer. The PrintDialog class is then instantiated.

To choose a printer and then print a file

  • Select the printer to be used using the PrintDialog component.

    In the following example, there are two events being handled. In the first, a Button control's Click event, the PrintDialog class is instantiated and the printer selected by the user is captured in the DialogResult property.

    In the second event, the PrintDocument component's PrintPage event, a sample document is printed to the printer specified.

    Note For more information about working with event handlers

    // C#.net
    private void button1_Click(object sender, System.EventArgs e)
    {
    PrintDialog printDialog1 = new PrintDialog();
    printDialog1.Document = printDocument1;
    DialogResult result = printDialog1.ShowDialog();
    if (result == DialogResult.OK)
    {
    printDocument1.Print();
    }
    }

    private void printDocument1_PrintPage(object sender,
    System.Drawing.Printing.PrintPageEventArgs e)
    {
    e.Graphics.FillRectangle(Brushes.Red,
    new Rectangle(500, 500, 500, 500));
    }


    Visual C#.net Be sure that the necessary code to enable the event handlers is present. In this case, they would be similar to the following:
    // C#.net
    this.printDocument1.PrintPage += new
    System.Drawing.Printing.PrintPageEventHandler
    (this.printDocument1_PrintPage);
    this.button1.Click += new System.EventHandler(this.button1_Click);

0 comments:

Post a Comment

Bejeweled

SNAKE Game - Top 20 Challenge

Learn c#.net

THE PRIMITIVE GAME - คนป่ามหาโหด