Dock style: Fill


   


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

  public class AnchorForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Button button1;

    public AnchorForm()
    {
      InitializeComponent();
      CenterToScreen();
    }
    private void InitializeComponent()
    {
      this.button1 = new System.Windows.Forms.Button();
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button1});
      this.Text = "Anchoring (and Docking) Controls";
      
            // dock Fill

      button1.Dock = DockStyle.Fill;
      button1.Text = "Anchor: " + button1.Anchor.ToString() + 
        "
Dock: " + button1.Dock.ToString();

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

  }

           
          


Dock Style: None


   


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

  public class AnchorForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Button button1;

    public AnchorForm()
    {
      InitializeComponent();
      CenterToScreen();
    }
    private void InitializeComponent()
    {
      this.button1 = new System.Windows.Forms.Button();
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.button1});
      this.Text = "Anchoring (and Docking) Controls";
      
      
            // dock None
      button1.Dock = DockStyle.None;
      button1.Text = "Anchor: " + button1.Anchor.ToString() + "
Dock: " + button1.Dock.ToString();
      

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

  }

           
          


Dock Two Buttons

   
 


using System;
using System.Drawing;
using System.Windows.Forms;
   
class TwoButtonsDock: Form
{
     public static void Main()
     {
          Application.Run(new TwoButtonsDock());
     }
     public TwoButtonsDock()
     {
          ResizeRedraw = true;
   
          Button btn = new Button();
          btn.Parent = this;
          btn.Text   = "&Larger";
          btn.Height = 2 * Font.Height;
          btn.Dock   = DockStyle.Top;
          btn.Click += new EventHandler(ButtonLargerOnClick);
          
          btn = new Button();
          btn.Parent = this;
          btn.Text   = "&Smaller";
          btn.Height = 2 * Font.Height;
          btn.Dock   = DockStyle.Bottom;
          btn.Click += new EventHandler(ButtonSmallerOnClick);
     }
     void ButtonLargerOnClick(object obj, EventArgs ea)
     {
          Console.WriteLine("large");
     }
     void ButtonSmallerOnClick(object obj, EventArgs ea)
     {
          Console.WriteLine("small");
     }
}

    


Directory tree and property grid



   

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

Publisher: Apress
ISBN: 1590590457
*/

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

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

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

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

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

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

    }
    #endregion

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


    }

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


           
          


DirectoryTreeHost.zip( 66 k)

Define your own dialog box and get user input


   

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

  class Test
  {
    static void Main(string[] args)
    {
      SomeCustomForm myForm = new SomeCustomForm();
      myForm.Message = "Message";

      myForm.ShowDialog(new Form());

      if(myForm.DialogResult == DialogResult.OK)
      {
        Console.WriteLine(myForm.Message);
      }
    }
  }
    
    public class SomeCustomForm : System.Windows.Forms.Form
    {
        private System.ComponentModel.Container components;
    private System.Windows.Forms.Button btnCancel;
    private System.Windows.Forms.Button btnOK;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox txtMessage;

        public SomeCustomForm()
        {
            InitializeComponent();
      this.StartPosition = FormStartPosition.CenterParent;      
        }

    private string strMessage;

    public string Message
    {
      get{ return strMessage;}
      set
      { 
        strMessage = value;
        txtMessage.Text = strMessage;
      }
    }


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


    #region Windows Form Designer generated code
        private void InitializeComponent()
    {
      this.components = new System.ComponentModel.Container ();
      this.label1 = new System.Windows.Forms.Label ();
      this.btnOK = new System.Windows.Forms.Button ();
      this.btnCancel = new System.Windows.Forms.Button ();
      this.txtMessage = new System.Windows.Forms.TextBox ();
      label1.Location = new System.Drawing.Point (12, 8);
      label1.Text = "Type in your message.";
      label1.Size = new System.Drawing.Size (240, 48);
      label1.TabIndex = 1;

      btnOK.Location = new System.Drawing.Point (16, 104);
      btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
      btnOK.Size = new System.Drawing.Size (96, 24);
      btnOK.TabIndex = 2;
      btnOK.Text = "OK";
      btnOK.Click += new System.EventHandler (this.btnOK_Click);
      btnCancel.Location = new System.Drawing.Point (152, 104);
      btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
      btnCancel.Size = new System.Drawing.Size (96, 24);
      btnCancel.TabIndex = 3;
      btnCancel.Text = "Cancel";
      txtMessage.Location = new System.Drawing.Point (16, 72);
      txtMessage.TabIndex = 0;
      txtMessage.Size = new System.Drawing.Size (232, 20);
      this.Text = "Some Custom Dialog";
      this.MaximizeBox = false;
      this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
      this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
      this.ControlBox = false;
      this.MinimizeBox = false;
      this.ClientSize = new System.Drawing.Size (266, 151);
      this.Controls.Add (this.btnCancel);
      this.Controls.Add (this.btnOK);
      this.Controls.Add (this.label1);
      this.Controls.Add (this.txtMessage);
    }
    #endregion

    protected void btnOK_Click (object sender, System.EventArgs e)
    {
      // OK button clicked.
      // get new message.
      strMessage = txtMessage.Text;
    }
    }  


           
          


