Change Image alignment inside a Control


   


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

  public class ButtonForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Button btnImage;
    private System.Windows.Forms.Button btnStandard;

    // Hold the current text alignment
    ContentAlignment currAlignment = ContentAlignment.MiddleCenter;
    int currEnumPos = 0;
    
    public ButtonForm()
    {
      InitializeComponent();

      // Set btnStandard as default accept.
      this.AcceptButton = btnStandard;

      CenterToScreen();
    }
    private void InitializeComponent()
    {
      this.btnStandard = new System.Windows.Forms.Button();
      this.btnImage = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // btnStandard
      // 
      this.btnStandard.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
      this.btnStandard.ForeColor = System.Drawing.SystemColors.ControlText;
      this.btnStandard.Location = new System.Drawing.Point(16, 80);
      this.btnStandard.Name = "btnStandard";
      this.btnStandard.Size = new System.Drawing.Size(312, 88);
      this.btnStandard.TabIndex = 2;
      this.btnStandard.Text = "Click to change the Image alignment";
      this.btnStandard.Click += new System.EventHandler(this.btnStandard_Click);
      // 
      // btnImage
      // 
      this.btnImage.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Bold);
      this.btnImage.Image = new Bitmap("winter.jpg");
      this.btnImage.Location = new System.Drawing.Point(16, 192);
      this.btnImage.Name = "btnImage";
      this.btnImage.Size = new System.Drawing.Size(312, 72);
      this.btnImage.TabIndex = 3;
      this.btnImage.Text = "Image Button";
      this.btnImage.TextAlign = System.Drawing.ContentAlignment.TopCenter;
      // 
      // ButtonForm
      // 
      this.AcceptButton = this.btnStandard;
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(340, 269);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.btnImage,
                                     this.btnStandard,
                                    });
      this.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
      this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
      this.Name = "ButtonForm";
      this.Text = "Buttons";
      this.ResumeLayout(false);

    }
    
    protected void btnStandard_Click (object sender, System.EventArgs e)
    {      
      Array values = Enum.GetValues(currAlignment.GetType());
    
      currEnumPos++;
      if(currEnumPos >= values.Length)
        currEnumPos = 0;
      
      currAlignment = (ContentAlignment)ContentAlignment.Parse(currAlignment.GetType(), 
              values.GetValue(currEnumPos).ToString());

      btnImage.ImageAlign = currAlignment;
    }

    public static void Main(string[] args) 
    {
      Application.Run(new ButtonForm());
    }
  }

           
          


Add a Control Programmatically


   


using System;
using System.Windows.Forms;

public class DynamicCheckBox : System.Windows.Forms.Form {

    public DynamicCheckBox(){
    
        string[] foods = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"};

        int topPosition = 10;
        foreach (string food in foods)
        {
            // Create a new check box.
            CheckBox checkBox = new CheckBox();
            checkBox.Left = 10;
            checkBox.Top = topPosition;
            topPosition += 30;
            checkBox.Text = food;

            // Add the check box to the form.
            this.Controls.Add(checkBox);
        }
    }
    public static void Main(){
       Application.Run(new DynamicCheckBox());
    }
}
           
          


Control renderer Demo: CheckBox


   

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

public class Form1 : Form
{

      public Form1() {
            InitializeComponent();
            
      }

    private void ControlRenderer_Paint(object sender, PaintEventArgs e)
    {
      
        CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(10,10),
          new Rectangle(10,10,110,15), "Style checkbox", Font,false, CheckBoxState.CheckedNormal);
      
    }
    private void InitializeComponent()
    {
      this.SuspendLayout();
      // 
      // ControlRenderer
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(292, 266);
      this.Name = "ControlRenderer";
      this.Text = "ControlRenderer";
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.ControlRenderer_Paint);
      this.ResumeLayout(false);

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

}



           
          


Control Enabled

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

class MainWindow : Form {
    private TextBox firstNameBox = new TextBox();
    private Button btnShowControls = new Button();

    public MainWindow() {
        this.Text = "Simple Controls";
        this.Width = 300;
        this.Height = 200;
        CenterToScreen();

        firstNameBox.Text = "Hello";
        firstNameBox.Size = new Size(150, 50);
        firstNameBox.Location = new Point(10, 10);
        this.Controls.Add(firstNameBox);

        btnShowControls.Text = "Click Me";
        btnShowControls.Size = new Size(90, 30);
        btnShowControls.Location = new Point(10, 70);
        btnShowControls.BackColor = Color.DodgerBlue;
        btnShowControls.Click += new EventHandler(btnShowControls_Clicked);
        this.Controls.Add(btnShowControls);
    }

