Radio Button click event


   


using System;
using System.Drawing;
using System.Windows.Forms;
public class SelectItem : Form {
  private RadioButton square = new RadioButton();
  private RadioButton circle = new RadioButton();
  private ComboBox color = new ComboBox();

  public SelectItem( ) {
    Text = "Select Item";
    square.Text = "Square";
    circle.Text = "Circle";
    color.Text = "Choose a color";

    Size = new Size(400,250);

    int w = 20;
    square.Location = new Point(w, 30);
    circle.Location = new Point(w += 10 + square.Width, 30);
    color.Location = new Point(w += 10 + circle.Width, 30);

    color.Items.Add("Red");
    color.Items.Add("Green");
    color.Items.Add("Blue");

    Controls.Add(square);
    Controls.Add(circle);
    Controls.Add(color);

    square.CheckedChanged += new EventHandler(Checked_Changed);
    circle.CheckedChanged += new EventHandler(Checked_Changed);
    color.SelectedIndexChanged += new EventHandler(Selected_Index);
  } 
  protected void Selected_Index(Object sender, EventArgs e) {
    if (color.SelectedItem.ToString() == "Red" )
      Console.WriteLine("It is red.");
    else if (color.SelectedItem.ToString() == "Green")
      Console.WriteLine("It is green.");
    else
      Console.WriteLine("It is Blue"); 
  }

  protected void Checked_Changed(Object sender, EventArgs e) {
    if (square.Checked)
      Console.WriteLine("It is rectangle");
    else
      Console.WriteLine("Ellipse");
  }
  static void Main() {
    Application.Run(new SelectItem());
  }
}
           
          


RadioButton on a form


   

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Radio
{
  /// <summary>
  /// Summary description for FormRadio.
  /// </summary>
  public class FormRadio : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.RadioButton radioButton2;
    private System.Windows.Forms.RadioButton radioButton1;
    private System.Windows.Forms.RadioButton radioButton3;
    private System.Windows.Forms.RadioButton radioButton5;
    private System.Windows.Forms.RadioButton radioButton4;
    private System.Windows.Forms.Panel panel2;
    private System.Windows.Forms.RadioButton radioButton6;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public FormRadio()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null) 
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.panel1 = new System.Windows.Forms.Panel();
      this.radioButton2 = new System.Windows.Forms.RadioButton();
      this.radioButton1 = new System.Windows.Forms.RadioButton();
      this.radioButton3 = new System.Windows.Forms.RadioButton();
      this.radioButton5 = new System.Windows.Forms.RadioButton();
      this.radioButton4 = new System.Windows.Forms.RadioButton();
      this.panel2 = new System.Windows.Forms.Panel();
      this.radioButton6 = new System.Windows.Forms.RadioButton();
      this.panel1.SuspendLayout();
      this.panel2.SuspendLayout();
      this.SuspendLayout();
      // 
      // panel1
      // 
      this.panel1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                         this.radioButton2,
                                         this.radioButton1,
                                         this.radioButton3});
      this.panel1.Location = new System.Drawing.Point(8, 16);
      this.panel1.Name = "panel1";
      this.panel1.Size = new System.Drawing.Size(136, 136);
      this.panel1.TabIndex = 1;
      // 
      // radioButton2
      // 
      this.radioButton2.Location = new System.Drawing.Point(16, 56);
      this.radioButton2.Name = "radioButton2";
      this.radioButton2.Size = new System.Drawing.Size(96, 16);
      this.radioButton2.TabIndex = 0;
      this.radioButton2.Text = "radioButton2";
      // 
      // radioButton1
      // 
      this.radioButton1.Location = new System.Drawing.Point(16, 16);
      this.radioButton1.Name = "radioButton1";
      this.radioButton1.Size = new System.Drawing.Size(96, 16);
      this.radioButton1.TabIndex = 0;
      this.radioButton1.Text = "radioButton1";
      // 
      // radioButton3
      // 
      this.radioButton3.Location = new System.Drawing.Point(16, 96);
      this.radioButton3.Name = "radioButton3";
      this.radioButton3.Size = new System.Drawing.Size(96, 16);
      this.radioButton3.TabIndex = 0;
      this.radioButton3.Text = "radioButton3";
      // 
      // radioButton5
      // 
      this.radioButton5.Location = new System.Drawing.Point(16, 56);
      this.radioButton5.Name = "radioButton5";
      this.radioButton5.Size = new System.Drawing.Size(96, 16);
      this.radioButton5.TabIndex = 0;
      this.radioButton5.Text = "radioButton5";
      // 
      // radioButton4
      // 
      this.radioButton4.Location = new System.Drawing.Point(16, 16);
      this.radioButton4.Name = "radioButton4";
      this.radioButton4.Size = new System.Drawing.Size(96, 16);
      this.radioButton4.TabIndex = 0;
      this.radioButton4.Text = "radioButton4";
      // 
      // panel2
      // 
      this.panel2.Controls.AddRange(new System.Windows.Forms.Control[] {
                                         this.radioButton6,
                                         this.radioButton4,
                                         this.radioButton5});
      this.panel2.Location = new System.Drawing.Point(152, 16);
      this.panel2.Name = "panel2";
      this.panel2.Size = new System.Drawing.Size(136, 136);
      this.panel2.TabIndex = 2;
      // 
      // radioButton6
      // 
      this.radioButton6.Location = new System.Drawing.Point(16, 96);
      this.radioButton6.Name = "radioButton6";
      this.radioButton6.Size = new System.Drawing.Size(96, 16);
      this.radioButton6.TabIndex = 0;
      this.radioButton6.Text = "radioButton6";
      // 
      // FormRadio
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.panel1,
                                      this.panel2});
      this.Name = "FormRadio";
      this.Text = "FormRadio";
      this.panel1.ResumeLayout(false);
      this.panel2.ResumeLayout(false);
      this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
      Application.Run(new FormRadio());
    }
  }
}



           
          


Load image to RadioButton


   


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
    private System.Windows.Forms.RadioButton radioButton1;

  public Form1() {
        InitializeComponent();
  }

    private void InitializeComponent()
    {
        this.radioButton1 = new System.Windows.Forms.RadioButton();
        this.SuspendLayout();

        this.radioButton1.Image = new Bitmap("winter.jpg");
        this.radioButton1.Location = new System.Drawing.Point(12, 195);
        this.radioButton1.Name = "radioButton1";
        this.radioButton1.Size = new System.Drawing.Size(224, 47);
        this.radioButton1.TabIndex = 3;
        this.radioButton1.TabStop = true;
        this.radioButton1.Text = "radioButton1";
        this.radioButton1.UseVisualStyleBackColor = true;
  
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(299, 271);

        this.Controls.Add(this.radioButton1);
        this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Name = "ImagesInCommonControls";
        this.Text = "ImagesInCommonControls";
        this.ResumeLayout(false);

    }

  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }

}


           
          


ProgressBar Host


   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/

using System.Drawing;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ProgressBarHost
{
    /// <summary>
    /// Summary description for ProgressBarHost.
    /// </summary>
    public class ProgressBarHost : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.Timer tmrIncrementBar;
        private System.Windows.Forms.Button cmdStart;
        private Progress status;
        private System.ComponentModel.IContainer components;

        public ProgressBarHost()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.status = new Progress();
            this.tmrIncrementBar = new System.Windows.Forms.Timer(this.components);
            this.cmdStart = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // status
            // 
            this.status.Location = new System.Drawing.Point(12, 8);
            this.status.Name = "status";
            this.status.Size = new System.Drawing.Size(272, 88);
            this.status.TabIndex = 0;
            // 
            // tmrIncrementBar
            // 
            this.tmrIncrementBar.Interval = 1000;
            this.tmrIncrementBar.Tick += new System.EventHandler(this.tmrIncrementBar_Tick);
            // 
            // cmdStart
            // 
            this.cmdStart.Location = new System.Drawing.Point(88, 152);
            this.cmdStart.Name = "cmdStart";
            this.cmdStart.Size = new System.Drawing.Size(92, 24);
            this.cmdStart.TabIndex = 1;
            this.cmdStart.Text = "Start";
            this.cmdStart.Click += new System.EventHandler(this.cmdStart_Click);
            // 
            // ProgressBarHost
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(292, 194);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.cmdStart,
                                                                          this.status});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "ProgressBarHost";
            this.Text = "ProgressBarHost";
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new ProgressBarHost());
        }

        private void tmrIncrementBar_Tick(object sender, System.EventArgs e)
        {
            status.PerformStep();
            if (status.Maximum == status.Value)
            {
                tmrIncrementBar.Enabled = false;
            }

        }

        private void cmdStart_Click(object sender, System.EventArgs e)
        {
            tmrIncrementBar.Enabled = false;

            status.Value = 0;
            status.Maximum = 20;
            status.Step = 1;

            tmrIncrementBar.Enabled = true;

        }

    }
    /// <summary>
    /// Summary description for Progress.
    /// </summary>
    public class Progress : System.Windows.Forms.UserControl
    {
        internal System.Windows.Forms.Label lblProgress;
        internal System.Windows.Forms.ProgressBar Bar;
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Progress()
        {
            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();

            // TODO: Add any initialization after the InitForm call

        }

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Component Designer generated code
        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.lblProgress = new System.Windows.Forms.Label();
            this.Bar = new System.Windows.Forms.ProgressBar();
            this.SuspendLayout();
            // 
            // lblProgress
            // 
            this.lblProgress.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.lblProgress.Location = new System.Drawing.Point(5, 46);
            this.lblProgress.Name = "lblProgress";
            this.lblProgress.Size = new System.Drawing.Size(152, 16);
            this.lblProgress.TabIndex = 3;
            this.lblProgress.Text = "0% Done";
            this.lblProgress.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            // 
            // Bar
            // 
            this.Bar.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.Bar.Location = new System.Drawing.Point(5, 6);
            this.Bar.Name = "Bar";
            this.Bar.Size = new System.Drawing.Size(154, 32);
            this.Bar.TabIndex = 2;
            // 
            // Progress
            // 
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.lblProgress,
                                                                          this.Bar});
            this.Name = "Progress";
            this.Size = new System.Drawing.Size(164, 68);
            this.ResumeLayout(false);

        }
        #endregion
        
        [Description("The current value (between 0 and Maximum) which sets the position of the progress bar"), 
        Category("Behavior"), DefaultValue(0)]
        public int Value
        {
            get
            {
                return Bar.Value;
            }
            set
            {
                Bar.Value = value;
                UpdateLabel();
            }
        }

        public int Maximum
        {
            get
            {
                return Bar.Maximum;
            }
            set
            {
                Bar.Maximum = value;
            }
        }

        public int Step
        {
            get
            {
                return Bar.Step;
            }
            set
            {
                Bar.Step = value;
            }
        }

        public void PerformStep()
        {
            Bar.PerformStep();
            UpdateLabel();
        }

        private void UpdateLabel()
        {
            lblProgress.Text = (Math.Round((decimal)(Bar.Value * 100) /
                Bar.Maximum)).ToString();
            lblProgress.Text += "% Done";
        }
    }



}


           
          


