Windows Systems Programming: Spring 2003

[ Home | Syllabus | Course Notes | Assignments | Search]


Some Experiments with Visual Studio .NET

At this point you have the following program (all for free) 

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

namespace Experiment1
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// 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.components = new System.ComponentModel.Container();
			this.Size = new System.Drawing.Size(300,300);
			this.Text = "Form1";
		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread] // what could this be?
		static void Main() 
		{
			Application.Run(new Form1());
		}
	}
}
private void button1_Click(object sender, System.EventArgs e)
{
	MessageBox.Show("you clicked here");
}	 
//// around line 14
public class Form1 : System.Windows.Forms.Form
{
	private System.Windows.Forms.Button button1;
/// <summary>
//// around line 54 - changed most of this function
private void InitializeComponent()
{
	this.button1 = new System.Windows.Forms.Button();
	this.SuspendLayout();
	// 
	// button1
	// 
	this.button1.Location = new System.Drawing.Point(104, 8);
	this.button1.Name = "button1";
	this.button1.TabIndex = 0;
	this.button1.Text = "Click Here";
	this.button1.Click += new System.EventHandler(this.button1_Click);
	// 
	// Form1
	// 
	this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
	this.ClientSize = new System.Drawing.Size(292, 273);
	this.Controls.AddRange(new System.Windows.Forms.Control[] {
																  this.button1});
	this.Name = "Form1";
	this.Text = "Form1";
	this.ResumeLayout(false);

}
private void myText_TextChanged(object sender, System.EventArgs e)
{
	MessageBox.Show("Text is: " + ((TextBox) sender).Text);		}
}
private void button1_Click(object sender, System.EventArgs e)
{
	MessageBox.Show("you clicked here, text is: " + this.myText.Text);
	this.Text = this.myText.Text;
}
private void myText_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
	MessageBox.Show("Text is: " + ((TextBox) sender).Text);
}

Here is a program I had at the end of a set of experiments: (deleting some lines - full source is here).

public class Form1 : System.Windows.Forms.Form
{
	private System.Windows.Forms.Button clickButton;
	private System.Windows.Forms.TextBox myText;
	private System.Timers.Timer timer1;
	private System.Windows.Forms.TrackBar trackBar1;
	private int trackDirection; // plus or minus one depending on direction
	private int trackIncrement;
	private System.Windows.Forms.ListBox listBox1;
////...
	public Form1()
	{
		//
		// Required for Windows Form Designer support
		//
		InitializeComponent();
			//
		// TODO: Add any constructor code after InitializeComponent call
		//
		timer1.Interval = 50; // set for two seconds
		timer1.AutoReset = true; // let it repeat
		timer1.Enabled = true; // and turn it on
		trackDirection = 1;
		trackIncrement = (trackBar1.Maximum - trackBar1.Minimum)/10;
	}

	#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.clickButton = new System.Windows.Forms.Button();
		this.myText = new System.Windows.Forms.TextBox();
		this.timer1 = new System.Timers.Timer();
		this.trackBar1 = new System.Windows.Forms.TrackBar();
		this.listBox1 = new System.Windows.Forms.ListBox();
		((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
		((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
		this.SuspendLayout();
		// 
		// clickButton
		// 
		this.clickButton.Location = new System.Drawing.Point(8, 64);
		this.clickButton.Name = "clickButton";
		this.clickButton.TabIndex = 0;
		this.clickButton.Text = "Click Here";
		this.clickButton.Click += new System.EventHandler(this.button1_Click);
		// 
		// myText
		// 
		this.myText.Location = new System.Drawing.Point(16, 8);
		this.myText.Name = "myText";
		this.myText.Size = new System.Drawing.Size(248, 20);
		this.myText.TabIndex = 1;
		this.myText.Text = "";
		this.myText.TextChanged += new System.EventHandler(this.myText_TextChanged);
		this.myText.MouseUp += new System.Windows.Forms.MouseEventHandler(this.myText_MouseUp);
		// 
		// timer1
		// 
		this.timer1.Enabled = true;
		this.timer1.SynchronizingObject = this;
		this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
		// 
		// trackBar1
		// 
		this.trackBar1.Location = new System.Drawing.Point(16, 208);
		this.trackBar1.Name = "trackBar1";
		this.trackBar1.Size = new System.Drawing.Size(272, 42);
		this.trackBar1.TabIndex = 2;
		// 
		// listBox1
		// 
		this.listBox1.Items.AddRange(new object[] {
			"Red",
			"Green",
			"Blue",
			"Purple"});
		this.listBox1.Location = new System.Drawing.Point(160, 48);
		this.listBox1.Name = "listBox1";
		this.listBox1.Size = new System.Drawing.Size(120, 95);
		this.listBox1.TabIndex = 3;
		this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
		// 
		// Form1
		// 
		this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
		this.ClientSize = new System.Drawing.Size(292, 273);
		this.Controls.AddRange(new System.Windows.Forms.Control[] {
			  this.listBox1,
			  this.trackBar1,
			  this.myText,
			  this.clickButton});
		this.Name = "Form1";
		this.Text = "Form1";
		((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
		((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
		this.ResumeLayout(false);
		}
	#endregion
	/// <summary>
	/// The main entry point for the application.
	/// </summary>
	[STAThread]
	static void Main() 
	{
		Application.Run(new Form1());
	}
	private void button1_Click(object sender, System.EventArgs e)
	{
		MessageBox.Show("you clicked here, text is: " + this.myText.Text);
		this.Text = this.myText.Text;
	}
	private void myText_TextChanged(object sender, System.EventArgs e)
	{
//			MessageBox.Show("Text is: " + ((TextBox) sender).Text);
	}
	private void myText_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
	{
		MessageBox.Show("Text is: " + ((TextBox) sender).Text);
	}
	private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
	{
		int currentValue = this.trackBar1.Value +
			trackDirection*trackIncrement;
		
		if(currentValue > trackBar1.Maximum) 
		{
			currentValue = trackBar1.Maximum;
			trackDirection = -1;
		}
		if(currentValue < trackBar1.Minimum) 
		{
			currentValue = trackBar1.Minimum;
			trackDirection = 1;
		}
		this.trackBar1.Value = currentValue;
	}
	private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
	{
		ListBox tempBox = (ListBox) sender;
		string msg = tempBox.Items[tempBox.SelectedIndex].ToString();
		MessageBox.Show(" You selected index "
			+ listBox1.SelectedIndex.ToString()
			+ " Value is " + msg);
	}
}
}

Copyright chris wild 1999-2003.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: February 09, 2003.