Click here to Skip to main content
15,884,064 members
Articles / Desktop Programming / Windows Forms

Ye Aulde Application Starter

Rate me:
Please Sign up or sign in to vote.
4.45/5 (13 votes)
3 Dec 20057 min read 54.2K   894   72  
Give your application a good start! Handle unhandled exceptions, ensure one instance running, use splash screen for lengthy load and more. All in one pack - enjoy!
using System;
using System.Drawing;
using System.Windows.Forms;

namespace AppStart {

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

		#region Windows Form Designer generated code

		protected System.Windows.Forms.Button cbCancel;
		protected System.Windows.Forms.Button cbOk;
		protected Line Line1;
		private System.ComponentModel.Container components = null;

		private void InitializeComponent() {
			this.cbCancel = new System.Windows.Forms.Button();
			this.cbOk = new System.Windows.Forms.Button();
			this.Line1 = new AppStart.Line();
			this.SuspendLayout();
			// 
			// cbCancel
			// 
			this.cbCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.cbCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.cbCancel.Location = new System.Drawing.Point(112, 224);
			this.cbCancel.Name = "cbCancel";
			this.cbCancel.Size = new System.Drawing.Size(92, 24);
			this.cbCancel.TabIndex = 11;
			this.cbCancel.Text = "Cancel";
			this.cbCancel.Click += new System.EventHandler(this.cbCancel_Click);
			// 
			// cbOk
			// 
			this.cbOk.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.cbOk.Location = new System.Drawing.Point(12, 224);
			this.cbOk.Name = "cbOk";
			this.cbOk.Size = new System.Drawing.Size(92, 24);
			this.cbOk.TabIndex = 10;
			this.cbOk.Text = "OK";
			this.cbOk.Click += new System.EventHandler(this.cbOk_Click);
			// 
			// Line1
			// 
			this.Line1.Location = new System.Drawing.Point(44, 216);
			this.Line1.Name = "Line1";
			this.Line1.Size = new System.Drawing.Size(304, 6);
			this.Line1.TabIndex = 12;
			// 
			// BaseDialog
			// 
			this.AcceptButton = this.cbOk;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.cbCancel;
			this.ClientSize = new System.Drawing.Size(390, 251);
			this.Controls.Add(this.Line1);
			this.Controls.Add(this.cbCancel);
			this.Controls.Add(this.cbOk);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.KeyPreview = true;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "BaseDialog";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.ResumeLayout(false);

		}

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

		#endregion

		public BaseDialog() {
			InitializeComponent();
		}

		private void cbOk_Click(object sender, System.EventArgs e) {
			ExitOk();
		}

		private void cbCancel_Click(object sender, System.EventArgs e) {
			ExitCancel();
		}

		protected void ExitOk() {
			if(cbOk.Enabled==true) {
				if(OnOk()) {
					this.DialogResult = DialogResult.OK;
					this.Close();
				}
			}
		}

		protected void ExitCancel() {
			if(cbCancel.Enabled==true) {
				if(OnCancel()) {
					this.DialogResult = DialogResult.Cancel;
					this.Close();
				}
			}
		}

		protected virtual bool OnOk() {
			return true;
		}

		protected virtual bool OnCancel() {
			return true;
		}

		protected override void OnLoad(EventArgs e) {
			base.OnLoad (e);
			OnResize(e);
		}

		private int lineDistanceToBottom = 0;
		private int cbOkDistanceToBottom = 0;
		private int cbCancelDistanceToBottom = 0;
		protected override void OnResize(EventArgs e) {
			base.OnResize (e);
			if(lineDistanceToBottom==0)
				lineDistanceToBottom = this.Bottom - Line1.Top;
			if(cbOkDistanceToBottom==0)
				cbOkDistanceToBottom = this.Bottom - cbOk.Top;
			if(cbCancelDistanceToBottom==0)
				cbCancelDistanceToBottom = this.Bottom - cbCancel.Top;
			Line1.Location = new Point(6, this.Height - lineDistanceToBottom);
			Line1.Size = new Size(this.Width - 18, 2);
			int okx = this.Width - cbOk.Width - 14;
			if(cbCancel.Visible && this.Visible)
				okx -= cbCancel.Width + 4;
			cbOk.Location = new Point(okx, this.Height - cbOkDistanceToBottom);
			cbCancel.Location = new Point(this.Width - cbCancel.Width - 14, this.Height - cbCancelDistanceToBottom);
		}

	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions