Drag and drop image to another window


   


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

public class Palette : Form
{
  private System.Windows.Forms.Label lblPictureThree;
  private System.Windows.Forms.Label lblPictureTwo;
  private System.Windows.Forms.Label lblPictureOne;

  public Palette() {
        InitializeComponent();
  }

  private void lbl_MouseDown(object sender, MouseEventArgs e)
  {
    Label lbl = (Label)sender;
    lbl.DoDragDrop(lbl.Image, DragDropEffects.Copy);

  }
  private void InitializeComponent()
  {
    this.lblPictureThree = new System.Windows.Forms.Label();
    this.lblPictureTwo = new System.Windows.Forms.Label();
    this.lblPictureOne = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // lblPictureThree
    // 
    this.lblPictureThree.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.lblPictureThree.Image = new Bitmap("winter.jpg");
    this.lblPictureThree.Location = new System.Drawing.Point(12, 113);
    this.lblPictureThree.Name = "lblPictureThree";
    this.lblPictureThree.Size = new System.Drawing.Size(56, 48);
    this.lblPictureThree.TabIndex = 6;
    this.lblPictureThree.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseDown);
    // 
    // lblPictureTwo
    // 
    this.lblPictureTwo.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.lblPictureTwo.Image = new Bitmap("winter.jpg");
    this.lblPictureTwo.Location = new System.Drawing.Point(12, 61);
    this.lblPictureTwo.Name = "lblPictureTwo";
    this.lblPictureTwo.Size = new System.Drawing.Size(56, 48);
    this.lblPictureTwo.TabIndex = 5;
    this.lblPictureTwo.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseDown);
    // 
    // lblPictureOne
    // 
    this.lblPictureOne.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
    this.lblPictureOne.Image = new Bitmap("winter.jpg");
    this.lblPictureOne.Location = new System.Drawing.Point(12, 9);
    this.lblPictureOne.Name = "lblPictureOne";
    this.lblPictureOne.Size = new System.Drawing.Size(56, 48);
    this.lblPictureOne.TabIndex = 4;
    this.lblPictureOne.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseDown);
    // 
    // Palette
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(83, 173);
    this.Controls.Add(this.lblPictureTwo);
    this.Controls.Add(this.lblPictureOne);
    this.Controls.Add(this.lblPictureThree);
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
    this.Name = "Palette";
    this.ShowInTaskbar = false;
    this.Text = "Palette";
    this.ResumeLayout(false);

  }

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

}

  public class DrawingArea : Form
  {
    private System.Windows.Forms.PictureBox picDrawingArea;

    public DrawingArea()
    {
      InitializeComponent();
    }

    private void DrawingArea_Load(object sender, EventArgs e)
    {
      Palette frmTool = new Palette();
      this.AddOwnedForm(frmTool);
      frmTool.Show();
      picDrawingArea.AllowDrop = true;
    }

    private void picDrawingArea_DragEnter(object sender, DragEventArgs e)
    {
      if (e.Data.GetDataPresent(DataFormats.Bitmap))
      {
        e.Effect = DragDropEffects.Copy;
      }
      else
      {
        e.Effect = DragDropEffects.None;
      }
    }

    private void picDrawingArea_DragDrop(object sender, DragEventArgs e)
    {
      Graphics g = picDrawingArea.CreateGraphics();
      g.DrawImage((Image)e.Data.GetData(DataFormats.Bitmap),
        new Point(e.X - this.Left, e.Y - this.Top));

    }
     private void InitializeComponent()
    {
      this.picDrawingArea = new System.Windows.Forms.PictureBox();
      ((System.ComponentModel.ISupportInitialize)(this.picDrawingArea)).BeginInit();
      this.SuspendLayout();
      // 
      // picDrawingArea
      // 
      this.picDrawingArea.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
            | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
      this.picDrawingArea.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.picDrawingArea.Location = new System.Drawing.Point(1, 2);
      this.picDrawingArea.Name = "picDrawingArea";
      this.picDrawingArea.Size = new System.Drawing.Size(377, 270);
      this.picDrawingArea.TabIndex = 2;
      this.picDrawingArea.TabStop = false;
      this.picDrawingArea.DragDrop += new System.Windows.Forms.DragEventHandler(this.picDrawingArea_DragDrop);
      this.picDrawingArea.DragEnter += new System.Windows.Forms.DragEventHandler(this.picDrawingArea_DragEnter);
      // 
      // DrawingArea
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(379, 274);
      this.Controls.Add(this.picDrawingArea);
      this.Name = "DrawingArea";
      this.Text = "Drawing Area";
      this.Load += new System.EventHandler(this.DrawingArea_Load);
      ((System.ComponentModel.ISupportInitialize)(this.picDrawingArea)).EndInit();
      this.ResumeLayout(false);
    }
  }

           
          