Directory tree and property grid



   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace DirectoryTreeHost
{
  /// <summary>
  /// Summary description for DirectoryTreeHost.
  /// </summary>
  public class DirectoryTreeHost : System.Windows.Forms.Form
  {
    private DirectoryTreeControl.DirectoryTree dirTree;
    private System.Windows.Forms.PropertyGrid propertyGrid1;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public DirectoryTreeHost()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if(components != null)
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.dirTree = new DirectoryTreeControl.DirectoryTree();
      this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
      this.SuspendLayout();
      // 
      // dirTree
      // 
      this.dirTree.Drive = &#039;C&#039;;
      this.dirTree.ImageIndex = -1;
      this.dirTree.Location = new System.Drawing.Point(292, 12);
      this.dirTree.Name = "dirTree";
      
      this.dirTree.SelectedImageIndex = -1;
      this.dirTree.Size = new System.Drawing.Size(276, 272);
      this.dirTree.TabIndex = 0;
      // 
      // propertyGrid1
      // 
      this.propertyGrid1.CommandsVisibleIfAvailable = true;
      this.propertyGrid1.LargeButtons = false;
      this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
      this.propertyGrid1.Location = new System.Drawing.Point(12, 12);
      this.propertyGrid1.Name = "propertyGrid1";
      this.propertyGrid1.SelectedObject = this.propertyGrid1;
      this.propertyGrid1.Size = new System.Drawing.Size(260, 272);
      this.propertyGrid1.TabIndex = 1;
      this.propertyGrid1.Text = "propertyGrid1";
      this.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window;
      this.propertyGrid1.ViewForeColor = System.Drawing.SystemColors.WindowText;
      // 
      // DirectoryTreeHost
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
      this.ClientSize = new System.Drawing.Size(592, 298);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.propertyGrid1,
                                      this.dirTree});
      this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.Name = "DirectoryTreeHost";
      this.Text = "DirectoryTreeHost";
      this.Load += new System.EventHandler(this.DirectoryTreeHost_Load);
      this.ResumeLayout(false);

    }
    #endregion

    private void DirectoryTreeHost_Load(object sender, System.EventArgs e)
    {


    }

    public static void Main()
    {
      Application.Run(new DirectoryTreeHost());
    }
  }
}


           
          


DirectoryTreeHost.zip( 66 k)

Printer Caps 3

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Drawing.Printing;

