Click here to Skip to main content
15,891,513 members
Articles / Programming Languages / Visual Basic

Regions Add-In for VS.NET 2003

Rate me:
Please Sign up or sign in to vote.
4.77/5 (53 votes)
11 Feb 20053 min read 359.7K   4.7K   127  
Create an add-in for VS.NET 2003 to create regions.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using EnvDTE;


namespace RegionsAddIn
{
	/// <summary>
	/// Summary description for RegionsDialog.
	/// </summary>
	public class RegionsDialog : System.Windows.Forms.Form
	{
		private System.Windows.Forms.CheckBox checkBoxNewRegion;
		private System.Windows.Forms.TextBox textBoxNewRegion;
		private System.Windows.Forms.Button buttonOK;
		private System.Windows.Forms.Button buttonCancel;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ErrorProvider errorProvider;
		private System.ComponentModel.IContainer components = null;
		private int selectionTopLine;
		private int selectionBottomLine;
		bool atStartOfBottomLine;
		private System.Windows.Forms.ListBox listBoxTemplate;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.CheckBox checkBoxAddToTemplates;
		private _DTE applicationObject;
		private Document document;
		private RegionsAddIn.RegionTree _regionTree;
		private TreeItemTagData _targetRegion;

		public RegionsDialog(Document document, int selectionTopLine, int selectionBottomLine, bool atStartOfBottomLine)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			this.selectionTopLine = selectionTopLine;
			this.selectionBottomLine = selectionBottomLine;
			this.atStartOfBottomLine = atStartOfBottomLine;
			this.listBoxTemplate.Enabled = false;
			this.applicationObject = applicationObject;
			this.document = document;
		}