    private void btnShowControls_Clicked(object sender, EventArgs e) {
        string ctrlInfo = "";
        foreach (Control c in this.Controls) {
            ctrlInfo += string.Format("Control: {0}
",c.ToString());
        }
        MessageBox.Show(ctrlInfo, "Controls on Form");
        DisableAllButtons();
    }

    private void DisableAllButtons() {
        foreach (Control c in this.Controls) {
            if (c is Button)
                ((Button)c).Enabled = false;
        }
    }
    public static void Main(string[] args) {
        Application.Run(new MainWindow());
    }
}

    


Use Control.GetType to check the control type

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

class CustomCheckBox: Form
{
public static void Main()
{
Application.Run(new CustomCheckBox());
}
public CustomCheckBox()
{
int cyText = Font.Height;
int cxText = cyText / 2;
FontStyle[] afs = { FontStyle.Bold, FontStyle.Italic,
FontStyle.Underline, FontStyle.Strikeout };

Label label = new Label();
label.Parent = this;
label.Text = “Sample Text”;

for (int i = 0; i < 4; i++) { FontStyleCheckBox chkbox = new FontStyleCheckBox(); chkbox.Parent = this; chkbox.Text = afs[i].ToString(); chkbox.fontstyle = afs[i]; chkbox.Location = new Point(2 * cxText, (4 + 3 * i) * cyText / 2); chkbox.Size = new Size(12 * cxText, cyText); chkbox.CheckedChanged += new EventHandler(CheckBoxOnCheckedChanged); } } void CheckBoxOnCheckedChanged(object obj, EventArgs ea) { FontStyle fs = 0; Label label = null; for (int i = 0; i < Controls.Count; i++) { Control ctrl = Controls[i]; if (ctrl.GetType() == typeof(Label)) label = (Label) ctrl; else if (ctrl.GetType() == typeof(FontStyleCheckBox)) if (((FontStyleCheckBox) ctrl).Checked) fs |= ((FontStyleCheckBox) ctrl).fontstyle; } label.Font = new Font(label.Font, fs); } } class FontStyleCheckBox: CheckBox { public FontStyle fontstyle; } [/csharp]

Form Anchor


   

/*
C# Programming Tips &amp; 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 Anchor
{
  /// <summary>
  /// Summary description for Form1.
  /// </summary>
  public class FormAnchor : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Button btnOK;
    private System.Windows.Forms.Button btnCancel;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button btnClear;
    private System.Windows.Forms.CheckBox checkBox1;
    private System.Windows.Forms.CheckBox checkBox2;
    private System.Windows.Forms.CheckBox checkBox3;
    private System.Windows.Forms.RadioButton radioButton1;
    private System.Windows.Forms.RadioButton radioButton2;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public FormAnchor()
    {
      //
      // 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.btnOK = new System.Windows.Forms.Button();
      this.btnCancel = new System.Windows.Forms.Button();
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.btnClear = new System.Windows.Forms.Button();
      this.checkBox1 = new System.Windows.Forms.CheckBox();
      this.checkBox2 = new System.Windows.Forms.CheckBox();
      this.checkBox3 = new System.Windows.Forms.CheckBox();
      this.radioButton1 = new System.Windows.Forms.RadioButton();
      this.radioButton2 = new System.Windows.Forms.RadioButton();
      this.SuspendLayout();
      // 
      // btnOK
      // 
      this.btnOK.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
      this.btnOK.Location = new System.Drawing.Point(106, 188);
      this.btnOK.Name = "btnOK";
      this.btnOK.Size = new System.Drawing.Size(72, 32);
      this.btnOK.TabIndex = 0;
      this.btnOK.Text = "Bottom";
      // 
      // btnCancel
      // 
      this.btnCancel.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
      this.btnCancel.Location = new System.Drawing.Point(196, 188);
      this.btnCancel.Name = "btnCancel";
      this.btnCancel.Size = new System.Drawing.Size(72, 32);
      this.btnCancel.TabIndex = 1;
      this.btnCancel.Text = "Bottom Right";
      // 
      // textBox1
      // 
      this.textBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right);
      this.textBox1.Location = new System.Drawing.Point(72, 80);
      this.textBox1.Multiline = true;
      this.textBox1.Name = "textBox1";
      this.textBox1.Size = new System.Drawing.Size(140, 60);
      this.textBox1.TabIndex = 2;
      this.textBox1.Text = "Top Bottom Left Right";
      // 
      // btnClear
      // 
      this.btnClear.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
      this.btnClear.Location = new System.Drawing.Point(16, 188);
      this.btnClear.Name = "btnClear";
      this.btnClear.Size = new System.Drawing.Size(72, 32);
      this.btnClear.TabIndex = 3;
      this.btnClear.Text = "Bottom Left";
      // 
      // checkBox1
      // 
      this.checkBox1.Location = new System.Drawing.Point(16, 8);
      this.checkBox1.Name = "checkBox1";
      this.checkBox1.Size = new System.Drawing.Size(56, 24);
      this.checkBox1.TabIndex = 4;
      this.checkBox1.Text = "Top Left";
      // 
      // checkBox2
      // 
      this.checkBox2.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
      this.checkBox2.Location = new System.Drawing.Point(228, 8);
      this.checkBox2.Name = "checkBox2";
      this.checkBox2.Size = new System.Drawing.Size(64, 24);
      this.checkBox2.TabIndex = 5;
      this.checkBox2.Text = "Top Right";
      // 
      // checkBox3
      // 
      this.checkBox3.Anchor = System.Windows.Forms.AnchorStyles.Top;
      this.checkBox3.Location = new System.Drawing.Point(106, 8);
      this.checkBox3.Name = "checkBox3";
      this.checkBox3.Size = new System.Drawing.Size(80, 24);
      this.checkBox3.TabIndex = 6;
      this.checkBox3.Text = "Top";
      // 
      // radioButton1
      // 
      this.radioButton1.Anchor = System.Windows.Forms.AnchorStyles.Left;
      this.radioButton1.Location = new System.Drawing.Point(8, 82);
      this.radioButton1.Name = "radioButton1";
      this.radioButton1.Size = new System.Drawing.Size(48, 48);
      this.radioButton1.TabIndex = 7;
      this.radioButton1.Text = "Left";
      // 
      // radioButton2
      // 
      this.radioButton2.Anchor = System.Windows.Forms.AnchorStyles.Right;
      this.radioButton2.Location = new System.Drawing.Point(228, 82);
      this.radioButton2.Name = "radioButton2";
      this.radioButton2.Size = new System.Drawing.Size(56, 48);
      this.radioButton2.TabIndex = 8;
      this.radioButton2.Text = "Right";
      // 
      // FormAnchor
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(296, 245);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.radioButton2,
                                      this.radioButton1,
                                      this.checkBox3,
                                      this.checkBox2,
                                      this.checkBox1,
                                      this.btnClear,
                                      this.textBox1,
                                      this.btnCancel,
                                      this.btnOK});
      this.Name = "FormAnchor";
      this.Text = "FormAnchor";
      this.ResumeLayout(false);

    }
    #endregion

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


           
          


Use ContentAlignment

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

class MainForm : System.Windows.Forms.Form {
    ContentAlignment currAlignment = ContentAlignment.MiddleCenter;
    int currEnumPos = 0;

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

    protected void btnStandard_Click(object sender, System.EventArgs e) {
        Array values = Enum.GetValues(currAlignment.GetType());

        currEnumPos++;
        if (currEnumPos >= values.Length)
            currEnumPos = 0;

        currAlignment = (ContentAlignment)Enum.Parse(currAlignment.GetType(),
                        values.GetValue(currEnumPos).ToString());
        btnStandard.TextAlign = currAlignment;

        btnStandard.Text = currAlignment.ToString();

        btnImage.ImageAlign = currAlignment;
    }
    private void InitializeComponent() {
        this.btnStandard = new System.Windows.Forms.Button();
        this.btnFlat = new System.Windows.Forms.Button();
        this.btnImage = new System.Windows.Forms.Button();
        this.btnPopup = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // btnStandard
        // 
        this.btnStandard.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
        this.btnStandard.ForeColor = System.Drawing.SystemColors.ControlText;
        this.btnStandard.Location = new System.Drawing.Point(16, 80);
        this.btnStandard.Name = "btnStandard";
        this.btnStandard.Size = new System.Drawing.Size(312, 88);
        this.btnStandard.TabIndex = 2;
        this.btnStandard.Text = "I am a standard button";
        this.btnStandard.Click += new System.EventHandler(this.btnStandard_Click);
        // 
        // btnFlat
        // 
        this.btnFlat.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.btnFlat.ForeColor = System.Drawing.Color.Blue;
        this.btnFlat.Location = new System.Drawing.Point(16, 24);
        this.btnFlat.Name = "btnFlat";
        this.btnFlat.Size = new System.Drawing.Size(152, 32);
        this.btnFlat.TabIndex = 0;
        this.btnFlat.Text = "I am flat...";
        // 
        // btnImage
        // 
        this.btnImage.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Bold);
        this.btnImage.ForeColor = System.Drawing.Color.Black;
        this.btnImage.Location = new System.Drawing.Point(16, 192);
        this.btnImage.Name = "btnImage";
        this.btnImage.Size = new System.Drawing.Size(312, 72);
        this.btnImage.TabIndex = 3;
        this.btnImage.Text = "Image Button";
        this.btnImage.TextAlign = System.Drawing.ContentAlignment.TopCenter;
        // 
        // btnPopup
        // 
        this.btnPopup.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
        this.btnPopup.ForeColor = System.Drawing.SystemColors.ControlText;
        this.btnPopup.Location = new System.Drawing.Point(176, 24);
        this.btnPopup.Name = "btnPopup";
        this.btnPopup.Size = new System.Drawing.Size(152, 32);
        this.btnPopup.TabIndex = 1;
        this.btnPopup.Text = "I am a Popup!";
        // 
        // MainForm
        // 
        this.ClientSize = new System.Drawing.Size(340, 269);
        this.Controls.Add(this.btnImage);
        this.Controls.Add(this.btnStandard);
        this.Controls.Add(this.btnPopup);
        this.Controls.Add(this.btnFlat);
        this.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
        this.Name = "MainForm";
        this.Text = "Buttons";
        this.ResumeLayout(false);

    }


    private System.ComponentModel.Container components = null;
    private System.Windows.Forms.Button btnImage;
    private System.Windows.Forms.Button btnStandard;
    private System.Windows.Forms.Button btnPopup;
    private System.Windows.Forms.Button btnFlat;

    [STAThread]
    public static void Main(string[] args) {
        Application.Run(new MainForm());
    }
}