namespace PrinterCaps3
{
public class PrinterCaps3 : System.Windows.Forms.Form
{
private int m_nCurrPrinter;

private System.Drawing.Printing.PrintDocument printDoc;
private System.Windows.Forms.PrintPreviewDialog ppDialog;
private System.Windows.Forms.Button btnPrint;
private System.ComponentModel.Container components = null;

public PrinterCaps3()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}

///

/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///

/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
// System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(PrinterCaps3));
this.printDoc = new System.Drawing.Printing.PrintDocument();
this.ppDialog = new System.Windows.Forms.PrintPreviewDialog();
this.btnPrint = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// printDoc
//
this.printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDoc_PrintPage);
//
// ppDialog
//
this.ppDialog.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.ppDialog.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.ppDialog.ClientSize = new System.Drawing.Size(400, 300);
this.ppDialog.Enabled = true;
// this.ppDialog.Icon = ((System.Drawing.Icon)(resources.GetObject(“ppDialog.Icon”)));
this.ppDialog.Location = new System.Drawing.Point(195, 22);
this.ppDialog.MaximumSize = new System.Drawing.Size(0, 0);
this.ppDialog.Name = “ppDialog”;
this.ppDialog.Opacity = 1;
this.ppDialog.TransparencyKey = System.Drawing.Color.Empty;
this.ppDialog.Visible = false;
//
// btnPrint
//
this.btnPrint.Location = new System.Drawing.Point(52, 28);
this.btnPrint.Name = “btnPrint”;
this.btnPrint.TabIndex = 0;
this.btnPrint.Text = “Print”;
this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
//
// PrinterCaps3
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(176, 86);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnPrint});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = “PrinterCaps3”;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = “Printer Caps”;
this.ResumeLayout(false);

}
#endregion

///

/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Application.Run(new PrinterCaps3());
}

private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
PrinterSettings pSettings = new PrinterSettings();
Font printFont = new Font(“Arial”, 12);

//Use the Margins
int nTextPosY = e.MarginBounds.Top;
int nTextPosX = e.MarginBounds.Left;
int nHeight = (int)printFont.GetHeight(e.Graphics);

//Height of a printer block
int nBlockHeight = 9 * nHeight;

//Loop through using indexor now
//Start with the previous index in m_nCurrPrinter
for(int x = m_nCurrPrinter;x< PrinterSettings.InstalledPrinters.Count; x++)//each(string sPtr in PrinterSettings.InstalledPrinters) { pSettings.PrinterName = PrinterSettings.InstalledPrinters[x]; if(pSettings.IsValid) { //Ensure this printer block can fit on the page if(nTextPosY + nBlockHeight < e.MarginBounds.Bottom) { //Print the caps of the printer e.Graphics.DrawString(PrinterSettings.InstalledPrinters[x], printFont,Brushes.Black, nTextPosX, nTextPosY + 5); e.Graphics.DrawString("Can Duplex: " + pSettings.CanDuplex.ToString(), printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight)); e.Graphics.DrawString("Is Default: " + pSettings.IsDefaultPrinter.ToString(), printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight*2)); e.Graphics.DrawString("Is Plotter: " + pSettings.IsPlotter.ToString(), printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight*3)); e.Graphics.DrawString("Landscape Angle: " + pSettings.LandscapeAngle.ToString(), printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight*4)); e.Graphics.DrawString("Maximum Copies: " + pSettings.MaximumCopies.ToString(), printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight*5)); e.Graphics.DrawString("Maximum Page: " + pSettings.MaximumPage.ToString(), printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight*6)); e.Graphics.DrawString("Minimum Page: " + pSettings.MinimumPage.ToString(), printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight*7)); e.Graphics.DrawString("Supports Color: " + pSettings.SupportsColor.ToString(), printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight*8)); nTextPosY = nTextPosY + ((5 + nHeight*8) + nHeight); } else { //Could nto fit block on the page - need more pages m_nCurrPrinter = x; e.HasMorePages = true; return; } } } //Last page if we reached here e.HasMorePages = false; m_nCurrPrinter = 0; return; } private void btnPrint_Click(object sender, System.EventArgs e) { //Set to defaults m_nCurrPrinter = 0; try { ppDialog.Document = printDoc; ppDialog.ShowDialog(); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } } } } [/csharp]

Printer Caps 4

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Drawing.Printing;
using System.Drawing.Drawing2D;