Drag and drop the PictureBox


   


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

  public class Form1 : System.Windows.Forms.Form
  {
    private bool  isDragging = false;
    private int   currentX, currentY;

    Rectangle dropRect = new Rectangle(180, 180, 60, 60);

    private PictureBox myPictureBox; 

    public Form1()
    {
      InitializeComponent();
      CenterToScreen();

      myPictureBox = new PictureBox();
      myPictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
      myPictureBox.Location = new System.Drawing.Point(64, 32);
      myPictureBox.Size = new System.Drawing.Size(50, 50);
      myPictureBox.Image = new Bitmap("winter.jpg");
      myPictureBox.MouseDown += new MouseEventHandler(myPictureBox_MouseDown);
      myPictureBox.MouseUp += new MouseEventHandler(myPictureBox_MouseUp);
      myPictureBox.MouseMove += new MouseEventHandler(myPictureBox_MouseMove);
      myPictureBox.Cursor = Cursors.Hand;

      Controls.Add(myPictureBox);
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Dragging Images";
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }

    static void Main() 
    {
      Application.Run(new Form1());
    }
    private void myPictureBox_MouseDown(object sender, MouseEventArgs e) 
    {
      isDragging = true;

      currentX = e.X;
      currentY = e.Y;
    }

    private void myPictureBox_MouseMove(object sender, MouseEventArgs e) {
      if (isDragging) {
        myPictureBox.Top = myPictureBox.Top + (e.Y - currentY);
        myPictureBox.Left = myPictureBox.Left + (e.X - currentX);
      }
    }
    private void myPictureBox_MouseUp(object sender, MouseEventArgs e) 
    {
      isDragging = false;
      Console.WriteLine(dropRect.Contains(myPictureBox.Bounds));
    }

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      g.FillRectangle(Brushes.AntiqueWhite, dropRect);

      g.DrawString("Drag and drop the image here.", new Font("Times New Roman", 8), Brushes.Red, dropRect);
    }
  }

           
          


Fake Drag And Drop



   

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

    public FakeDragAndDrop()
    {
      //
      // 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(FakeDragAndDrop));
      this.lblDragger = new System.Windows.Forms.Label();
      this.SuspendLayout();
      // 
      // lblDragger
      // 
      this.lblDragger.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
      this.lblDragger.Image = ((System.Drawing.Bitmap)(resources.GetObject("lblDragger.Image")));
      this.lblDragger.Location = new System.Drawing.Point(110, 105);
      this.lblDragger.Name = "lblDragger";
      this.lblDragger.Size = new System.Drawing.Size(72, 56);
      this.lblDragger.TabIndex = 2;
      this.lblDragger.MouseUp += new System.Windows.Forms.MouseEventHandler(this.lblDragger_MouseUp);
      this.lblDragger.MouseMove += new System.Windows.Forms.MouseEventHandler(this.lblDragger_MouseMove);
      this.lblDragger.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lblDragger_MouseDown);
      // 
      // FakeDragAndDrop
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 266);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.lblDragger});
      this.Name = "FakeDragAndDrop";
      this.Text = "Fake Drag And Drop";
      this.ResumeLayout(false);

    }
    #endregion

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

    // Keep track of when fake "drag and drop" mode is enabled.
    private bool isDragging = false;

    // Store the location where the user clicked the control.
    private int clickOffsetX, clickOffsetY;

    // Start dragging.
    private void lblDragger_MouseDown(System.Object sender,
      System.Windows.Forms.MouseEventArgs e)
    {
      isDragging = true;
      clickOffsetX = e.X;
      clickOffsetY = e.Y;
    }

    // End dragging.
    private void lblDragger_MouseUp(System.Object sender,
      System.Windows.Forms.MouseEventArgs e)
    {
      isDragging = false;
    }

    // Move the control (during dragging).
    private void lblDragger_MouseMove(System.Object sender,
      System.Windows.Forms.MouseEventArgs e)
    {
      if (isDragging == true)
      {
        // The control coordinates are converted into form coordinates
        // by adding the label position offset.
        // The offset where the user clicked in the control is also
        // accounted for. Otherwise, it looks like the top-left corner
        // of the label is attached to the mouse.
        lblDragger.Left = e.X + lblDragger.Left - clickOffsetX;
        lblDragger.Top = e.Y + lblDragger.Top - clickOffsetY;
      }
    }

  }
}


           
          


