Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press

Buy directly from Paradoxal Press at $33.99 (Save 43%)



Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA


Listing 18-3 extracted from chapter Windows forms applications


Listing 18-2<     > Listing 18-4


This listing can be compiled with the command line:
csc.exe /out:USRCTRL.dll /target:library Example_18_3_to_rename_USRCTRL.cs
Errors: 0 Warnings: 0


Example_18_3_to_rename_USRCTRL.cs
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

public class PercentViewer : UserControl {
   private TextBox PercentLeft;
   private TextBox PercentRight;

   // Tune the percentage value (between 0.0 and 1.0).
   private byte m_Value = 30;
   public double Value {
      set {
         if (value 0.0)      m_Value = 0;
         else if (value 1.0) m_Value = 100;
         else                  m_Value = (byte)(100.value);
         Draw();
      }
   }

   // Should we draw the grid?
   private bool m_bGrid = true;
   [Category("Appearance")]
   public bool bGrid {
      set { m_bGrid = value; Draw(); }
      get return m_bGrid; }
   }

   // Should we draw borders?
   private bool m_bBorder = true;
   [Category("Appearance")]
   public bool bBorder {
      set { m_bBorder = value; Draw(); }
      get return m_bBorder; }
   }

   // Constructor.
   public PercentViewer() { InitializeComponent(); }

   // Code to intialize controls.
   #region Component Designer generated code
   private void InitializeComponent() {
      this.PercentLeft = new TextBox();
      this.PercentRight = new TextBox();
      this.SuspendLayout();

      // PercentLeft
      this.PercentLeft.Location = new Point16);
      this.PercentLeft.Name = "PercentLeft";
      this.PercentLeft.ReadOnly = true;
      this.PercentLeft.Size = new Size2420 );
      this.PercentLeft.TabIndex = 0;
      this.PercentLeft.Text = "";
      this.PercentLeft.TextAlign = HorizontalAlignment.Center;

      // PercentRight
      this.PercentRight.Location = new Point(648);
      this.PercentRight.Name = "PercentRight";
      this.PercentRight.ReadOnly = true;
      this.PercentRight.Size = new Size2420 );
      this.PercentRight.TabIndex = 1;
      this.PercentRight.Text = "";
      this.PercentRight.TextAlign = HorizontalAlignment.Center;

      // PercentViewer
      this.Controls.AddRange(new Control[] { this.PercentRight,
                                             this.PercentLeft});
      this.Name = "PercentViewer";
      this.Size = new Size104168 );
      // Bind the 'PercentViewer_Paint' method with the 'Paint' event.
      this.Paint += new PaintEventHandlerthis.PercentViewer_Paint );
      this.ResumeLayout( false );
   }
   #endregion

   private void Draw() {
      // Fill text boxes.
      PercentLeft.Text = m_Value.ToString();
      PercentRight.Text = (100 - m_Value).ToString();

      // Ensure that the Graphics object dispose method will be called.
      using Graphics g = CreateGraphics() ) {
         // Discard last display with a white-filled rectangle.
         g.FillRectangle( Brushes.White, 339100101 );

         // If 'm_bBorder' is true then draw the border.
         if (m_bBorder)
            g.DrawRectangle( new Pen(Color.Black), 239100101 );

         // If 'm_bGrid' is true then draw the grid.
         if (m_bGrid) {
            Pen p = new Pen(Color.Gray);
            for (int = 1; i < 10; i++)
               g.DrawLine(p, 339 + i * 1010239 + i * 10);
         }

         // Draw percentage filled rectangles. 
         g.FillRectangle( Brushes.Blue, 174022, m_Value );
         g.FillRectangle( Brushes.Red, 644022100 - m_Value );
      }
   }

   // Callback procedure for the 'Paint' event.
   // To keep it simple, we are redrawing everything each time.
   private void PercentViewer_Paint( object sender,PaintEventArgs e ) {
      Draw();
   }
}	
Copyright Patrick Smacchia 2006 2007