Click here to Skip to main content
15,893,622 members
Articles / Programming Languages / C#

Build Stack Array for Formatting or Searching Data

Rate me:
Please Sign up or sign in to vote.
3.80/5 (37 votes)
3 Jul 20042 min read 58.8K   920   14  
This article builds stacks to filter or format data.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace Stacks
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button bttnLoad;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.TextBox txtDisplay;
		// Declare string Variables
		private int intArrayIndex, intPass; 
		private string strFormat, strBuild, strDisplay, strFormatDisplay;
		// Declare Multi Dimensional String Array
		private string [ , ] myArray = new string [ , ]
			{
			    {"Courtney", "Krupets", "Gymnastics"},
				{"Jason", "Gatson", "Gymnastics"},
				{"Paul", "Hamm", "Gymnastics"},
				{"Brett", "McClure", "Gymnastics"},
				{"Morgan", "Hamm", "Gymnastics"},
				{"Mia", "Hamm", "Soccer"},
				{"Jonny", "Walker", "Soccer"},
				{"Shannon", "Boxx", "Soccer"},
				{"Julie", "Foudy", "Soccer"},
				{"Kylie", "Bivens", "Soccer"},
				{"Angela", "Hucles", "Soccer"},
				{"Kristy", "Kowal", "Swimming"},
				{"Tom", "Malchow", "Swimming"},
				{"Mary", "DeScenza", "Swimming"},
				{"John", "Foster", "Swimming"}
			};
		private System.Windows.Forms.ListBox lstSports;
		private System.Windows.Forms.Button bttnSearch;
        // Declare Single Dimensional String Arrays ( stacks ) 
		private string [] arrayBuild = new string [45];
		private string [] arrayDisplay = new string[45];

		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()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.txtDisplay = new System.Windows.Forms.TextBox();
			this.bttnLoad = new System.Windows.Forms.Button();
			this.lstSports = new System.Windows.Forms.ListBox();
			this.bttnSearch = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// txtDisplay
			// 
			this.txtDisplay.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtDisplay.Location = new System.Drawing.Point(8, 8);
			this.txtDisplay.Multiline = true;
			this.txtDisplay.Name = "txtDisplay";
			this.txtDisplay.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.txtDisplay.Size = new System.Drawing.Size(432, 344);
			this.txtDisplay.TabIndex = 0;
			this.txtDisplay.Text = "";
			// 
			// bttnLoad
			// 
			this.bttnLoad.BackColor = System.Drawing.Color.Turquoise;
			this.bttnLoad.Location = new System.Drawing.Point(64, 384);
			this.bttnLoad.Name = "bttnLoad";
			this.bttnLoad.Size = new System.Drawing.Size(72, 24);
			this.bttnLoad.TabIndex = 1;
			this.bttnLoad.Text = "Load Array";
			this.bttnLoad.Click += new System.EventHandler(this.bttnLoad_Click);
			// 
			// lstSports
			// 
			this.lstSports.Location = new System.Drawing.Point(152, 368);
			this.lstSports.Name = "lstSports";
			this.lstSports.Size = new System.Drawing.Size(136, 56);
			this.lstSports.TabIndex = 2;
			// 
			// bttnSearch
			// 
			this.bttnSearch.BackColor = System.Drawing.Color.Aqua;
			this.bttnSearch.Location = new System.Drawing.Point(304, 384);
			this.bttnSearch.Name = "bttnSearch";
			this.bttnSearch.Size = new System.Drawing.Size(72, 24);
			this.bttnSearch.TabIndex = 3;
			this.bttnSearch.Text = "Search";
			this.bttnSearch.Click += new System.EventHandler(this.bttnSearch_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.Color.Orange;
			this.ClientSize = new System.Drawing.Size(448, 438);
			this.Controls.Add(this.bttnSearch);
			this.Controls.Add(this.lstSports);
			this.Controls.Add(this.bttnLoad);
			this.Controls.Add(this.txtDisplay);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "Form1";
			this.Text = "Stack Arrays";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
			this.Load += new System.EventHandler(this.Form1_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
		
		private void Form1_Load(object sender, System.EventArgs e)
		{
			// Add Items To listBox
			lstSports.Items.Add("Gymnastics");
			lstSports.Items.Add("Soccer");
			lstSports.Items.Add("Swimming");
			lstSports.SelectedItem = "Gymnastics";
            // Clear txtBox
			txtDisplay.Clear();
			// Initialize Variables
			strFormat = "";
		}
		
		private void bttnLoad_Click(object sender, System.EventArgs e)
		{
			try
			{
				// Create Header
				txtDisplay.Text = "First Name" + "  " + "Last Name" + "  " + "Olympic Sport" + "\r\n\r\n";;
			
				// Concatenate String - Note: we insert a comma in between
				// these fields so we can easily split them later
				foreach( string strStack in myArray )
				{
					strFormat += strStack + ",";
				}

				// Split String at the commas, and load Into Single Dimensional Array
				// The reason we do this is so we can format our string. We will also
				// accomplish our Linear Search with this array. 
				char[] trimChars = {'\u002c'};
				arrayBuild = Regex.Split( strFormat, @",\s*");

				// Iterate through stack and format string for txtDisplay
				for( int i = 0; i < arrayBuild.Length - 1; i += 3 )
				{
					// Format String for Display in textBox
					strBuild = String.Format("{0, -12}{1, -12}{2, -12}\r\n", arrayBuild[ i ], arrayBuild[ i + 1 ], arrayBuild[ i + 2 ]);
					// Append string to txtDisplay.Text
					txtDisplay.AppendText( strBuild );
				}
			}
			finally
			{
				// Disable bttnLoad
				bttnLoad.Enabled = false;
			}
		}

		private void bttnSearch_Click(object sender, System.EventArgs e)
		{
			try
			{
				// Clear txtBox
				txtDisplay.Clear();
			
				// Create Header
				txtDisplay.Text = "First Name" + "  " + "Last Name" + "  " + "Olympic Sport" + "\r\n\r\n";;
			
				// Call SearchArray Function
				intArrayIndex = SearchArray( arrayBuild, lstSports.SelectedItem.ToString() );
			
				// Split string into Single Dimensional Array for formatting
				// Again we will split the string at commas
				char[] trimChars = {'\u002c'};
				arrayDisplay = Regex.Split( strDisplay, @",\s*");
			
				// Iterate through stack and format string for txtDisplay
				for( int i = 0; i < arrayDisplay.Length - 1; i += 3 )
				{
					// Format String for Display in textBox
					strFormatDisplay = String.Format("{0, -12}{1, -12}{2, -12}\r\n", arrayDisplay[ i ], arrayDisplay[ i + 1 ], arrayDisplay[ i + 2 ]);
					// Append string to txtDisplay.Text
					txtDisplay.AppendText( strFormatDisplay );
				}
			}
			finally
			{
		        //Clear string variable
				strFormatDisplay = "";
		
				// Disable bttnLoad
				bttnLoad.Enabled = false;
			}
		}
   
		public int SearchArray( string [] array, string key )
		{
			// Initialize string variable 
			strDisplay = "";
			
			// Iterate through the stack, and define the condition for the filter 
			for( int n = 0; n < array.Length; n++ )
				{
					if( array[n] == key)
					{
						// Pass loop counter to global variable
						intPass = n;
						// Concatenate String
						strDisplay += arrayBuild[n - 2] + "," + arrayBuild[n - 1] + "," + arrayBuild[n] + ",";

					}
						
				}

			return intPass;
		}

		// Good Etiquette
		private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			Application.Exit();
		}
	}
}

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
United States United States
I studied Fortran IV in HighSchool where we had 2 keypunch machines, and access to an IBM 1100 at the Community College. We ran our programs batch, and compiled our programs on paper tape.

Years later when PC's became affordable, I gave programming another shot. This time I studied RPG with the IBM AS-400 computer. I could access the College Computer with Emulator Software( Mocha Soft MW 5250 ) and my home PC.

C++ came later, then VB-6, C#.Net, and Managed C++. I am currently studying VB.Net

Comments and Discussions