Use DialogResult property in Form

   
 


using System;
using System.Drawing;
using System.Windows.Forms;
   
class BetterDialog: Form
{
     public static void Main()
     {
          Application.Run(new BetterDialog());
     }
     public BetterDialog()
     {
          Menu = new MainMenu();
          Menu.MenuItems.Add("&amp;Dialog!", new EventHandler(MenuOnClick));
     }
     void MenuOnClick(object obj, EventArgs ea)
     {
          BetterDialogBox dlg = new BetterDialogBox();
          DialogResult    dr  = dlg.ShowDialog();
   
          Console.WriteLine(dr);
     }
}
class BetterDialogBox: Form
{
     public BetterDialogBox()
     {
          Text = "Better Dialog Box";
   
          FormBorderStyle = FormBorderStyle.FixedDialog;
          ControlBox      = false;
          MaximizeBox     = false;
          MinimizeBox     = false;
          ShowInTaskbar   = false;
          StartPosition   = FormStartPosition.Manual;
          Location        = ActiveForm.Location + 
                            SystemInformation.CaptionButtonSize +
                            SystemInformation.FrameBorderSize;
   
          Button btn = new Button();
          btn.Parent   = this;
          btn.Text     = "OK";
          btn.Location = new Point(50, 50);
          btn.Size     = new Size (10 * Font.Height, 2 * Font.Height);
          btn.DialogResult = DialogResult.OK;
   
          AcceptButton = btn;
   
          btn = new Button();
          btn.Parent   = this;
          btn.Text     = "Cancel";
          btn.Location = new Point(50, 100);
          btn.Size     = new Size (10 * Font.Height, 2 * Font.Height);
          btn.DialogResult = DialogResult.Cancel;
   
          CancelButton = btn;
     }
}

    


Italic User Message Dialog: your own dialog

   
 

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

public class ItalicUserMessageDialog : UserMessageDialog {
    public ItalicUserMessageDialog() {
        InitializeComponent();
    }

    public bool Italic {
        set { checkBoxItalic.Checked = value; }
        get { return checkBoxItalic.Checked; }
    }
    private void InitializeComponent() {
        this.checkBoxItalic = new System.Windows.Forms.CheckBox();
        this.SuspendLayout();

        this.checkBoxItalic.AutoSize = true;
        this.checkBoxItalic.Location = new System.Drawing.Point(12, 79);
        this.checkBoxItalic.Size = new System.Drawing.Size(50, 17);
        this.checkBoxItalic.Text = "Italic?";

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(352, 113);
        this.Controls.Add(this.checkBoxItalic);
        this.ResumeLayout(false);
        this.PerformLayout();

    }
    private System.Windows.Forms.CheckBox checkBoxItalic;
}
public class UserMessageDialog : Form {
    public UserMessageDialog() {
        InitializeComponent();
    }

