ToolBar float 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);
    }
  }

           
          


Using ToolBarButton

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

class ToggleButtons: Form
{
protected Panel panel = new Panel();
protected ToolBar tbar;
protected string strText = “Toggle”;
protected Color clrText = SystemColors.WindowText;
FontStyle fontstyle = FontStyle.Regular;

public static void Main()
{
Application.Run(new ToggleButtons());
}
public ToggleButtons()
{
panel.Parent = this;
panel.Dock = DockStyle.Fill;
panel.BackColor = SystemColors.Window;
panel.ForeColor = SystemColors.WindowText;
panel.Resize += new EventHandler(PanelOnResize);
panel.Paint += new PaintEventHandler(PanelOnPaint);

Bitmap bm = new Bitmap(GetType(), “ToggleButtons.bmp”);

ImageList imglst = new ImageList();
imglst.ImageSize = new Size(bm.Width / 4, bm.Height);
imglst.Images.AddStrip(bm);
imglst.TransparentColor = Color.White;

tbar = new ToolBar();
tbar.ImageList = imglst;
tbar.Parent = this;
tbar.ShowToolTips = true;
tbar.ButtonClick += new ToolBarButtonClickEventHandler(ToolBarOnClick);

FontStyle[] afs = { FontStyle.Bold, FontStyle.Italic,
FontStyle.Underline, FontStyle.Strikeout };

for (int i = 0; i < 4; i++) { ToolBarButton tbarbtn = new ToolBarButton(); tbarbtn.ImageIndex = i; tbarbtn.Style = ToolBarButtonStyle.ToggleButton; tbarbtn.ToolTipText = afs[i].ToString(); tbarbtn.Tag = afs[i]; tbar.Buttons.Add(tbarbtn); } } void ToolBarOnClick(object obj, ToolBarButtonClickEventArgs tbbcea) { ToolBarButton tbarbtn = tbbcea.Button; if (tbarbtn.Tag == null || tbarbtn.Tag.GetType() != typeof(FontStyle)) return; if (tbarbtn.Pushed) fontstyle |= (FontStyle) tbarbtn.Tag; else fontstyle &= ~(FontStyle) tbarbtn.Tag; panel.Invalidate(); } void PanelOnResize(object obj, EventArgs ea) { Panel panel = (Panel) obj; panel.Invalidate(); } void PanelOnPaint(object obj, PaintEventArgs pea) { Panel panel = (Panel) obj; Graphics grfx = pea.Graphics; Font font = new Font("Times New Roman", 72, fontstyle); SizeF sizef = grfx.MeasureString(strText, font); grfx.DrawString(strText, font, new SolidBrush(clrText), (panel.Width - sizef.Width) / 2, (panel.Height - sizef.Height) / 2); } } [/csharp]

Thread and UI Demo

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

public struct MyData {
public double pi;
public int iters;
}
public class Calc {
private double _pi;
private int _iters;

private readonly int TotalIters;
public Calc(int it) {
_iters = 1;
_pi = 0;
TotalIters = it;
}
public MyData PI {
get {
MyData pi = new MyData();
lock (this) {
pi.pi = _pi;
pi.iters = _iters;
}
return pi;
}
}
public Thread MakeThread() {
return new Thread(new ThreadStart(this.ThreadStarter));
}
private void calculate() {
double series = 0;
do {
series ++;
lock (this) {
_iters += 4;
_pi = series * 4;
}
} while (_iters < TotalIters); } private void ThreadStarter() { try { calculate(); } catch (ThreadAbortException e) { Console.WriteLine("ThreadAbortException"); } } } public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox PiValue; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox Iteratons; private Calc pi = new Calc(100000000); private Thread calcThread = null; private System.Windows.Forms.Timer timer1; private System.Windows.Forms.Button StopButton; private System.Windows.Forms.Button Pause; private System.ComponentModel.IContainer components; public Form1() { this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.Pause = new System.Windows.Forms.Button(); this.PiValue = new System.Windows.Forms.TextBox(); this.StopButton = new System.Windows.Forms.Button(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.Iteratons = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // label1 // this.label1.Location = new System.Drawing.Point(8, 24); this.label1.Name = "label1"; this.label1.TabIndex = 0; this.label1.Text = "Value of PI:"; // // label2 // this.label2.Location = new System.Drawing.Point(8, 72); this.label2.Name = "label2"; this.label2.TabIndex = 2; this.label2.Text = "Iterations:"; // // Pause // this.Pause.Location = new System.Drawing.Point(24, 112); this.Pause.Name = "Pause"; this.Pause.TabIndex = 5; this.Pause.Text = "Pause"; this.Pause.Click += new System.EventHandler(this.Pause_Click); // // PiValue // this.PiValue.Location = new System.Drawing.Point(128, 24); this.PiValue.Name = "PiValue"; this.PiValue.ReadOnly = true; this.PiValue.Size = new System.Drawing.Size(136, 20); this.PiValue.TabIndex = 1; this.PiValue.Text = ""; // // StopButton // this.StopButton.Location = new System.Drawing.Point(200, 112); this.StopButton.Name = "StopButton"; this.StopButton.TabIndex = 4; this.StopButton.Text = "Stop"; this.StopButton.Click += new System.EventHandler(this.StopButton_Click); // this.timer1.Enabled = true; this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // this.Iteratons.Location = new System.Drawing.Point(128, 72); this.Iteratons.Name = "Iteratons"; this.Iteratons.ReadOnly = true; this.Iteratons.TabIndex = 3; this.Iteratons.Text = ""; // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 149); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.Pause, this.StopButton, this.Iteratons, this.label2, this.PiValue, this.label1}); this.Load += new System.EventHandler(this.Form1_Load); this.Closed += new System.EventHandler(this.Form1_Closed); this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { calcThread = pi.MakeThread(); calcThread.Priority = ThreadPriority.Lowest; calcThread.Start(); } private void timer1_Tick(object sender, System.EventArgs e) { if (this.Pause.Text == "Pause") { MyData p = pi.PI; this.PiValue.Text = p.pi.ToString(); this.Iteratons.Text = p.iters.ToString(); } if (calcThread.IsAlive == false) { StopButton.Enabled = false; Pause.Enabled = false; timer1.Enabled = false; calcThread = null; } } private void StopButton_Click(object sender, System.EventArgs e) { StopButton.Enabled = false; Pause.Enabled = false; timer1.Enabled = false; calcThread.Abort(); calcThread.Join(); calcThread = null; } private void Pause_Click(object sender, System.EventArgs e) { if (this.Pause.Text == "Pause") { calcThread.Suspend(); this.Pause.Text = "Resume"; this.StopButton.Enabled = false; } else { calcThread.Resume(); this.Pause.Text = "Pause"; this.StopButton.Enabled = true; } } private void Form1_Closed(object sender, System.EventArgs e) { if (calcThread != null) { calcThread.Abort(); calcThread.Join(); } } } [/csharp]

GUI and timer


   

/*
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 Clock
{
  /// <summary>
  /// Summary description for ClockForm.
  /// </summary>
  public class ClockForm : System.Windows.Forms.Form
  {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Timer timer1;
    private System.Windows.Forms.Label label2;
    private System.ComponentModel.IContainer components;

    #region ClockForm Constructor
    public ClockForm()
    {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
      // The following line sets the text for the label box.
      // To display no text, simply use two quote marks.
      label1.Text = "";
      // The following two statements set the text size and
      // cause the time to appear centered in the label box
      label1.Font = new System.Drawing.Font
        ("Microsoft Sans Serif", 24);
      label1.TextAlign = ContentAlignment.MiddleCenter;
      label2.Font = label1.Font;
      label2.TextAlign = ContentAlignment.MiddleCenter;
      // This.Text is the text that will appear in the form&#039;s
      // title bar.
//      this.Text = "My Clock";
      // The following three lines set the timer to tick
      // every second (1000 milliseconds), start the timer and
      // write the initial time to the label box
      timer1.Interval = 1000;
      timer1.Start ();
      SetClock ();
    }
    #endregion
    /// <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.components = new System.ComponentModel.Container();
      this.timer1 = new System.Windows.Forms.Timer(this.components);
      this.label1 = new System.Windows.Forms.Label();
      this.label2 = new System.Windows.Forms.Label();
      this.SuspendLayout();
      // 
      // timer1
      // 
      this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
      // 
      // 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(24, 56);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(248, 48);
      this.label1.TabIndex = 0;
      this.label1.Text = "label1";
      // 
      // label2
      // 
      this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
      this.label2.Location = new System.Drawing.Point(24, 136);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(248, 48);
      this.label2.TabIndex = 1;
      this.label2.Text = "label2";
      // 
      // ClockForm
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
      this.ClientSize = new System.Drawing.Size(292, 272);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.label2,
                                      this.label1});
      this.Name = "ClockForm";
      this.Text = "ClockForm";
      this.ResumeLayout(false);

    }
    #endregion

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

    private void timer1_Tick(object sender, System.EventArgs e)
    {
      SetClock ();
    }
    protected void SetClock ()
    {
      string str = DateTime.Now.ToString();
      int index = str.IndexOf (" ");
      label1.Text = str.Substring (index + 1);
      label2.Text = str.Substring (0, index);
    }
  }
}


           
          


ImageList for ToolBar

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

class SimpleToolBar: Form
{
public static void Main()
{
Application.Run(new SimpleToolBar());
}
public SimpleToolBar()
{
Menu = new MainMenu();
Menu.MenuItems.Add(“File”);
Menu.MenuItems.Add(“Edit”);
Menu.MenuItems.Add(“View”);
Menu.MenuItems.Add(“Help”);

Bitmap bm = new Bitmap(GetType(),”SimpleToolBar.bmp”);

ImageList imglst = new ImageList();
imglst.Images.AddStrip(bm);
imglst.TransparentColor = Color.Cyan;

ToolBar tbar = new ToolBar();
tbar.Parent = this;
tbar.ImageList = imglst;
tbar.ShowToolTips = true;

string[] astr = {“New”, “Open”, “Save”, “Print”, “Cut”, “Copy”, “Paste” };

for (int i = 0; i < 7; i++) { ToolBarButton tbarbtn = new ToolBarButton(); tbarbtn.ImageIndex = i; tbarbtn.ToolTipText = astr[i]; tbar.Buttons.Add(tbarbtn); } } } [/csharp]

Simple Editor based on TextBox

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

public class SimpleEditorForm : Form {
    private string filename = "Untitled";

    public SimpleEditorForm(string filename) {
        InitializeComponent();

        if (filename != null) {
            this.filename = filename;
            OpenFile();
        }
    }

    protected void OpenFile() {
        try {
            textBoxEdit.Clear();
            textBoxEdit.Text = File.ReadAllText(filename);
        } catch (IOException ex) {
            MessageBox.Show(ex.Message, "Simple Editor",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
    }

    private void OnFileNew(object sender, EventArgs e) {
        filename = "Untitled";
        textBoxEdit.Clear();
    }

    private void OnFileOpen(object sender, EventArgs e) {
        if (dlgOpenFile.ShowDialog() == DialogResult.OK) {
            filename = dlgOpenFile.FileName;
            OpenFile();
        }
    }

    private void OnFileSave(object sender, EventArgs e) {

    }

    private void OnFileSaveAs(object sender, EventArgs e) {

    }
    private void InitializeComponent() {
        this.textBoxEdit = new System.Windows.Forms.TextBox();
        this.mainMenu = new System.Windows.Forms.MenuStrip();
        this.miFile = new System.Windows.Forms.ToolStripMenuItem();
        this.miFileNew = new System.Windows.Forms.ToolStripMenuItem();
        this.miFileOpen = new System.Windows.Forms.ToolStripMenuItem();
        this.miFileSave = new System.Windows.Forms.ToolStripMenuItem();
        this.miFileSaveAs = new System.Windows.Forms.ToolStripMenuItem();
        this.dlgOpenFile = new System.Windows.Forms.OpenFileDialog();
        this.mainMenu.SuspendLayout();
        this.SuspendLayout();
        // 
        // textBoxEdit
        // 
        this.textBoxEdit.AcceptsReturn = true;
        this.textBoxEdit.AcceptsTab = true;
        this.textBoxEdit.Dock = System.Windows.Forms.DockStyle.Fill;
        this.textBoxEdit.Location = new System.Drawing.Point(0, 24);
        this.textBoxEdit.Multiline = true;
        this.textBoxEdit.Name = "textBoxEdit";
        this.textBoxEdit.ScrollBars = System.Windows.Forms.ScrollBars.Both;
        this.textBoxEdit.Size = new System.Drawing.Size(562, 219);
        this.textBoxEdit.TabIndex = 0;
        // 
        // mainMenu
        // 
        this.mainMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.miFile});
        this.mainMenu.Location = new System.Drawing.Point(0, 0);
        this.mainMenu.Name = "mainMenu";
        this.mainMenu.Size = new System.Drawing.Size(562, 24);
        this.mainMenu.TabIndex = 1;
        this.mainMenu.Text = "menuStrip1";
        // 
        // miFile
        // 
        this.miFile.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.miFileNew,
            this.miFileOpen,
            this.miFileSave,
            this.miFileSaveAs});
        this.miFile.Name = "miFile";
        this.miFile.Text = "&amp;File";
        // 
        // miFileNew
        // 
        this.miFileNew.Name = "miFileNew";
        this.miFileNew.Text = "&amp;New";
        this.miFileNew.Click += new System.EventHandler(this.OnFileNew);
        // 
        // miFileOpen
        // 
        this.miFileOpen.Name = "miFileOpen";
        this.miFileOpen.Text = "&amp;Open";
        this.miFileOpen.Click += new System.EventHandler(this.OnFileOpen);
        // 
        // miFileSave
        // 
        this.miFileSave.Name = "miFileSave";
        this.miFileSave.Text = "&amp;Save";
        this.miFileSave.Click += new System.EventHandler(this.OnFileSave);
        // 
        // miFileSaveAs
        // 
        this.miFileSaveAs.Name = "miFileSaveAs";
        this.miFileSaveAs.Text = "Save &amp;As";
        this.miFileSaveAs.Click += new System.EventHandler(this.OnFileSaveAs);
        // 
        // dlgOpenFile
        // 
        this.dlgOpenFile.Filter = "Text Documents (*.txt)|*.txt|Wrox Documents (*.wroxtext)|*.wroxtext|All Files|*.*" +
            "";
        this.dlgOpenFile.FilterIndex = 2;
        // 
        // SimpleEditorForm
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(562, 243);
        this.Controls.Add(this.textBoxEdit);
        this.Controls.Add(this.mainMenu);
        this.MainMenuStrip = this.mainMenu;
        this.Name = "SimpleEditorForm";
        this.Text = "Simple Editor";
        this.mainMenu.ResumeLayout(false);
        this.ResumeLayout(false);
        this.PerformLayout();

    }



    private System.Windows.Forms.TextBox textBoxEdit;
    private System.Windows.Forms.MenuStrip mainMenu;
    private System.Windows.Forms.ToolStripMenuItem miFile;
    private System.Windows.Forms.ToolStripMenuItem miFileNew;
    private System.Windows.Forms.ToolStripMenuItem miFileOpen;
    private System.Windows.Forms.ToolStripMenuItem miFileSave;
    private System.Windows.Forms.ToolStripMenuItem miFileSaveAs;
    private System.Windows.Forms.OpenFileDialog dlgOpenFile;

    [STAThread]
    static void Main(string[] args) {
        string filename = null;
        if (args.Length != 0)
            filename = args[0];

        Application.EnableVisualStyles();
        Application.Run(new SimpleEditorForm(filename));
    }

}

    


Talking to a visual element in a background thread.

   
 


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

public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.ProgressBar progressBar1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private Thread fThread = null;
    private System.ComponentModel.Container components = null;

    public Form1() {
        this.progressBar1 = new System.Windows.Forms.ProgressBar();
        this.button1 = new System.Windows.Forms.Button();
        this.button2 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        this.progressBar1.Location = new System.Drawing.Point(24, 32);
        this.progressBar1.Name = "progressBar1";
        this.progressBar1.Size = new System.Drawing.Size(264, 23);
        this.progressBar1.TabIndex = 0;
        this.button1.Location = new System.Drawing.Point(24, 80);
        this.button1.Size = new System.Drawing.Size(136, 40);
        this.button1.Text = "Start Thread";
        this.button1.Click += new
             System.EventHandler(this.button1_Click);
        this.button2.Location = new System.Drawing.Point(168, 80);
        this.button2.Size = new System.Drawing.Size(120, 40);
        this.button2.Text = "Stop Thread";
        this.button2.Click += new
             System.EventHandler(this.button2_Click);
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(704, 429);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
          this.button2,
          this.button1,
          this.progressBar1});

        this.ResumeLayout(false);
    }

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

    }

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

    private void UpdateProgress() {
        if (progressBar1.Value == progressBar1.Maximum) {
            progressBar1.Value = progressBar1.Minimum;
        }
        progressBar1.PerformStep();
    }


    public void ThreadProc() {

        try {
            MethodInvoker mi = new MethodInvoker(this.UpdateProgress);
            while (true) {
                this.BeginInvoke(mi);
                Thread.Sleep(500);
            }
        } catch (ThreadInterruptedException e) {
            Console.WriteLine(
              "Interruption Exception in Thread: {0}",
                  e);
        } catch (Exception we) {
            Console.WriteLine("Exception in Thread: {0}", we);
        }
    }

    private void button1_Click(object sender, System.EventArgs e) {
        fThread = new Thread(new ThreadStart(ThreadProc));
        fThread.IsBackground = true;
        fThread.Start();
    }

    private void button2_Click(object sender, System.EventArgs e) {
        fThread.Interrupt();
        fThread = null;
    }
}