Date Time Picker Demo


   

/*
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;

namespace DateTimePicker
{
    /// <summary>
    /// Summary description for DateTimePicker1.
    /// </summary>
    public class DateTimePicker1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.DateTimePicker dateTimePicker1;
        private System.Windows.Forms.DateTimePicker dateTimePicker2;
        private System.Windows.Forms.DateTimePicker dateTimePicker3;
        private System.Windows.Forms.DateTimePicker dateTimePicker4;
        private System.Windows.Forms.DateTimePicker dateTimePicker5;
        private System.Windows.Forms.DateTimePicker dateTimePicker6;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Label label7;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public DateTimePicker1()
        {
            //
            // 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.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
            this.dateTimePicker2 = new System.Windows.Forms.DateTimePicker();
            this.dateTimePicker3 = new System.Windows.Forms.DateTimePicker();
            this.dateTimePicker4 = new System.Windows.Forms.DateTimePicker();
            this.dateTimePicker5 = new System.Windows.Forms.DateTimePicker();
            this.dateTimePicker6 = new System.Windows.Forms.DateTimePicker();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.label7 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // dateTimePicker1
            // 
            this.dateTimePicker1.CustomFormat = "hh:mm:s : MM/dd/yy";
            this.dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            this.dateTimePicker1.Location = new System.Drawing.Point(16, 16);
            this.dateTimePicker1.Name = "dateTimePicker1";
            this.dateTimePicker1.Size = new System.Drawing.Size(240, 20);
            this.dateTimePicker1.TabIndex = 0;
            // 
            // dateTimePicker2
            // 
            this.dateTimePicker2.CustomFormat = "h/mm/ss : dd.MM.yyyy";
            this.dateTimePicker2.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            this.dateTimePicker2.Location = new System.Drawing.Point(16, 56);
            this.dateTimePicker2.Name = "dateTimePicker2";
            this.dateTimePicker2.Size = new System.Drawing.Size(240, 20);
            this.dateTimePicker2.TabIndex = 0;
            // 
            // dateTimePicker3
            // 
            this.dateTimePicker3.CustomFormat = "HH-mm-ss : MMMM/dd/yyyy  tt";
            this.dateTimePicker3.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            this.dateTimePicker3.Location = new System.Drawing.Point(16, 88);
            this.dateTimePicker3.Name = "dateTimePicker3";
            this.dateTimePicker3.Size = new System.Drawing.Size(240, 20);
            this.dateTimePicker3.TabIndex = 0;
            // 
            // dateTimePicker4
            // 
            this.dateTimePicker4.CustomFormat = "tt hh/mm/ss : dddd.MMMM.yyyy";
            this.dateTimePicker4.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            this.dateTimePicker4.Location = new System.Drawing.Point(16, 200);
            this.dateTimePicker4.Name = "dateTimePicker4";
            this.dateTimePicker4.Size = new System.Drawing.Size(240, 20);
            this.dateTimePicker4.TabIndex = 0;
            // 
            // dateTimePicker5
            // 
            this.dateTimePicker5.CustomFormat = "HH/mm/ss : ddd-MM-yy";
            this.dateTimePicker5.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            this.dateTimePicker5.Location = new System.Drawing.Point(16, 160);
            this.dateTimePicker5.Name = "dateTimePicker5";
            this.dateTimePicker5.Size = new System.Drawing.Size(240, 20);
            this.dateTimePicker5.TabIndex = 0;
            // 
            // dateTimePicker6
            // 
            this.dateTimePicker6.CustomFormat = "hh/mm/ss : dddd MMMM yyyy tt";
            this.dateTimePicker6.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
            this.dateTimePicker6.Location = new System.Drawing.Point(16, 128);
            this.dateTimePicker6.Name = "dateTimePicker6";
            this.dateTimePicker6.Size = new System.Drawing.Size(240, 20);
            this.dateTimePicker6.TabIndex = 0;
            this.dateTimePicker6.Value = new System.DateTime(2002, 5, 14, 14, 28, 0, 0);
            // 
            // label1
            // 
            this.label1.Font = new System.Drawing.Font("Courier New", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.label1.Location = new System.Drawing.Point(264, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(352, 23);
            this.label1.TabIndex = 1;
            this.label1.Text = "Format : hh:mm:ss : MM/dd/yy";
            // 
            // label2
            // 
            this.label2.Font = new System.Drawing.Font("Courier New", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.label2.Location = new System.Drawing.Point(264, 56);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(352, 23);
            this.label2.TabIndex = 2;
            this.label2.Text = "Format : h/mm/ss : dd.MM.yyyy";
            // 
            // label3
            // 
            this.label3.Font = new System.Drawing.Font("Courier New", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.label3.Location = new System.Drawing.Point(264, 88);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(352, 23);
            this.label3.TabIndex = 3;
            this.label3.Text = "Format : HH-mm-ss : MMMM/dd/yyyy  tt";
            // 
            // label4
            // 
            this.label4.Font = new System.Drawing.Font("Courier New", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.label4.Location = new System.Drawing.Point(264, 160);
            this.label4.Name = "label4";
            this.label4.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.label4.Size = new System.Drawing.Size(352, 23);
            this.label4.TabIndex = 5;
            this.label4.Text = "Format : HH/mm/ss : ddd-MM-yy";
            // 
            // label5
            // 
            this.label5.Font = new System.Drawing.Font("Courier New", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.label5.Location = new System.Drawing.Point(264, 128);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(352, 23);
            this.label5.TabIndex = 4;
            this.label5.Text = "Format : hh/mm/ss : dddd MMMM yyyy tt";
            // 
            // label7
            // 
            this.label7.Font = new System.Drawing.Font("Courier New", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.label7.Location = new System.Drawing.Point(264, 200);
            this.label7.Name = "label7";
            this.label7.Size = new System.Drawing.Size(352, 23);
            this.label7.TabIndex = 6;
            this.label7.Text = "Format : tt hh/mm/ss : dddd.MMMM.yyyy";
            // 
            // DateTimePicker1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(632, 245);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.label7,
                                                                          this.label4,
                                                                          this.label5,
                                                                          this.label3,
                                                                          this.label2,
                                                                          this.label1,
                                                                          this.dateTimePicker1,
                                                                          this.dateTimePicker2,
                                                                          this.dateTimePicker3,
                                                                          this.dateTimePicker4,
                                                                          this.dateTimePicker5,
                                                                          this.dateTimePicker6});
            this.Name = "DateTimePicker1";
            this.Text = "DateTime Formats";
            this.Load += new System.EventHandler(this.DateTimePicker1_Load);
            this.ResumeLayout(false);

        }
        #endregion

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

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


           
          


Calendar Control


   

/*
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;

namespace Calendar
{
    /// <summary>
    /// Summary description for Calendar.
    /// </summary>
    public class Calendar : System.Windows.Forms.Form
    {
        private System.Windows.Forms.MonthCalendar monthCalendar1;
        private System.Windows.Forms.Button button1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Calendar()
        {
            //
            // 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.monthCalendar1 = new System.Windows.Forms.MonthCalendar();
         this.button1 = new System.Windows.Forms.Button();
         this.SuspendLayout();
         // 
         // monthCalendar1
         // 
         this.monthCalendar1.FirstDayOfWeek = System.Windows.Forms.Day.Thursday;
         this.monthCalendar1.Location = new System.Drawing.Point(16, 16);
         this.monthCalendar1.Name = "monthCalendar1";
         this.monthCalendar1.ShowTodayCircle = false;
         this.monthCalendar1.ShowWeekNumbers = true;
         this.monthCalendar1.TabIndex = 0;
         // 
         // button1
         // 
         this.button1.Location = new System.Drawing.Point(48, 184);
         this.button1.Name = "button1";
         this.button1.Size = new System.Drawing.Size(128, 23);
         this.button1.TabIndex = 1;
         this.button1.Text = "Selection Range";
         this.button1.Click += new System.EventHandler(this.button1_Click_1);
         // 
         // Calendar
         // 
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(232, 213);
         this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                      this.button1,
                                                                      this.monthCalendar1});
         this.Name = "Calendar";
         this.Text = "Calendar Control";
         this.Load += new System.EventHandler(this.Calendar_Load);
         this.ResumeLayout(false);

      }
        #endregion

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

        private void Calendar_Load(object sender, System.EventArgs e)
        {
            string str = monthCalendar1.Text.ToString()  ; 
        }

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

        private void button1_Click_1(object sender, System.EventArgs e)
        {
            // Get the selection Range.  
            SelectionRange sr = monthCalendar1.SelectionRange;
            // Get the start of Range.
            DateTime st = sr.Start;
            // Get the end of Range.
            DateTime se = sr.End ; 

            MessageBox.Show("RANGE START = " + st.ToString() + "
RANGE END = " + se.ToString(), "Range Selection");
    
        }
    }
}






           
          


Get value from MonthCalendar control

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

public class CarConfigForm : System.Windows.Forms.Form
{
private System.Windows.Forms.ToolTip calendarTip;
private System.Windows.Forms.Label label5;
protected System.Windows.Forms.MonthCalendar monthCalendar;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
protected System.Windows.Forms.ComboBox comboSalesPerson;
protected System.Windows.Forms.ListBox carMakeList;
protected System.Windows.Forms.CheckedListBox checkedBoxRadioOptions;
protected System.Windows.Forms.Button btnOrder;
protected System.Windows.Forms.CheckBox checkFloorMats;
protected System.Windows.Forms.RadioButton radioPink;
protected System.Windows.Forms.RadioButton radioYellow;
protected System.Windows.Forms.RadioButton radioRed;
protected System.Windows.Forms.RadioButton radioGreen;
protected System.Windows.Forms.GroupBox groupBox1;

public CarConfigForm()
{
InitializeComponent();
CenterToScreen();
}

private void InitializeComponent()
{
this.comboSalesPerson = new System.Windows.Forms.ComboBox ();
this.radioRed = new System.Windows.Forms.RadioButton ();
this.groupBox1 = new System.Windows.Forms.GroupBox ();
this.radioPink = new System.Windows.Forms.RadioButton ();
this.monthCalendar = new System.Windows.Forms.MonthCalendar ();
this.checkedBoxRadioOptions = new System.Windows.Forms.CheckedListBox ();
this.calendarTip = new System.Windows.Forms.ToolTip (new System.ComponentModel.Container());
this.radioYellow = new System.Windows.Forms.RadioButton ();
this.label5 = new System.Windows.Forms.Label ();
this.carMakeList = new System.Windows.Forms.ListBox ();
this.radioGreen = new System.Windows.Forms.RadioButton ();
this.checkFloorMats = new System.Windows.Forms.CheckBox ();
this.label3 = new System.Windows.Forms.Label ();
this.btnOrder = new System.Windows.Forms.Button ();
this.label1 = new System.Windows.Forms.Label ();
this.label2 = new System.Windows.Forms.Label ();
comboSalesPerson.Location = new System.Drawing.Point (16, 80);
comboSalesPerson.Size = new System.Drawing.Size (128, 21);
comboSalesPerson.TabIndex = 1;
comboSalesPerson.Items.AddRange(new object[4] {“A”, “B”, “C”, “D”});
radioRed.Location = new System.Drawing.Point (264, 24);
radioRed.Text = “Red”;
radioRed.Size = new System.Drawing.Size (64, 23);
radioRed.TabIndex = 2;
radioRed.TabStop = true;
radioRed.BackColor = System.Drawing.SystemColors.ControlLight;
groupBox1.Location = new System.Drawing.Point (8, 120);
groupBox1.TabIndex = 4;
groupBox1.TabStop = false;
groupBox1.Text = “Exterior Color”;
groupBox1.Size = new System.Drawing.Size (432, 56);
groupBox1.Leave += new System.EventHandler (this.groupBox1_Leave);
groupBox1.Enter += new System.EventHandler (this.groupBox1_Enter);
radioPink.Location = new System.Drawing.Point (176, 24);
radioPink.Text = “Pink”;
radioPink.Size = new System.Drawing.Size (56, 23);
radioPink.TabIndex = 3;
radioPink.TabStop = true;
radioPink.BackColor = System.Drawing.SystemColors.ControlLight;

monthCalendar.Location = new System.Drawing.Point (20, 224);
calendarTip.SetToolTip (monthCalendar, “Please select the date (or dates)”);
monthCalendar.TabIndex = 10;
monthCalendar.TabStop = true;
checkedBoxRadioOptions.Location = new System.Drawing.Point (168, 32);
checkedBoxRadioOptions.Cursor = Cursors.Hand;
checkedBoxRadioOptions.Size = new System.Drawing.Size (152, 64);
checkedBoxRadioOptions.CheckOnClick = true;
checkedBoxRadioOptions.TabIndex = 2;
checkedBoxRadioOptions.Items.AddRange(new object[6] {“1”, “2”, “3”, “4”, “5”, “6”});
calendarTip.Active = true;
radioYellow.Location = new System.Drawing.Point (96, 24);
radioYellow.Text = “Yellow”;
radioYellow.Size = new System.Drawing.Size (56, 23);
radioYellow.TabIndex = 1;
radioYellow.TabStop = true;
radioYellow.BackColor = System.Drawing.SystemColors.ControlLight;
label5.Location = new System.Drawing.Point (20, 200);
label5.Text = “Delivery Date:”;
label5.Size = new System.Drawing.Size (184, 16);
label5.Font = new System.Drawing.Font (“Microsoft Sans Serif”, 9, System.Drawing.FontStyle.Bold);
label5.TabIndex = 12;
carMakeList.Location = new System.Drawing.Point (328, 32);
carMakeList.Size = new System.Drawing.Size (112, 56);
carMakeList.ScrollAlwaysVisible = true;
carMakeList.TabIndex = 3;
carMakeList.Sorted = true;
carMakeList.Items.AddRange(new object[9] {“A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”});
radioGreen.Location = new System.Drawing.Point (16, 24);
radioGreen.Text = “Green”;
radioGreen.Size = new System.Drawing.Size (64, 23);
radioGreen.TabIndex = 0;
radioGreen.TabStop = true;
radioGreen.BackColor = System.Drawing.SystemColors.ControlLight;

checkFloorMats.Location = new System.Drawing.Point (16, 16);
checkFloorMats.Text = “Extra Floor Mats”;
checkFloorMats.Size = new System.Drawing.Size (112, 24);
checkFloorMats.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
checkFloorMats.TabIndex = 0;
label3.Location = new System.Drawing.Point (328, 8);
label3.Text = “Make:”;
label3.Size = new System.Drawing.Size (112, 16);
label3.Font = new System.Drawing.Font (“Microsoft Sans Serif”, 9, System.Drawing.FontStyle.Bold);
label3.TabIndex = 9;
btnOrder.Location = new System.Drawing.Point (8, 440);
btnOrder.Size = new System.Drawing.Size (120, 32);
btnOrder.TabIndex = 6;
btnOrder.Text = “Confirm Order”;
btnOrder.Click += new System.EventHandler (this.btnOrder_Click);
label1.Location = new System.Drawing.Point (16, 56);
label1.Text = “Sales Person”;
label1.Size = new System.Drawing.Size (144, 24);
label1.TabIndex = 7;
label2.Location = new System.Drawing.Point (176, 8);
label2.Text = “Radio Options:”;
label2.Size = new System.Drawing.Size (144, 16);
label2.Font = new System.Drawing.Font (“Microsoft Sans Serif”, 9, System.Drawing.FontStyle.Bold);
label2.TabIndex = 8;
this.Text = “Car Configurator”;
this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
this.ClientSize = new System.Drawing.Size (456, 485);
groupBox1.Controls.Add (this.radioPink);
groupBox1.Controls.Add (this.radioYellow);
groupBox1.Controls.Add (this.radioRed);
groupBox1.Controls.Add (this.radioGreen);
this.Controls.Add (this.label5);
this.Controls.Add (this.monthCalendar);
this.Controls.Add (this.label3);
this.Controls.Add (this.label2);
this.Controls.Add (this.label1);
this.Controls.Add (this.comboSalesPerson);
this.Controls.Add (this.carMakeList);
this.Controls.Add (this.checkedBoxRadioOptions);
this.Controls.Add (this.btnOrder);
this.Controls.Add (this.checkFloorMats);
this.Controls.Add (this.groupBox1);
}
static void Main()
{
Application.Run(new CarConfigForm());
}

protected void btnOrder_Click (object sender, System.EventArgs e)
{
if(comboSalesPerson.Text != “”)
Console.WriteLine(“Sales Person: ” + comboSalesPerson.Text);
else
Console.WriteLine(“You did not select a sales person!”);

if(carMakeList.SelectedItem != null)
Console.WriteLine(“Make: ” + carMakeList.SelectedItem);

if(checkFloorMats.Checked)
Console.WriteLine(“You want floor mats.”);

if(radioRed.Checked)
Console.WriteLine(“You want a red exterior.”);

if(radioYellow.Checked)
Console.WriteLine(“You want a yellow exterior.”);

if(radioGreen.Checked)
Console.WriteLine(“You want a green exterior.”);

if(radioPink.Checked)
Console.WriteLine(“Why do you want a PINK exterior?”);

for(int i = 0; i < checkedBoxRadioOptions.Items.Count; i++) { if(checkedBoxRadioOptions.GetItemChecked(i)) { Console.WriteLine("Radio Item: "+ checkedBoxRadioOptions.Items[i]); } } DateTime startD = monthCalendar.SelectionStart; DateTime endD = monthCalendar.SelectionEnd; string dateStartStr = startD.Date.ToShortDateString(); string dateEndStr = endD.Date.ToShortDateString(); if(dateStartStr != dateEndStr) { Console.WriteLine("Date between" + dateStartStr + " and" + dateEndStr); } else{ Console.WriteLine("You chose a single date:" + dateStartStr); } } protected void groupBox1_Leave (object sender, System.EventArgs e) { groupBox1.Text = "Exterior Color: Thanks for visiting the group..."; } protected void groupBox1_Enter (object sender, System.EventArgs e) { groupBox1.Text = "Exterior Color: You are in the group..."; } } [/csharp]

Paint Owner-Draw Buttons

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

class OwnerDrawButtons: Form
{
readonly int cxImage, cyImage;
readonly int cxBtn, cyBtn, dxBtn;
readonly Button btnLarger, btnSmaller;

public static void Main()
{
Application.Run(new OwnerDrawButtons());
}
public OwnerDrawButtons()
{
ResizeRedraw = true;

cxImage = 4 * Font.Height;
cyImage = 4 * Font.Height;
cxBtn = cxImage + 8;
cyBtn = cyImage + 8;
dxBtn = Font.Height;

btnLarger = new Button();
btnLarger.Parent = this;
btnLarger.Size = new Size(cxBtn, cyBtn);
btnLarger.Click += new EventHandler(ButtonLargerOnClick);
btnLarger.Paint += new PaintEventHandler(ButtonOnPaint);

btnSmaller = new Button();
btnSmaller.Parent = this;
btnSmaller.Size = new Size(cxBtn, cyBtn);
btnSmaller.Click += new EventHandler(ButtonSmallerOnClick);
btnSmaller.Paint += new PaintEventHandler(ButtonOnPaint);

OnResize(EventArgs.Empty);
}
protected override void OnResize(EventArgs ea)
{
base.OnResize(ea);

btnLarger.Location =
new Point(ClientSize.Width / 2 – cxBtn – dxBtn / 2,
(ClientSize.Height – cyBtn) / 2);
btnSmaller.Location =
new Point(ClientSize.Width / 2 + dxBtn / 2,
(ClientSize.Height – cyBtn) / 2);
}
void ButtonLargerOnClick(object obj, EventArgs ea)
{
Console.WriteLine(“clicked large”);
}
void ButtonSmallerOnClick(object obj, EventArgs ea)
{
Console.WriteLine(“clicked small”);
}
void ButtonOnPaint(object obj, PaintEventArgs pea)
{
Button btn = (Button) obj;
Graphics grfx = pea.Graphics;

ControlPaint.DrawButton(grfx, 0, 0, cxBtn, cyBtn,
(btn == (Button) GetChildAtPoint(
PointToClient(
MousePosition))) &&
btn.Capture ? ButtonState.Pushed : ButtonState.Normal);

GraphicsState grfxstate = grfx.Save();

grfx.TranslateTransform((cxBtn – cxImage) / 2, (cyBtn – cyImage) / 2);
DrawLargerButton(grfx, cxImage, cyImage);

grfx.Restore(grfxstate);

if (btn.Focused)
ControlPaint.DrawFocusRectangle(grfx,
new Rectangle((cxBtn – cxImage) / 2 + cxImage / 16,
(cyBtn – cyImage) / 2 + cyImage / 16,
7 * cxImage / 8, 7 * cyImage / 8));
}
void DrawLargerButton(Graphics grfx, int cx, int cy)
{
Brush brush = new SolidBrush(btnLarger.ForeColor);
Pen pen = new Pen(btnLarger.ForeColor);

grfx.TranslateTransform(cx / 2, cy / 2);

for (int i = 0; i < 4; i++) { grfx.DrawLine(pen, 0, 0, cx / 4, 0); grfx.FillPolygon(brush, new Point[] { new Point(cx / 4, -cy / 8), new Point(cx / 2, 0), new Point(cx / 4, cy / 8)}); grfx.RotateTransform(90); } } } [/csharp]

Popup Text


   

/*
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;
using System.Data;

namespace PopupText
{
    /// <summary>
    /// Summary description for PopupText.
    /// </summary>
    public class PopupText : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.Button cmdDelete;
        internal System.Windows.Forms.Button cmdAdd;
        internal System.Windows.Forms.HelpProvider hlp;
        private System.Windows.Forms.Label label1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public PopupText()
        {
            //
            // 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.cmdDelete = new System.Windows.Forms.Button();
            this.cmdAdd = new System.Windows.Forms.Button();
            this.hlp = new System.Windows.Forms.HelpProvider();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // cmdDelete
            // 
            this.cmdDelete.Location = new System.Drawing.Point(20, 68);
            this.cmdDelete.Name = "cmdDelete";
            this.cmdDelete.Size = new System.Drawing.Size(92, 20);
            this.cmdDelete.TabIndex = 3;
            this.cmdDelete.Text = "Delete";
            // 
            // cmdAdd
            // 
            this.cmdAdd.Location = new System.Drawing.Point(20, 16);
            this.cmdAdd.Name = "cmdAdd";
            this.cmdAdd.Size = new System.Drawing.Size(92, 20);
            this.cmdAdd.TabIndex = 2;
            this.cmdAdd.Text = "Add";
            // 
            // label1
            // 
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.label1.Location = new System.Drawing.Point(48, 176);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(208, 68);
            this.label1.TabIndex = 4;
            this.label1.Text = "Tab to a button and press F1.";
            // 
            // PopupText
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.label1,
                                                                          this.cmdDelete,
                                                                          this.cmdAdd});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "PopupText";
            this.Text = "PopupText";
            this.Load += new System.EventHandler(this.PopupText_Load);
            this.ResumeLayout(false);

        }
        #endregion

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

        private void PopupText_Load(object sender, System.EventArgs e)
        {
            hlp.SetHelpString(cmdAdd, "Choose another item from the catalog.");
        hlp.SetHelpString(cmdDelete, "Delete the selected item from your order.");
        }
    }
}



           
          


Button Generator


   

/*
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;
using System.Data;

namespace ButtonGenerator
{
    /// <summary>
    /// Summary description for ButtonGenerator.
    /// </summary>
    public class ButtonGenerator : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.GroupBox GroupBox2;
        internal System.Windows.Forms.GroupBox GroupBox1;
        internal System.Windows.Forms.Label Label1;
        internal System.Windows.Forms.TextBox txtLeft;
        internal System.Windows.Forms.Label Label2;
        internal System.Windows.Forms.Button cmdCreate;
        internal System.Windows.Forms.TextBox txtTop;
        internal System.Windows.Forms.StatusBar status;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public ButtonGenerator()
        {
            //
            // 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.GroupBox2 = new System.Windows.Forms.GroupBox();
            this.GroupBox1 = new System.Windows.Forms.GroupBox();
            this.Label1 = new System.Windows.Forms.Label();
            this.txtLeft = new System.Windows.Forms.TextBox();
            this.Label2 = new System.Windows.Forms.Label();
            this.cmdCreate = new System.Windows.Forms.Button();
            this.txtTop = new System.Windows.Forms.TextBox();
            this.status = new System.Windows.Forms.StatusBar();
            this.GroupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // GroupBox2
            // 
            this.GroupBox2.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.GroupBox2.Location = new System.Drawing.Point(0, 180);
            this.GroupBox2.Name = "GroupBox2";
            this.GroupBox2.Size = new System.Drawing.Size(368, 4);
            this.GroupBox2.TabIndex = 10;
            this.GroupBox2.TabStop = false;
            // 
            // GroupBox1
            // 
            this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                    this.Label1,
                                                                                    this.txtLeft,
                                                                                    this.Label2,
                                                                                    this.cmdCreate,
                                                                                    this.txtTop});
            this.GroupBox1.Location = new System.Drawing.Point(12, 6);
            this.GroupBox1.Name = "GroupBox1";
            this.GroupBox1.Size = new System.Drawing.Size(168, 128);
            this.GroupBox1.TabIndex = 9;
            this.GroupBox1.TabStop = false;
            // 
            // Label1
            // 
            this.Label1.Location = new System.Drawing.Point(16, 52);
            this.Label1.Name = "Label1";
            this.Label1.Size = new System.Drawing.Size(52, 16);
            this.Label1.TabIndex = 0;
            this.Label1.Text = "Left:";
            // 
            // txtLeft
            // 
            this.txtLeft.Location = new System.Drawing.Point(76, 52);
            this.txtLeft.Name = "txtLeft";
            this.txtLeft.Size = new System.Drawing.Size(68, 21);
            this.txtLeft.TabIndex = 4;
            this.txtLeft.Text = "200";
            // 
            // Label2
            // 
            this.Label2.Location = new System.Drawing.Point(16, 24);
            this.Label2.Name = "Label2";
            this.Label2.Size = new System.Drawing.Size(52, 16);
            this.Label2.TabIndex = 1;
            this.Label2.Text = "Top:";
            // 
            // cmdCreate
            // 
            this.cmdCreate.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.cmdCreate.Location = new System.Drawing.Point(36, 84);
            this.cmdCreate.Name = "cmdCreate";
            this.cmdCreate.Size = new System.Drawing.Size(112, 28);
            this.cmdCreate.TabIndex = 2;
            this.cmdCreate.Text = "Create Button";
            this.cmdCreate.Click += new System.EventHandler(this.cmdCreate_Click);
            // 
            // txtTop
            // 
            this.txtTop.Location = new System.Drawing.Point(76, 24);
            this.txtTop.Name = "txtTop";
            this.txtTop.Size = new System.Drawing.Size(68, 21);
            this.txtTop.TabIndex = 3;
            this.txtTop.Text = "50";
            // 
            // status
            // 
            this.status.Location = new System.Drawing.Point(0, 184);
            this.status.Name = "status";
            this.status.Size = new System.Drawing.Size(372, 22);
            this.status.TabIndex = 8;
            // 
            // ButtonGenerator
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(372, 206);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.GroupBox2,
                                                                          this.GroupBox1,
                                                                          this.status});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "ButtonGenerator";
            this.Text = "Button Generator";
            this.GroupBox1.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

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

        private int buttonCount = 0;

        private void cmdCreate_Click(object sender, System.EventArgs e)
        {
            buttonCount++;

            // Create the button.
            Button newButton = new Button();
            newButton.Text = "Button " + buttonCount.ToString();
            newButton.Left = int.Parse(txtLeft.Text);
            newButton.Top = int.Parse(txtTop.Text);

            // Attach the event handler.
            newButton.Click += new EventHandler(ButtonHandler);

            this.Controls.Add(newButton);

        }


        private void ButtonHandler(object sender, System.EventArgs e)
        {
            status.Text = " You clicked ... ";
            status.Text += ((Button)sender).Text;
        }

    }
}



           
          


Hot Track Button Host


   

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

Publisher: Apress
ISBN: 1590590457
*/
using System.ComponentModel;
using System.Drawing;

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