    public string Message {
        set { txtUserInput.Text = value; }
        get { return txtUserInput.Text; }
    }
    private void InitializeComponent() {
        this.btnCancel = new System.Windows.Forms.Button();
        this.btnOK = new System.Windows.Forms.Button();
        this.label1 = new System.Windows.Forms.Label();
        this.txtUserInput = new System.Windows.Forms.TextBox();
        this.SuspendLayout();

        this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        this.btnCancel.Location = new System.Drawing.Point(263, 73);
        this.btnCancel.Size = new System.Drawing.Size(75, 23);
        this.btnCancel.Text = "Cancel";

        this.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK;
        this.btnOK.Location = new System.Drawing.Point(182, 73);
        this.btnOK.Size = new System.Drawing.Size(75, 23);
        this.btnOK.Text = "OK";

        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(12, 9);
        this.label1.Size = new System.Drawing.Size(132, 13);
        this.label1.Text = "Please Enter your Message";

        this.txtUserInput.Location = new System.Drawing.Point(13, 35);
        this.txtUserInput.Size = new System.Drawing.Size(325, 20);

        this.AcceptButton = this.btnOK;
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.CancelButton = this.btnCancel;
        this.ClientSize = new System.Drawing.Size(350, 113);
        this.Controls.Add(this.txtUserInput);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.btnOK);
        this.Controls.Add(this.btnCancel);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.ShowInTaskbar = false;
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    private System.Windows.Forms.Button btnCancel;
    private System.Windows.Forms.Button btnOK;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox txtUserInput;
}


public class MainWindow : Form {
    private string userMessage = "Default Message";
    private bool textIsItalic = false;

    public MainWindow() {
        InitializeComponent();
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
        Application.Exit();
    }

    private void configureToolStripMenuItem_Click(object sender, EventArgs e) {
        ItalicUserMessageDialog dlg = new ItalicUserMessageDialog();
        dlg.Message = userMessage;
        dlg.Italic = textIsItalic;

        if (DialogResult.OK == dlg.ShowDialog()) {
            userMessage = dlg.Message;
            textIsItalic = dlg.Italic;
            Invalidate();
        }
        dlg.Dispose();
    }

    private void MainWindow_Paint(object sender, PaintEventArgs e) {
        Graphics g = e.Graphics;
        Font f = null;
        if (textIsItalic)
            f = new Font("Times New Roman", 24, FontStyle.Italic);
        else
            f = new Font("Times New Roman", 24);
        g.DrawString(userMessage, f, Brushes.DarkBlue,50, 50);
    }
    private void InitializeComponent() {
        this.menuStrip1 = new System.Windows.Forms.MenuStrip();
        this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.toolsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.configureToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.menuStrip1.SuspendLayout();
        this.SuspendLayout();

        this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem,
            this.toolsToolStripMenuItem});
        this.menuStrip1.Location = new System.Drawing.Point(0, 0);
        this.menuStrip1.Size = new System.Drawing.Size(390, 24);
        this.menuStrip1.Text = "menuStrip1";

        this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.exitToolStripMenuItem});
        this.fileToolStripMenuItem.Text = "&amp;File";
        this.exitToolStripMenuItem.Text = "E&amp;xit";
        this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);

        this.toolsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.configureToolStripMenuItem});
        this.toolsToolStripMenuItem.Text = "&amp;Tools";
        this.configureToolStripMenuItem.Text = "&amp;Configure";
        this.configureToolStripMenuItem.Click += new System.EventHandler(this.configureToolStripMenuItem_Click);

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(390, 182);
        this.Controls.Add(this.menuStrip1);
        this.MainMenuStrip = this.menuStrip1;
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainWindow_Paint);
        this.menuStrip1.ResumeLayout(false);
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    private System.Windows.Forms.MenuStrip menuStrip1;
    private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem toolsToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem configureToolStripMenuItem;

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