FakeDragAndDrop.zip( 23 k)

Hex View


   

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

namespace HexView
{
  /// <summary>
  /// Summary description for Form1.
  /// </summary>
  public class frmHexView : System.Windows.Forms.Form
  {
    private System.Windows.Forms.MainMenu mainMenu1;
    private System.Windows.Forms.MenuItem mnuFileMenu;
    private System.Windows.Forms.MenuItem mnuFileOpen;
    private System.Windows.Forms.MenuItem mnuFileClose;
    private System.Windows.Forms.MenuItem mnuFileExit;
    private System.Windows.Forms.MenuItem mnuAboutMenu;
    private System.Windows.Forms.MenuItem mnuAboutHexView;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.OpenFileDialog openFileDialog1;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

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

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
      textBox1.BackColor = Color.White;
      this.openFileDialog1.Filter = "Text files (*.txt)|*.txt|Wide Character Files (*.wcs)|*.wcs|All Files (*.*)|*.*||";
      textBox1.Font = new Font ("Courier New", 12);
    }

    /// <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.mainMenu1 = new System.Windows.Forms.MainMenu();
      this.mnuFileMenu = new System.Windows.Forms.MenuItem();
      this.mnuFileOpen = new System.Windows.Forms.MenuItem();
      this.mnuFileClose = new System.Windows.Forms.MenuItem();
      this.mnuFileExit = new System.Windows.Forms.MenuItem();
      this.mnuAboutMenu = new System.Windows.Forms.MenuItem();
      this.mnuAboutHexView = new System.Windows.Forms.MenuItem();
      this.textBox1 = new System.Windows.Forms.TextBox();
      this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
      this.SuspendLayout();
      // 
      // mainMenu1
      // 
      this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                            this.mnuFileMenu,
                                            this.mnuAboutMenu});
      // 
      // mnuFileMenu
      // 
      this.mnuFileMenu.Index = 0;
      this.mnuFileMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                            this.mnuFileOpen,
                                            this.mnuFileClose,
                                            this.mnuFileExit});
      this.mnuFileMenu.Text = "File";
      // 
      // mnuFileOpen
      // 
      this.mnuFileOpen.Index = 0;
      this.mnuFileOpen.Text = "Open";
      this.mnuFileOpen.Click += new System.EventHandler(this.OnFileOpen);
      // 
      // mnuFileClose
      // 
      this.mnuFileClose.Index = 1;
      this.mnuFileClose.Text = "Close";
      this.mnuFileClose.Click += new System.EventHandler(this.OnFileClose);
      // 
      // mnuFileExit
      // 
      this.mnuFileExit.Index = 2;
      this.mnuFileExit.Text = "Exit";
      this.mnuFileExit.Click += new System.EventHandler(this.OnFileExit);
      // 
      // mnuAboutMenu
      // 
      this.mnuAboutMenu.Index = 1;
      this.mnuAboutMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                             this.mnuAboutHexView});
      this.mnuAboutMenu.Text = "About";
      // 
      // mnuAboutHexView
      // 
      this.mnuAboutHexView.Index = 0;
      this.mnuAboutHexView.Text = "About HexView";
      this.mnuAboutHexView.Click += new System.EventHandler(this.OnAboutAbout);
      // 
      // textBox1
      // 
      this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
      this.textBox1.Multiline = true;
      this.textBox1.Name = "textBox1";
      this.textBox1.ReadOnly = true;
      this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
      this.textBox1.Size = new System.Drawing.Size(592, 317);
      this.textBox1.TabIndex = 0;
      this.textBox1.Text = "";
      this.textBox1.WordWrap = false;
      // 
      // frmHexView
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(592, 317);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.textBox1});
      this.Menu = this.mainMenu1;
      this.Name = "frmHexView";
      this.Text = "Hex View";
      this.ResumeLayout(false);

    }
    #endregion

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

    byte [] Data;

    private void OnFileOpen(object sender, System.EventArgs e)
    {
      if (openFileDialog1.ShowDialog () == DialogResult.Cancel)
        return;
      FileStream strm;
      try
      {
        strm = new FileStream (openFileDialog1.FileName, FileMode.Open, FileAccess.Read);
      }
      catch (Exception)
      {
        string str = "Cannot open " + openFileDialog1.FileName + " for reading";
        MessageBox.Show (str, "HexView");
        return;
      }
      if (Data != null)
        Array.Clear (Data, 0, Data.Length);
      Data = new Byte[strm.Length];
      strm.Read (Data, 0, (int) strm.Length);
      strm.Close();
      FillTextBox ();
      int index = openFileDialog1.FileName.LastIndexOf (&#039;&#039;);
      this.Text = this.Text + " - " + openFileDialog1.FileName.Substring (index + 1);
    }
    private void FillTextBox ()
    {
      textBox1.Text = "";
      StringBuilder strb = new StringBuilder ();
      StringBuilder text = new StringBuilder ();
      char [] ch = new char [1];
      for (int x = 0; x < Data.Length; x += 16)
      {
        text.Length = 0;
        strb.Length = 0;
        for (int y = 0; y < 16; ++y)
        {
          if ((x + y) > (Data.Length - 1))
            break;
          ch[0] = (char) Data[x + y];
          strb.AppendFormat ("{0,0:X2} ", (int) ch[0]);
          if (((int) ch[0] < 32) || ((int) ch&#91;0&#93; > 127))
            ch[0] = &#039;.&#039;;
          text.Append (ch);
        }
        text.Append ("
");
        while (strb.Length < 52)
          strb.Append (" ");
        strb.Append (text.ToString());
        textBox1.Text += strb.ToString ();
      }
      textBox1.Select (0, 0);
    }

    private void OnFileClose(object sender, System.EventArgs e)
    {
      textBox1.Text = "";
      this.Text = "Hex View";
    }

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

    private void OnAboutAbout(object sender, System.EventArgs e)
    {
      About about = new About();
      about.ShowDialog();
    }
  }
  /// <summary>
  /// Summary description for About.
  /// </summary>
  public class About : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Button button1;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;

    public About()
    {
      //
      // 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.button1 = new System.Windows.Forms.Button();
      this.label1 = new System.Windows.Forms.Label();
      this.label2 = new System.Windows.Forms.Label();
      this.SuspendLayout();
      // 
      // button1
      // 
      this.button1.Location = new System.Drawing.Point(148, 112);
      this.button1.Name = "button1";
      this.button1.Size = new System.Drawing.Size(104, 24);
      this.button1.TabIndex = 2;
      this.button1.Text = "OK";
      this.button1.Click += new System.EventHandler(this.button1_Click);
      // 
      // label1
      // 
      this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.label1.Location = new System.Drawing.Point(36, 16);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(344, 56);
      this.label1.TabIndex = 0;
      this.label1.Text = "A Simple Hex Viewer";
      this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
      // 
      // label2
      // 
      this.label2.Location = new System.Drawing.Point(36, 80);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(344, 16);
      this.label2.TabIndex = 1;
      this.label2.Text = "C# Tips and Techniques";
      this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
      // 
      // About
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(416, 149);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.button1,
                                      this.label2,
                                      this.label1});
      this.Name = "About";
      this.Text = "About Hex View";
      this.ResumeLayout(false);

    }
    #endregion

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

  
}

           
          


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;
     }
}

    


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;
    }
    }  


           
          


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)