namespace HotTrackButtonHost
{
    /// <summary>
    /// Summary description for HotTrackButtonHost.
    /// </summary>
    public class HotTrackButtonHost : System.Windows.Forms.Form
    {
        private HotTrackButton hotTrackButton1;
        internal HotTrackButton hotTrackButton2;
        internal HotTrackButton HotTrackButton3;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public HotTrackButtonHost()
        {
            //
            // 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()
        {
//            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(HotTrackButtonHost));
            this.hotTrackButton1 = new HotTrackButton();
            this.hotTrackButton2 = new HotTrackButton();
            this.HotTrackButton3 = new HotTrackButton();
            this.SuspendLayout();
            // 
            // hotTrackButton1
            // 
//            this.hotTrackButton1.Image = ((System.Drawing.Bitmap)(resources.GetObject("hotTrackButton1.Image")));
            this.hotTrackButton1.Location = new System.Drawing.Point(28, 128);
            this.hotTrackButton1.Name = "hotTrackButton1";
            this.hotTrackButton1.Size = new System.Drawing.Size(168, 36);
            this.hotTrackButton1.TabIndex = 0;
            this.hotTrackButton1.Text = "Large HotTrackButton";
            // 
            // hotTrackButton2
            // 
            this.hotTrackButton2.BackColor = System.Drawing.SystemColors.Control;
            this.hotTrackButton2.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.hotTrackButton2.ForeColor = System.Drawing.Color.Black;
//            this.hotTrackButton2.Image = ((System.Drawing.Bitmap)(resources.GetObject("hotTrackButton2.Image")));
            this.hotTrackButton2.Location = new System.Drawing.Point(32, 24);
            this.hotTrackButton2.Name = "hotTrackButton2";
            this.hotTrackButton2.Size = new System.Drawing.Size(168, 20);
            this.hotTrackButton2.TabIndex = 1;
            this.hotTrackButton2.Text = "Ordinary HotTrackButton";
            // 
            // HotTrackButton3
            // 
            this.HotTrackButton3.BackColor = System.Drawing.SystemColors.Control;
            this.HotTrackButton3.Enabled = false;
            this.HotTrackButton3.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.HotTrackButton3.ForeColor = System.Drawing.Color.Black;
//            this.HotTrackButton3.Image = ((System.Drawing.Bitmap)(resources.GetObject("HotTrackButton3.Image")));
            this.HotTrackButton3.Location = new System.Drawing.Point(32, 68);
            this.HotTrackButton3.Name = "HotTrackButton3";
            this.HotTrackButton3.Size = new System.Drawing.Size(168, 28);
            this.HotTrackButton3.TabIndex = 3;
            this.HotTrackButton3.Text = "Disabled HotTrackButton";
            // 
            // HotTrackButtonHost
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.HotTrackButton3,
                                                                          this.hotTrackButton2,
                                                                          this.hotTrackButton1});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "HotTrackButtonHost";
            this.Text = "HotTrackButtonHost";
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new HotTrackButtonHost());
        }
    }
    /// <summary>
    /// Summary description for HotTrackButton.
    /// </summary>
    public class HotTrackButton : Control
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public HotTrackButton()
        {
            // 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()
        {
            components = new System.ComponentModel.Container();
        }
        #endregion


        public enum State
        {
            Normal,
            MouseOver,
            Pushed
        }

        private State state = State.Normal;

        private Image image;
        private Rectangle bounds;

        public Image Image
        {
            get
            {
                return image;
            }
            set
            {
                image = value;
                bounds = new Rectangle(0, 0, image.Width + 5, image.Height + 5);
                this.Invalidate();
            }
        }

        // You must override this property to invalidate the display and
        // provide automatic refresh when the property is changed.
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = value;
                this.Invalidate();
            }
        }
        protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
        {
            base.OnMouseMove(e);

            // Check if the mouse pointer is over the button.
            // If the mouse moves off the button surface, it will be deactivated,
            // even if the button is being held in a pressed position.
            // The code repaints the button only if needed.
            if (bounds.Contains(e.X, e.Y))
            {
                if (state == State.Normal)
                {
                    state = State.MouseOver;
                    this.Invalidate(bounds);
                }
            }
            else
            {
                if (state != State.Normal)
                {
                    state = State.Normal;
                    this.Invalidate(bounds);
                }
            }
        }

        protected override void OnMouseLeave(System.EventArgs e)
        {
            // Reset the button appearance. This will also deactivate the button
            // if it has been pressed but not released.
            // The code repaints the button only if needed.
            if (state != State.Normal)
            {
                state = State.Normal;
                this.Invalidate(bounds);
            }
        }

        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            // Change the button to a pushed state, provided the mouse pointer is
            // over the image and the Left mouse button has been clicked 
            if (bounds.Contains(e.X, e.Y) &amp;&amp; 
                (e.Button &amp; MouseButtons.Left) == MouseButtons.Left)
            {
                state = State.Pushed;
                this.Invalidate(bounds);
            }
        }

        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            // Change the button to a normal state and repaint if needed.
            if (!((e.Button &amp; MouseButtons.Left) == MouseButtons.Left))
            {
                state = State.Normal;

                if (bounds.Contains(e.X, e.Y))
                {
                    state = State.MouseOver;
                    }
                else
                {
                    state = State.Normal;
                }

                this.Invalidate(bounds);
            }
        }


        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            if (image == null)
            {
                // Draw the text without the image.
                e.Graphics.DrawString(this.Text, this.Font,
                    new SolidBrush(this.ForeColor), 10, 0);
            }
            else
            {
                if (!this.Enabled)
                {
                    // Paint the picture in a disabled state.
                    ControlPaint.DrawImageDisabled(e.Graphics, image, 2, 2,
                        this.BackColor);
                }
                else
                {
                    // Paint the image according to the button state.
                    switch (state)
                    {
                        case State.Normal:
                            e.Graphics.DrawImage(image, 2, 2);
                            break;
                        case State.MouseOver:
                            ControlPaint.DrawBorder3D(e.Graphics, bounds, 
                                Border3DStyle.Raised, Border3DSide.All);
                            e.Graphics.DrawImage(image, 2, 2);
                            break;
                        case State.Pushed:
                            ControlPaint.DrawBorder3D(e.Graphics, bounds, 
                                Border3DStyle.Sunken, Border3DSide.All);
                            e.Graphics.DrawImage(image, 3, 3);
                            break;
                    }
                }

                // Paint the caption text next to the image.
                e.Graphics.DrawString(this.Text, this.Font,
                    new SolidBrush(this.ForeColor), bounds.Width + 3,
                    (bounds.Height - this.Font.Height) / 2);
            }

        }

        protected override void OnClick(System.EventArgs e)
        {
            // Only propagate the click to the client if it was detected over the image.
            if (state == State.Pushed)
            {
                base.OnClick(e);
            }
        }


    }

    
}