		/// <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.checkBoxNewRegion = new System.Windows.Forms.CheckBox();
			this.textBoxNewRegion = new System.Windows.Forms.TextBox();
			this.buttonOK = new System.Windows.Forms.Button();
			this.buttonCancel = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.errorProvider = new System.Windows.Forms.ErrorProvider();
			this.listBoxTemplate = new System.Windows.Forms.ListBox();
			this.label2 = new System.Windows.Forms.Label();
			this.checkBoxAddToTemplates = new System.Windows.Forms.CheckBox();
			this._regionTree = new RegionsAddIn.RegionTree();
			this.SuspendLayout();
			// 
			// checkBoxNewRegion
			// 
			this.checkBoxNewRegion.Location = new System.Drawing.Point(8, 184);
			this.checkBoxNewRegion.Name = "checkBoxNewRegion";
			this.checkBoxNewRegion.Size = new System.Drawing.Size(168, 16);
			this.checkBoxNewRegion.TabIndex = 1;
			this.checkBoxNewRegion.Text = "Add new Region";
			this.checkBoxNewRegion.Click += new System.EventHandler(this.checkBoxNewRegion_Click);
			// 
			// textBoxNewRegion
			// 
			this.textBoxNewRegion.Enabled = false;
			this.textBoxNewRegion.Location = new System.Drawing.Point(8, 208);
			this.textBoxNewRegion.Name = "textBoxNewRegion";
			this.textBoxNewRegion.Size = new System.Drawing.Size(376, 20);
			this.textBoxNewRegion.TabIndex = 2;
			this.textBoxNewRegion.Text = "";
			// 
			// buttonOK
			// 
			this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.buttonOK.Location = new System.Drawing.Point(120, 240);
			this.buttonOK.Name = "buttonOK";
			this.buttonOK.TabIndex = 3;
			this.buttonOK.Text = "OK";
			this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
			// 
			// buttonCancel
			// 
			this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.buttonCancel.Location = new System.Drawing.Point(216, 240);
			this.buttonCancel.Name = "buttonCancel";
			this.buttonCancel.TabIndex = 4;
			this.buttonCancel.Text = "Cancel";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(168, 16);
			this.label1.TabIndex = 5;
			this.label1.Text = "Existing Regions";
			// 
			// errorProvider
			// 
			this.errorProvider.ContainerControl = this;
			// 
			// listBoxTemplate
			// 
			this.listBoxTemplate.Enabled = false;
			this.listBoxTemplate.Location = new System.Drawing.Point(208, 24);
			this.listBoxTemplate.Name = "listBoxTemplate";
			this.listBoxTemplate.Size = new System.Drawing.Size(192, 147);
			this.listBoxTemplate.Sorted = true;
			this.listBoxTemplate.TabIndex = 6;
			this.listBoxTemplate.DoubleClick += new System.EventHandler(this.listBoxTemplate_DoubleClick);
			this.listBoxTemplate.MouseUp += new System.Windows.Forms.MouseEventHandler(this.listBoxTemplate_MouseUp);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(208, 8);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(168, 16);
			this.label2.TabIndex = 7;
			this.label2.Text = "Templates";
			// 
			// checkBoxAddToTemplates
			// 
			this.checkBoxAddToTemplates.Location = new System.Drawing.Point(208, 184);
			this.checkBoxAddToTemplates.Name = "checkBoxAddToTemplates";
			this.checkBoxAddToTemplates.Size = new System.Drawing.Size(168, 16);
			this.checkBoxAddToTemplates.TabIndex = 8;
			this.checkBoxAddToTemplates.Text = "Add to templates";
			// 
			// _regionTree
			// 
			this._regionTree.ApplicationObject = null;
			this._regionTree.Location = new System.Drawing.Point(8, 24);
			this._regionTree.Name = "_regionTree";
			this._regionTree.Size = new System.Drawing.Size(192, 147);
			this._regionTree.TabIndex = 0;
			this._regionTree.TreeDoubleClick += new System.EventHandler(this._regionTree_DoubleClick);
			this._regionTree.TreeAfterSelect += new System.Windows.Forms.TreeViewEventHandler(this._regionTree_AfterSelect);
			// 
			// RegionsDialog
			// 
			this.AcceptButton = this.buttonOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.CancelButton = this.buttonCancel;
			this.ClientSize = new System.Drawing.Size(410, 272);
			this.ControlBox = false;
			this.Controls.Add(this._regionTree);
			this.Controls.Add(this.checkBoxAddToTemplates);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.listBoxTemplate);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.buttonCancel);
			this.Controls.Add(this.buttonOK);
			this.Controls.Add(this.textBoxNewRegion);
			this.Controls.Add(this.checkBoxNewRegion);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Name = "RegionsDialog";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Existing Regions Dialog";
			this.Load += new System.EventHandler(this.RegionsDialog_Load);
			this.ResumeLayout(false);

		}
		#endregion

		#region Properties

		/// <summary>
		/// RegionTree control for this dialog.
		/// </summary>
		public RegionTree RegionTree
		{
			get {return _regionTree;}
		}

		/// <summary>
		/// New region name, if supplied.
		/// </summary>
		public string RegionName
		{
			get {return textBoxNewRegion.Text;}
			set {textBoxNewRegion.Text = value;}
		}

		/// <summary>
		/// Target region to add selection to.
		/// </summary>
		public TreeItemTagData TargetRegion
		{
			get {return _targetRegion;}
		}

		#endregion
		
		/// <summary>
		/// OnLoad handler for dialog.
		/// </summary>
		private void RegionsDialog_Load(object sender, System.EventArgs e)
		{
			_regionTree.BuildTree(document);

			string templateDir = Application.StartupPath + "\\RegionsAddIn";
			DirectoryInfo di = new DirectoryInfo(templateDir);
			if(!di.Exists)
				Directory.CreateDirectory(templateDir);

			string templateFile = templateDir + "\\templates.txt";
			FileInfo fi = new FileInfo(templateFile);
			if(fi.Exists)
			{
				StreamReader sr = new StreamReader(templateFile);
				string buffer = sr.ReadLine();
				while(buffer != null)
				{
					this.listBoxTemplate.Items.Add(buffer);
					buffer = sr.ReadLine();
				}
				sr.Close();
			}

			_regionTree.TreeView.HideSelection = false;
		}

		/// <summary>
		/// Handler for checkbox change of state
		/// </summary>
		private void checkBoxNewRegion_Click(object sender, System.EventArgs e)
		{
			textBoxNewRegion.Enabled = checkBoxNewRegion.Checked;
			if(textBoxNewRegion.Enabled)
				textBoxNewRegion.Focus();
			else
				textBoxNewRegion.Text = string.Empty;
			listBoxTemplate.Enabled = checkBoxNewRegion.Checked;
		}

		/// <summary>
		/// Handler for OK button.
		/// </summary>
		private void buttonOK_Click(object sender, System.EventArgs e)
		{
			if(checkBoxNewRegion.Checked)
			{
				if(textBoxNewRegion.Text.Length == 0)
				{
					errorProvider.SetError(textBoxNewRegion, "Cannot be zero length");
					this.DialogResult = DialogResult.None;
					return;
				}
			}

			if(checkBoxAddToTemplates.Checked)
			{
				bool addTemplate = false;
				if(checkBoxNewRegion.Checked)
				{
					if(this.listBoxTemplate.Items.IndexOf(RegionName) == -1)
					{
						this.listBoxTemplate.Items.Add(RegionName);
						addTemplate = true;
					}
				}
				else
				{
					if(this.listBoxTemplate.Items.IndexOf(_regionTree.TreeView.SelectedNode.Text) == -1)
					{
						this.listBoxTemplate.Items.Add(_regionTree.TreeView.SelectedNode.Text);
						addTemplate = true;
					}
				}

				if(addTemplate)
				{
					string templateDir = Application.StartupPath + "\\RegionsAddIn";
					DirectoryInfo di = new DirectoryInfo(templateDir);
					if(!di.Exists)
						Directory.CreateDirectory(templateDir);

					string templateFile = templateDir + "\\templates.txt";
					StreamWriter sw = new StreamWriter(templateFile);
					for(int i = 0; i < this.listBoxTemplate.Items.Count; i++)
						sw.WriteLine(this.listBoxTemplate.Items[i]);
					sw.Close();
				}
			}

			if(((TreeItemTagData)_regionTree.TreeView.SelectedNode.Tag).IsRegion)
				_targetRegion = (TreeItemTagData)_regionTree.TreeView.SelectedNode.Tag;
			else
				_targetRegion = null;

			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		/// <summary>
		/// Handler for tree selection changed event.
		/// </summary>
		private void _regionTree_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
		{
			if(_regionTree.SelectedNode != null)
			{
				if(_regionTree.SelectedNode.ForeColor != SystemColors.GrayText)
				{
					buttonOK.Enabled = true;
					checkBoxNewRegion.Enabled = true;
					checkBoxAddToTemplates.Enabled = true;
					_targetRegion = (TreeItemTagData)_regionTree.SelectedNode.Tag;
				}
				else
				{
					buttonOK.Enabled = false;
					checkBoxNewRegion.Enabled = false;
					checkBoxAddToTemplates.Enabled = false;
					textBoxNewRegion.Enabled = false;
					_targetRegion = null;
				}
			}
			else
			{
				buttonOK.Enabled = false;
				checkBoxNewRegion.Enabled = false;
				checkBoxAddToTemplates.Enabled = false;
			}
		}

		/// <summary>
		/// Handler for tree selection doubleclick event.
		/// </summary>
		private void _regionTree_DoubleClick(object sender, System.EventArgs e)
		{
			if(_regionTree.SelectedNode.ForeColor != SystemColors.GrayText)
			{
				this.buttonOK_Click(sender, e);
				this.DialogResult = DialogResult.OK;
				this.Close();
			}
		}

		/// <summary>
		/// Handler for template list doubleclick event.
		/// </summary>
		private void listBoxTemplate_DoubleClick(object sender, System.EventArgs e)
		{
			this.listBoxTemplate_MouseUp(sender, null);
			this.buttonOK_Click(sender, null);
		}

		/// <summary>
		/// Handler for template list mouseup event.
		/// </summary>
		private void listBoxTemplate_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (this.listBoxTemplate.Items[this.listBoxTemplate.SelectedIndex] != null)
			{
				this.textBoxNewRegion.Text = (string)(this.listBoxTemplate.Items[this.listBoxTemplate.SelectedIndex]);
			}
		}
	}
}

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
JWT
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions