Layout Manager

/*
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 LayoutManager
{
///

/// Summary description for LayoutManager.
///

public class LayoutManager : System.Windows.Forms.Form
{
internal System.Windows.Forms.TabControl tabControl1;
internal System.Windows.Forms.TabPage tabPage1;
internal System.Windows.Forms.TabPage tabPage2;
///

/// Required designer variable.
///

private System.ComponentModel.Container components = null;

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

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

///

/// 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()
{
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.tabControl1.SuspendLayout();
this.SuspendLayout();
//
// tabControl1
//
this.tabControl1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.tabControl1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.tabPage1,
this.tabPage2});
this.tabControl1.Location = new System.Drawing.Point(6, 3);
this.tabControl1.Name = “tabControl1”;
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(280, 260);
this.tabControl1.TabIndex = 1;
//
// tabPage1
//
this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Name = “tabPage1”;
this.tabPage1.Size = new System.Drawing.Size(272, 234);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = “TabPage1”;
//
// tabPage2
//
this.tabPage2.Location = new System.Drawing.Point(4, 22);
this.tabPage2.Name = “tabPage2”;
this.tabPage2.Size = new System.Drawing.Size(272, 234);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = “TabPage2”;
this.tabPage2.Visible = false;
//
// LayoutManager
//
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.tabControl1});
this.Font = new System.Drawing.Font(“Tahoma”, 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.Name = “LayoutManager”;
this.Text = “LayoutManager”;
this.Load += new System.EventHandler(this.LayoutManager_Load);
this.tabControl1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

///

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

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

private void LayoutManager_Load(object sender, System.EventArgs e)
{
// Create and attach the layout manager.
SingleLineFlow layoutManager = new SingleLineFlow(tabPage1, 20);

// Add 10 sample checkboxes.
CheckBox chkbox;

for (int i = 1; i < 11; i++) { chkbox = new CheckBox(); chkbox.Text = "Setting " + i.ToString(); tabPage1.Controls.Add(chkbox); } } } public class SingleLineFlow { private Control container; private int margin; public SingleLineFlow(Control parent, int margin) { this.container = parent; this.margin = margin; // Attach the event handler. container.Layout += new LayoutEventHandler(UpdateLayout); // Refresh the layout. UpdateLayout(this, null); } public int Margin { get { return margin; } set { margin = value; } } // This is public so it can be triggered manually if needed. public void UpdateLayout(object sender, System.Windows.Forms.LayoutEventArgs e) { int y = 0; foreach (Control ctrl in container.Controls) { y += Margin; ctrl.Left = Margin; ctrl.Top = y; ctrl.Width = container.Width; ctrl.Height = Margin; } } } } [/csharp]

Transparent Forms: holes


   

/*
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 TransparentForms
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Holes : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.PictureBox PictureBox4;
        internal System.Windows.Forms.PictureBox PictureBox2;
        internal System.Windows.Forms.PictureBox PictureBox1;
        internal System.Windows.Forms.PictureBox PictureBox3;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Holes()
        {
            //
            // 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.PictureBox4 = new System.Windows.Forms.PictureBox();
            this.PictureBox2 = new System.Windows.Forms.PictureBox();
            this.PictureBox1 = new System.Windows.Forms.PictureBox();
            this.PictureBox3 = new System.Windows.Forms.PictureBox();
            this.SuspendLayout();
            // 
            // PictureBox4
            // 
            this.PictureBox4.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(128)));
            this.PictureBox4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.PictureBox4.Location = new System.Drawing.Point(108, 108);
            this.PictureBox4.Name = "PictureBox4";
            this.PictureBox4.Size = new System.Drawing.Size(76, 76);
            this.PictureBox4.TabIndex = 10;
            this.PictureBox4.TabStop = false;
            // 
            // PictureBox2
            // 
            this.PictureBox2.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(128)));
            this.PictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.PictureBox2.Location = new System.Drawing.Point(12, 108);
            this.PictureBox2.Name = "PictureBox2";
            this.PictureBox2.Size = new System.Drawing.Size(76, 76);
            this.PictureBox2.TabIndex = 9;
            this.PictureBox2.TabStop = false;
            // 
            // PictureBox1
            // 
            this.PictureBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(128)));
            this.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.PictureBox1.Location = new System.Drawing.Point(108, 16);
            this.PictureBox1.Name = "PictureBox1";
            this.PictureBox1.Size = new System.Drawing.Size(76, 76);
            this.PictureBox1.TabIndex = 8;
            this.PictureBox1.TabStop = false;
            // 
            // PictureBox3
            // 
            this.PictureBox3.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(128)));
            this.PictureBox3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.PictureBox3.Location = new System.Drawing.Point(12, 16);
            this.PictureBox3.Name = "PictureBox3";
            this.PictureBox3.Size = new System.Drawing.Size(76, 76);
            this.PictureBox3.TabIndex = 7;
            this.PictureBox3.TabStop = false;
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(232, 234);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.PictureBox4,
                                                                          this.PictureBox2,
                                                                          this.PictureBox1,
                                                                          this.PictureBox3});
            this.Name = "Form1";
            this.Text = "A Form With Holes";
            this.TransparencyKey = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(128)), ((System.Byte)(128)));
            this.ResumeLayout(false);

        }
        #endregion

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


           
          


Transparent form


   

/*
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 Transparensy
{
    /// <summary>
    /// Summary description for Transparensy.
    /// </summary>
    public class Transparensy : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Transparensy()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            
            this.Text = "Transparency";
            //this.Opacity = 0.5;
            //this.TransparencyKey = System.Drawing.Color.Yellow;

            //
            // 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()
        {
            // 
            // Transparensy
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(248, 78);
            this.Name = "Transparensy";
            this.Text = "Transparensy";

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Transparensy());
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g;
            g = Graphics.FromHwnd(this.Handle);

            g.FillRectangle(new SolidBrush(Color.Red), 10, 10, 210, 50);
    
            // Five yellow squares with different alpha values
            Rectangle r = new Rectangle(40, 20, 30, 30);
            Color c = Color.FromArgb(255, 255, 255, 0);
            g.FillRectangle(new SolidBrush(c), r);
    
            r.Offset(30, 0);
            c = Color.FromArgb(200, 255, 255, 0);
            g.FillRectangle(new SolidBrush(c), r);
    
            r.Offset(30, 0);
            c = Color.FromArgb(150, 255, 255, 0);
            g.FillRectangle(new SolidBrush(c), r);
    
            r.Offset(30, 0);
            c = Color.FromArgb(100, 255, 255, 0);
            g.FillRectangle(new SolidBrush(c), r);
    
            r.Offset(30, 0);
            c = Color.FromArgb(50, 255, 255, 0);
            g.FillRectangle(new SolidBrush(c), r);

            g.Dispose();        
        }
    }
}

           
          


Label: Localtion, Name, Size, TabIndex, Text

   
 

using System;
using System.Windows.Forms;

class MainForm : Form
{
    private Label label1;
    private TextBox textBox1;
    private Button button1;

    public MainForm()
    {
         this.label1 = new Label();
         this.textBox1 = new TextBox();
         this.button1 = new Button();
         this.SuspendLayout();

         this.label1.Location = new System.Drawing.Point(16, 36);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(128, 16);
         this.label1.TabIndex = 0;
         this.label1.Text = "Please enter your name:"; 

         this.textBox1.Location = new System.Drawing.Point(152, 32);
         this.textBox1.Name = "textBox1";
         this.textBox1.TabIndex = 1;
         this.textBox1.Text = "";

         this.button1.Location = new System.Drawing.Point(109, 80);
         this.button1.Name = "button1";
         this.button1.TabIndex = 2;
         this.button1.Text = "Enter";
         this.button1.Click += new System.EventHandler(this.button1_Click);

         this.ClientSize = new System.Drawing.Size(292, 126);
         this.Controls.Add(this.button1);
         this.Controls.Add(this.textBox1);
         this.Controls.Add(this.label1);
         this.Name = "form1";
         this.Text = "Visual C#";

         this.ResumeLayout(false);
     }
     private void button1_Click(object sender, System.EventArgs e)
     {
        System.Console.WriteLine("User entered: " + textBox1.Text);
        MessageBox.Show("Welcome, " + textBox1.Text, "Visual C#");
     }
     [STAThread]
     public static void Main()
     {
        Application.EnableVisualStyles();
        Application.Run(new MainForm());
     }
}

    


Add image to Label and set ImageAlign to MiddleRight


   


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.Label label1;

  public Form1() {
        InitializeComponent();
  }

    private void InitializeComponent()
    {
        this.label1 = new System.Windows.Forms.Label();
        this.SuspendLayout();

        this.label1.Image = new Bitmap("winter.jpg");
        this.label1.ImageAlign = System.Drawing.ContentAlignment.TopRight;
        this.label1.Location = new System.Drawing.Point(20, 9);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(105, 77);
        this.label1.TabIndex = 0;
        this.label1.Text = "label1";

        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.label1);
        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());
  }

}


           
          


Image List Example


/*
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;
using System.Drawing.Drawing2D;
using System.IO;

namespace ImageListExample
{
///

/// Summary description for ImageListExample.
///

public class ImageListExample : System.Windows.Forms.Form
{
internal System.Windows.Forms.Button cmdDrawImageList;
internal System.Windows.Forms.Button cmdFillImageList;
///

/// Required designer variable.
///

private System.ComponentModel.Container components = null;

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

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

///

/// 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()
{
this.cmdDrawImageList = new System.Windows.Forms.Button();
this.cmdFillImageList = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// cmdDrawImageList
//
this.cmdDrawImageList.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.cmdDrawImageList.Location = new System.Drawing.Point(164, 172);
this.cmdDrawImageList.Name = “cmdDrawImageList”;
this.cmdDrawImageList.Size = new System.Drawing.Size(96, 24);
this.cmdDrawImageList.TabIndex = 3;
this.cmdDrawImageList.Text = “Draw Images”;
this.cmdDrawImageList.Click += new System.EventHandler(this.cmdDrawImageList_Click);
//
// cmdFillImageList
//
this.cmdFillImageList.FlatStyle = System.Windows.Forms.FlatStyle.System;
this.cmdFillImageList.Location = new System.Drawing.Point(28, 172);
this.cmdFillImageList.Name = “cmdFillImageList”;
this.cmdFillImageList.Size = new System.Drawing.Size(104, 24);
this.cmdFillImageList.TabIndex = 2;
this.cmdFillImageList.Text = “Fill ImageList”;
this.cmdFillImageList.Click += new System.EventHandler(this.cmdFillImageList_Click);
//
// ImageListExample
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 214);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.cmdDrawImageList,
this.cmdFillImageList});
this.Name = “ImageListExample”;
this.Text = “ImageList Example”;
this.ResumeLayout(false);

}
#endregion

///

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

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

ImageList iconImages = new System.Windows.Forms.ImageList();

private void cmdFillImageList_Click(object sender, System.EventArgs e)
{
// Configure the ImageList.
iconImages.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
iconImages.ImageSize = new System.Drawing.Size(16, 16);

// Get all the icon files in the current directory.
string[] iconFiles = Directory.GetFiles(Application.StartupPath, “*.ico”);

// Create an Image object for each file and add it to the ImageList.
// You can also use an Image subclass (like Icon).
foreach (string iconFile in iconFiles)
{
Icon newIcon = new Icon(iconFile);
iconImages.Images.Add(newIcon);
}
}

private void cmdDrawImageList_Click(object sender, System.EventArgs e)
{
// Get the graphics device context for the form.
Graphics g = this.CreateGraphics();

// Draw each image using the ImageList.Draw() method.
for (int i = 0; i < iconImages.Images.Count; i++) { iconImages.Draw(g, 30 + i * 30, 30, i); } // Release the graphics device context. g.Dispose(); } } } ImageListExample.zip( 24 k)[/csharp]

Transparent window: holes

   


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.PictureBox PictureBox4;
  private System.Windows.Forms.PictureBox PictureBox2;
  private System.Windows.Forms.PictureBox PictureBox1;
  private System.Windows.Forms.PictureBox PictureBox3;

  public Form1() {
        InitializeComponent();
  }

  private void InitializeComponent()
  {
    this.PictureBox4 = new System.Windows.Forms.PictureBox();
    this.PictureBox2 = new System.Windows.Forms.PictureBox();
    this.PictureBox1 = new System.Windows.Forms.PictureBox();
    this.PictureBox3 = new System.Windows.Forms.PictureBox();
    ((System.ComponentModel.ISupportInitialize)(this.PictureBox4)).BeginInit();
    ((System.ComponentModel.ISupportInitialize)(this.PictureBox2)).BeginInit();
    ((System.ComponentModel.ISupportInitialize)(this.PictureBox1)).BeginInit();
    ((System.ComponentModel.ISupportInitialize)(this.PictureBox3)).BeginInit();
    this.SuspendLayout();
    // 
    // PictureBox4
    // 
    this.PictureBox4.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
    this.PictureBox4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.PictureBox4.Location = new System.Drawing.Point(156, 141);
    this.PictureBox4.Name = "PictureBox4";
    this.PictureBox4.Size = new System.Drawing.Size(76, 76);
    this.PictureBox4.TabIndex = 14;
    this.PictureBox4.TabStop = false;
    // 
    // PictureBox2
    // 
    this.PictureBox2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
    this.PictureBox2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.PictureBox2.Location = new System.Drawing.Point(60, 141);
    this.PictureBox2.Name = "PictureBox2";
    this.PictureBox2.Size = new System.Drawing.Size(76, 76);
    this.PictureBox2.TabIndex = 13;
    this.PictureBox2.TabStop = false;
    // 
    // PictureBox1
    // 
    this.PictureBox1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
    this.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.PictureBox1.Location = new System.Drawing.Point(156, 49);
    this.PictureBox1.Name = "PictureBox1";
    this.PictureBox1.Size = new System.Drawing.Size(76, 76);
    this.PictureBox1.TabIndex = 12;
    this.PictureBox1.TabStop = false;
    // 
    // PictureBox3
    // 
    this.PictureBox3.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
    this.PictureBox3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.PictureBox3.Location = new System.Drawing.Point(60, 49);
    this.PictureBox3.Name = "PictureBox3";
    this.PictureBox3.Size = new System.Drawing.Size(76, 76);
    this.PictureBox3.TabIndex = 11;
    this.PictureBox3.TabStop = false;
    // 
    // Holes
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.Add(this.PictureBox4);
    this.Controls.Add(this.PictureBox2);
    this.Controls.Add(this.PictureBox1);
    this.Controls.Add(this.PictureBox3);
    this.Name = "Holes";
    this.Text = "Holes";
    this.TransparencyKey = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
    ((System.ComponentModel.ISupportInitialize)(this.PictureBox4)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(this.PictureBox2)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(this.PictureBox1)).EndInit();
    ((System.ComponentModel.ISupportInitialize)(this.PictureBox3)).EndInit();
    this.ResumeLayout(false);

  }

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

}