namespace PrinterCaps4
{
public class PrinterCaps4 : System.Windows.Forms.Form
{
private int m_nCurrPrinter;

private System.Drawing.Printing.PrintDocument printDoc;
private System.Windows.Forms.PrintPreviewDialog ppDialog;
private System.Windows.Forms.Button btnPrint;
private System.ComponentModel.Container components = null;

public PrinterCaps4()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}

///

/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///

/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
// System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(PrinterCaps4));
this.printDoc = new System.Drawing.Printing.PrintDocument();
this.ppDialog = new System.Windows.Forms.PrintPreviewDialog();
this.btnPrint = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// printDoc
//
this.printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDoc_PrintPage);
//
// ppDialog
//
this.ppDialog.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.ppDialog.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.ppDialog.ClientSize = new System.Drawing.Size(400, 300);
this.ppDialog.Enabled = true;
// this.ppDialog.Icon = ((System.Drawing.Icon)(resources.GetObject(“ppDialog.Icon”)));
this.ppDialog.Location = new System.Drawing.Point(195, 22);
this.ppDialog.MaximumSize = new System.Drawing.Size(0, 0);
this.ppDialog.Name = “ppDialog”;
this.ppDialog.Opacity = 1;
this.ppDialog.TransparencyKey = System.Drawing.Color.Empty;
this.ppDialog.Visible = false;
//
// btnPrint
//
this.btnPrint.Location = new System.Drawing.Point(52, 28);
this.btnPrint.Name = “btnPrint”;
this.btnPrint.TabIndex = 0;
this.btnPrint.Text = “Print”;
this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
//
// PrinterCaps4
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(176, 86);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnPrint});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = “PrinterCaps4”;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = “Printer Caps”;
this.ResumeLayout(false);

}
#endregion

///

/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Application.Run(new PrinterCaps4());
}

private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
PrinterSettings pSettings = new PrinterSettings();
Font printFont = new Font(“Arial”, 12);

//Use the Margins
int nTextPosY = e.MarginBounds.Top;
int nTextPosX = e.MarginBounds.Left;
int nHeight = (int)printFont.GetHeight(e.Graphics);

//Height of a printer block
int nBlockHeight = 9 * nHeight;

//Print Background Graphic
LinearGradientBrush aBrush =
new LinearGradientBrush(e.MarginBounds,Color.FromArgb(100,Color.LightBlue),
Color.FromArgb(100,Color.Blue),LinearGradientMode.ForwardDiagonal);
e.Graphics.FillRectangle(aBrush,e.MarginBounds);

//Loop through using indexor now
//Start with the previous index in m_nCurrPrinter
for(int x = m_nCurrPrinter;x< PrinterSettings.InstalledPrinters.Count; x++)//each(string sPtr in PrinterSettings.InstalledPrinters) { pSettings.PrinterName = PrinterSettings.InstalledPrinters[x]; if(pSettings.IsValid) { //Ensure this printer block can fit on the page if(nTextPosY + nBlockHeight < e.MarginBounds.Bottom) { //Print the caps of the printer e.Graphics.DrawString(PrinterSettings.InstalledPrinters[x], printFont,Brushes.Black, nTextPosX, nTextPosY + 5); e.Graphics.DrawString("Can Duplex: " + pSettings.CanDuplex.ToString(), printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight)); e.Graphics.DrawString("Is Default: " + pSettings.IsDefaultPrinter.ToString(), printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight*2)); e.Graphics.DrawString("Is Plotter: " + pSettings.IsPlotter.ToString(), printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight*3)); e.Graphics.DrawString("Landscape Angle: " + pSettings.LandscapeAngle.ToString(), printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight*4)); e.Graphics.DrawString("Maximum Copies: " + pSettings.MaximumCopies.ToString(), printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight*5)); e.Graphics.DrawString("Maximum Page: " + pSettings.MaximumPage.ToString(), printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight*6)); e.Graphics.DrawString("Minimum Page: " + pSettings.MinimumPage.ToString(), printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight*7)); e.Graphics.DrawString("Supports Color: " + pSettings.SupportsColor.ToString(), printFont,Brushes.Black, nTextPosX + 10, nTextPosY + (5 + nHeight*8)); nTextPosY = nTextPosY + ((5 + nHeight*8) + nHeight); //Draw line after each e.Graphics.DrawLine(System.Drawing.Pens.Black,nTextPosX,nTextPosY,e.MarginBounds.Right - 10,nTextPosY); e.Graphics.FillEllipse(System.Drawing.Brushes.Black,e.MarginBounds.Right - 10,nTextPosY-5,10,10); } else { //Could nto fit block on the page - need more pages m_nCurrPrinter = x; e.HasMorePages = true; return; } } } //Last page if we reached here e.HasMorePages = false; m_nCurrPrinter = 0; return; } private void btnPrint_Click(object sender, System.EventArgs e) { //Set to defaults m_nCurrPrinter = 0; try { ppDialog.Document = printDoc; ppDialog.ShowDialog(); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } } } } [/csharp]