Click here to Skip to main content
15,897,226 members
Articles / Programming Languages / C#

Neural Dot Net Pt 3 The Adaline Network

Rate me:
Please Sign up or sign in to vote.
3.71/5 (16 votes)
23 Oct 200316 min read 73.4K   379   41  
A neural network library in C#.

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Text;
using System.Security;
using System.Diagnostics;
using System.Threading;

namespace LogViewer
{
	/// <summary>
	/// Summary description for UserControl1.
	/// </summary>
	public class LogViewer : System.Windows.Forms.UserControl
	{
		/// <summary>
		/// private class to hoild the log details
		/// </summary>
		private class LogData
		{
			private string strElement;
			private string strTime;
			private string strPriority;
			private string strMessage;

			public LogData()
			{
			}

			public string Element
			{
				get
				{
					return strElement;
				}
				set
				{
					strElement = value;
				}
			}

			public string Time
			{
				get
				{
					return strTime;
				}
				set
				{
					strTime = value;
				}
			}

			public string Priority
			{
				get
				{
					return strPriority;
				}
				set
				{
					strPriority = value;
				}
			}

			public string Message
			{
				get
				{
					return strMessage;
				}
				set
				{
					strMessage = value;
				}
			}
		}

		/// <summary>
		/// private class to hold file data
		/// </summary>
		private class FileData
		{
			private string strFile;
			private string strElement;
			private string strName;

			public FileData()
			{
			}

			public string File
			{
				get
				{
					return strFile;
				}
				set
				{
					strFile = value;
				}
			}

			public string Element
			{
				get
				{
					return strElement;
				}
				set
				{
					strElement = value;
				}
			}

			public string Name
			{
				get
				{
					return strName;
				}
				set
				{
					strName = value;
				}
			}
		}
		private LogViewerTabPage tabPage1;
		private string strFileName;
		private string strEventLog;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button logViewerBrowser;
		private System.Windows.Forms.OpenFileDialog logViewerOpenFileDialog;
		private XmlTextReader xmlReader;
		private ArrayList arrayLogData;
		private System.Windows.Forms.TextBox fileNameBox;
		private System.Windows.Forms.TextBox eventLogBox;
		private AxSHDocVw.AxWebBrowser axWebBrowser1;
		private System.Windows.Forms.TreeView logViewerTree;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.ProgressBar progressBar1;
		private StreamWriter stream;
		private StringBuilder strHtml = new StringBuilder(); 
		ThreadStart threadStart;
		Thread thread;
		private System.Windows.Forms.Timer timerThread;
		private System.ComponentModel.IContainer components;

		/// <summary>
		/// The fileName of the xml file containing the log. 
		/// </summary>
		public string FileName
		{
			get
			{
				return strFileName;
			}
			set
			{
				strFileName = value;
			}
		}

		/// <summary>
		/// The Name of the Event Log Details to view
		/// </summary>
		public string EventLog
		{
			get
			{
				return strEventLog;
			}
			set
			{
				strEventLog = value;
			}
		}

		/// <summary>
		/// constructor
		/// </summary>
		public LogViewer()
		{
		
			this.tabPage1 = new LogViewerTabPage( this );			

			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
			fileNameBox.Text = string.Empty;
			eventLogBox.Text = string.Empty;
			FileName = string.Empty;

			// TODO: Add any initialization after the InitForm call
	///		this.button1.Click += new System.EventHandler( OnGetLogs );
			arrayLogData = new ArrayList();
			this.tabPage1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.tabPage1.Location = new System.Drawing.Point(4, 22);
			this.tabPage1.Name = "tabPage1";
			this.tabPage1.Size = new System.Drawing.Size(456, 230);
			this.tabPage1.TabIndex = 0;
			this.tabPage1.Text = "Event Viewer Errors";

			this.tabPage1.ResumeLayout(false);

			progressBar1.Hide();
			eventLogBox.Hide();
			button1.Hide();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			try
			{
				if( disposing )
				{
					if( components != null )
						components.Dispose();
				}
				base.Dispose( disposing );
			}
				/// explorer throws an exception on dispose if not shown
			catch( System.Reflection.TargetInvocationException )
			{
			}
		}

		#region Component 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();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(LogViewer));
			this.label1 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.fileNameBox = new System.Windows.Forms.TextBox();
			this.eventLogBox = new System.Windows.Forms.TextBox();
			this.logViewerBrowser = new System.Windows.Forms.Button();
			this.logViewerOpenFileDialog = new System.Windows.Forms.OpenFileDialog();
			this.logViewerTree = new System.Windows.Forms.TreeView();
			this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
			this.panel1 = new System.Windows.Forms.Panel();
			this.splitter1 = new System.Windows.Forms.Splitter();
			this.progressBar1 = new System.Windows.Forms.ProgressBar();
			this.timerThread = new System.Windows.Forms.Timer(this.components);
			((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(56, 16);
			this.label1.TabIndex = 1;
			this.label1.Text = "FileName";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(376, 16);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(72, 24);
			this.button1.TabIndex = 2;
			this.button1.Text = "Get Logs";
			// 
			// fileNameBox
			// 
			this.fileNameBox.Location = new System.Drawing.Point(64, 16);
			this.fileNameBox.Name = "fileNameBox";
			this.fileNameBox.TabIndex = 3;
			this.fileNameBox.Text = "textBox1";
			// 
			// eventLogBox
			// 
			this.eventLogBox.Location = new System.Drawing.Point(272, 16);
			this.eventLogBox.Name = "eventLogBox";
			this.eventLogBox.TabIndex = 5;
			this.eventLogBox.Text = "textBox2";
			// 
			// logViewerBrowser
			// 
			this.logViewerBrowser.Location = new System.Drawing.Point(64, 40);
			this.logViewerBrowser.Name = "logViewerBrowser";
			this.logViewerBrowser.TabIndex = 6;
			this.logViewerBrowser.Text = "Browse";
			this.logViewerBrowser.Click += new System.EventHandler(this.OnLogViewerBrowse);
			// 
			// logViewerTree
			// 
			this.logViewerTree.BackColor = System.Drawing.SystemColors.ScrollBar;
			this.logViewerTree.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.logViewerTree.Dock = System.Windows.Forms.DockStyle.Right;
			this.logViewerTree.ImageIndex = -1;
			this.logViewerTree.Location = new System.Drawing.Point(284, 0);
			this.logViewerTree.Name = "logViewerTree";
			this.logViewerTree.SelectedImageIndex = -1;
			this.logViewerTree.Size = new System.Drawing.Size(180, 256);
			this.logViewerTree.TabIndex = 7;
			this.logViewerTree.DoubleClick += new System.EventHandler(this.OnSelectPage);
			// 
			// axWebBrowser1
			// 
			this.axWebBrowser1.ContainingControl = this;
			this.axWebBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.axWebBrowser1.Enabled = true;
			this.axWebBrowser1.Location = new System.Drawing.Point(0, 0);
			this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState")));
			this.axWebBrowser1.Size = new System.Drawing.Size(281, 256);
			this.axWebBrowser1.TabIndex = 8;
			// 
			// panel1
			// 
			this.panel1.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.panel1.Controls.Add(this.axWebBrowser1);
			this.panel1.Controls.Add(this.splitter1);
			this.panel1.Controls.Add(this.logViewerTree);
			this.panel1.Location = new System.Drawing.Point(0, 80);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(464, 256);
			this.panel1.TabIndex = 9;
			// 
			// splitter1
			// 
			this.splitter1.Dock = System.Windows.Forms.DockStyle.Right;
			this.splitter1.Location = new System.Drawing.Point(281, 0);
			this.splitter1.Name = "splitter1";
			this.splitter1.Size = new System.Drawing.Size(3, 256);
			this.splitter1.TabIndex = 9;
			this.splitter1.TabStop = false;
			// 
			// progressBar1
			// 
			this.progressBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.progressBar1.Location = new System.Drawing.Point(208, 48);
			this.progressBar1.Name = "progressBar1";
			this.progressBar1.Size = new System.Drawing.Size(248, 23);
			this.progressBar1.TabIndex = 10;
			// 
			// timerThread
			// 
			this.timerThread.Interval = 1000;
			this.timerThread.Tick += new System.EventHandler(this.OnTimerTick);
			// 
			// LogViewer
			// 
			this.Controls.Add(this.progressBar1);
			this.Controls.Add(this.logViewerBrowser);
			this.Controls.Add(this.eventLogBox);
			this.Controls.Add(this.fileNameBox);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.panel1);
			this.Name = "LogViewer";
			this.Size = new System.Drawing.Size(464, 336);
			((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// Select an xml file
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnLogViewerBrowse(object sender, System.EventArgs e)
		{
			logViewerOpenFileDialog = new OpenFileDialog();
			logViewerOpenFileDialog.DefaultExt = ".xml";
			logViewerOpenFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
			logViewerOpenFileDialog.ReadOnlyChecked = true;
			logViewerOpenFileDialog.Title = "LogViewer Open XML File";
			logViewerOpenFileDialog.Filter = "All Files (*.*)|*.*|XML Files (*.xml)|.xml";
			if( logViewerOpenFileDialog.ShowDialog( this ) == DialogResult.OK )
			{
				FileName = logViewerOpenFileDialog.FileName;
				fileNameBox.Text = FileName;
				
			}

			progressBar1.Show();

			OnGetLogsThread();

		}

		private void OnGetLogsThread()
		{

			Cursor.Current = Cursors.WaitCursor;
			threadStart = new ThreadStart( OnGetLogs );
			thread = new Thread( threadStart );


			/// remove all html files
			string[] arrayHtml = Directory.GetFiles( Directory.GetCurrentDirectory(), "*.htm" );

			for( int i=0; i<arrayHtml.Length; i++ )
			{
				FileInfo info = new FileInfo( arrayHtml[ i ] );
				info.Delete();
			}

			logViewerTree.Nodes.Clear();

			timerThread.Start();

			thread.Start();
					
			progressBar1.PerformStep();

			Cursor.Current = Cursors.Default;
		}

		/// <summary>
		/// Once the user selects an xml file or an event log the seperate parts are written to
		/// their own htm files which are then loaded into the embedded explorer controls on the tab pages
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void OnGetLogs()
		{

			StringBuilder strElement = new StringBuilder();
			StringBuilder strTime = new StringBuilder();
			StringBuilder strPriority = new StringBuilder();
			StringBuilder strMessage = new StringBuilder();
			StringBuilder strTemp = new StringBuilder();
			ArrayList arrayFileList = new ArrayList();
			

			if( FileName != null && FileName != string.Empty )
			{
				FileStream fileStream = new FileStream( FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 8, true );
				xmlReader = new XmlTextReader( fileStream );

				try
				{
					bool bElement = false;

					/// get the data from the file before processing
					while( xmlReader.Read() == true )
					{
						switch( xmlReader.NodeType )
						{
							case XmlNodeType.Element : 
							{
								switch( xmlReader.Name )
								{
									case "Time" : 
									{
										xmlReader.Read();
										strTime.Remove( 0, strTime.Length );
										strTime.Append( xmlReader.Value );
									}break;
									case "Priority" : 
									{
										xmlReader.Read();
										strPriority.Remove( 0, strPriority.Length );
										strPriority.Append( xmlReader.Value );
									}break;
									case "Message" : 
									{
										xmlReader.Read();
										strMessage.Remove( 0, strMessage.Length );
										strMessage.Append( xmlReader.Value );
										bElement = false;
									}break;
									default :
									{
										strElement.Remove( 0, strElement.Length );
										strElement.Append( xmlReader.LocalName );
										bElement = true;
									}break;
								}

								if( bElement == false )
								{
									LogData data = new LogData();
									data.Element = strElement.ToString();
									data.Time = strTime.ToString();
									data.Priority = strPriority.ToString();
									data.Message = strMessage.ToString();

									arrayLogData.Add( data );
								}
							} break;
						}
					}
				}
				catch( XmlException xmlExp )
				{
					MessageBox.Show( "Error opening xml file " + xmlExp.Message );
				}
			}

			/// now process the data
			int nCount = arrayLogData.Count;
			string strEnd = "\n\t\t\t</TABLE>\n\t\t</FORM>\n\t</BODY>\n</HTML>";

			progressBar1.Maximum = nCount;
			progressBar1.Step = 1;
			progressBar1.Minimum = 0;
			progressBar1.Value = 0;
			
			for( int i=0; i<nCount; i++ )
			{
				LogData data = ( LogData )arrayLogData[ i ];
				if( strHtml != null && strHtml.Length > 0 )
					strHtml.Remove( 0, strHtml.Length );
				strHtml.Append( "<HTML>\n\t<HEAD>\n\t\t<TITLE>" + data.Element + "</TITLE>\n\t\t<META http-equiv=\"Content-Type\" content=\"text-html; charset=windows-1252\">\n\t\t</TITLE>\n\t</HEAD>\n\t<BODY>\n\t\t<FORM bgcolor=\"seashell\">\n\t\t\t<TABLE border=5 cellspacing=0 cellpadding=1 width=90%>" ); 

				FileInfo info = new FileInfo( data.Element + ".htm" );
				bool bInArray = false;
				if( arrayFileList != null && arrayFileList.Count != 0 )
				{
					foreach( FileData fileData in arrayFileList )
					{
						if( fileData.Element == data.Element )
							bInArray = true;
					}
				}

				FileStream fStream;
				if( bInArray == false )
				{
					info.Delete();
					fStream = info.Create();
					stream = new StreamWriter( fStream );

					FileData fileData = new FileData();
					fileData.Element = data.Element;
					fileData.File = info.FullName;
					fileData.Name = info.Name;
					arrayFileList.Add( fileData );

					stream.Write( strHtml.ToString(), 0, strHtml.Length );
				}
				else
				{
					try
					{
						stream = new StreamWriter( data.Element + ".htm", true );
					}
					catch( UnauthorizedAccessException uaExp )
					{
						MessageBox.Show( "Error opening the htm file " + data.Element + ".htm : " + uaExp.Message );
						Cursor.Current = Cursors.Default;
						return;
					}
					catch( ArgumentNullException argNullExp )
					{
						MessageBox.Show( "Error opening the htm file " + data.Element + ".htm : " + argNullExp.Message );
						Cursor.Current = Cursors.Default;
						return;
					}
					catch( ArgumentException argExp )
					{
						MessageBox.Show( "Error opening the htm file " + data.Element + ".htm : " + argExp.Message );
						Cursor.Current = Cursors.Default;
						return;
					}
					catch( DirectoryNotFoundException dirExp )
					{
						MessageBox.Show( "Error opening the htm file " + data.Element + ".htm : " + dirExp.Message );
						Cursor.Current = Cursors.Default;
						return;
					}
					catch( IOException ioExp )
					{
						MessageBox.Show( "Error opening the htm file " + data.Element + ".htm : " + ioExp.Message );
						Cursor.Current = Cursors.Default;
						return;
					}
					catch( SecurityException secExp )
					{
						MessageBox.Show( "Error opening the htm file " + data.Element + ".htm : " + secExp.Message );
						Cursor.Current = Cursors.Default;
						return;
					}
				}

				StreamWrite( stream, data );

				progressBar1.PerformStep();
			}

		

			/// end each html file
			foreach( FileData fileData in arrayFileList )
			{
				try
				{
					stream = new StreamWriter( fileData.File, true );
					stream.Write( strEnd, 0, strEnd.Length );
					stream.Close();
				}
				catch( UnauthorizedAccessException uaExp )
				{
					MessageBox.Show( "Error opening the htm file " + fileData.File + uaExp.Message );
					Cursor.Current = Cursors.Default;
					return;
				}
				catch( ArgumentNullException argNullExp )
				{
					MessageBox.Show( "Error opening the htm file " + fileData.File + argNullExp.Message );
					Cursor.Current = Cursors.Default;
					return;
				}
				catch( ArgumentException argExp )
				{
					MessageBox.Show( "Error opening the htm file " + fileData.File + argExp.Message );
					Cursor.Current = Cursors.Default;
					return;
				}
				catch( DirectoryNotFoundException dirExp )
				{
					MessageBox.Show( "Error opening the htm file " + fileData.File + dirExp.Message );
					Cursor.Current = Cursors.Default;
					return;
				}
				catch( IOException ioExp )
				{
					MessageBox.Show( "Error opening the htm file " + fileData.File + ioExp.Message );
					Cursor.Current = Cursors.Default;
					return;
				}
				catch( SecurityException secExp )
				{
					MessageBox.Show( "Error opening the htm file " + fileData.File + secExp.Message );
					Cursor.Current = Cursors.Default;
					return;
				}
			}

		}



		/// <summary>
		/// write a section of html to the htm file
		/// </summary>
		/// <param name="stream">Stream writer to write</param>
		/// <param name="data">Program Data to write</param>
		private void StreamWrite( StreamWriter stream, LogData data )
		{
			stream.Write( "\n\t\t\t\t<TR>\n\t\t\t\t\t<TD>" );
			stream.Write( "<FONT color=\"black\">" );
			stream.Write( data.Time );
			stream.Write( "\n\t\t\t\t\t</TD>" );
			stream.Write( "\n\t\t\t\t\t<TD>" );
			switch( data.Priority )
			{
				case "All" : stream.Write( "<FONT color=\"black\">" ); break;
				case "WarningsAndErrors" : stream.Write( "FONT color=\"gray\">" ); break;
				case "Errors" : stream.Write( "<FONT color=\"red\">" ); break;
				case "Warning" : stream.Write( "<FONT color=\"firebrick\">" ); break;
				case "Note" : stream.Write( "<FONT color=\"mediumblue\">" ); break;
				case "Important" : stream.Write( "<FONT color=\"cornflowerblue\">" ); break;
				case "HighLight" : stream.Write( "<FONT color=\"green\">" ); break;
				case "Progress" : stream.Write( "<FONT color=\"steelblue\">" ); break;
			}
			stream.Write( data.Priority );
			stream.Write( "</FONT>" );
			stream.Write( "\n\t\t\t\t\t</TD>" );
			stream.Write( "\n\t\t\t\t\t<TD>" );
			switch( data.Priority )
			{
				case "All" : stream.Write( "<FONT color=\"black\">" ); break;
				case "WarningsAndErrors" : stream.Write( "FONT color=\"gray\">" ); break;
				case "Errors" : stream.Write( "<FONT color=\"red\">" ); break;
				case "Warning" : stream.Write( "<FONT color=\"firebrick\">" ); break;
				case "Note" : stream.Write( "<FONT color=\"mediumblue\">" ); break;
				case "Important" : stream.Write( "<FONT color=\"cornflowerblue\">" ); break;
				case "HighLight" : stream.Write( "<FONT color=\"green\">" ); break;
				case "Progress" : stream.Write( "<FONT color=\"steelblue\">" ); break;
			}
			stream.Write( data.Message );
			stream.Write( "</FONT>" );
			stream.Write( "\n\t\t\t\t\t</TD>" );
			stream.Write( "\n\t</TR>" );

			stream.Close();
		}

		private void OnSelectPage(object sender, System.EventArgs e)
		{
			StringBuilder strPage = new StringBuilder();

			strPage.Append( Directory.GetCurrentDirectory() );
			strPage.Append( "\\" );
			strPage.Append( logViewerTree.SelectedNode.Text );
			strPage.Append( ".htm" );

			object flags = new object();
			object targetFrameName = new object();
			object postData = new object();
			object headers = new object();

			try
			{
				axWebBrowser1.Navigate( strPage.ToString(), ref flags, ref targetFrameName, ref postData, ref headers );
				/*	string strTest = ( string )targetFrameName;
					bool post = ( bool )postData;
					string strflags = ( string )flags;
					string strheaders = ( string )headers; */
			}
			catch( Exception exp )
			{
				MessageBox.Show( "Just Checking " + exp.Message );
			}
		}

		/// <summary>
		/// use a timer to wait for the thread to finish as using a wait is freezing the program
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void OnTimerTick(object sender, System.EventArgs e)
		{
			
			if( thread.IsAlive == false )
			{

				/// add all htm files to the display
				string[] arrayFiles = Directory.GetFiles( Directory.GetCurrentDirectory(), "*.htm" );
				StringBuilder strName = new StringBuilder( " " );
				StringBuilder strTest = new StringBuilder( " " );
				int nStartCount = 0;
				int nLetterCount = 0;
				for( int i=0; i<arrayFiles.Length; i++ )
				{
					strName.Remove( 0, strName.Length );
					nStartCount = arrayFiles[ i ].LastIndexOf( "\\" );
					strName.Append( arrayFiles[ i ].Substring( nStartCount+1 )  );
					nLetterCount = strName.ToString().IndexOf( ".htm" );
					strTest.Remove( 0, strTest.Length );
					strTest.Append( strName.ToString().Substring( 0, nLetterCount ) );
					logViewerTree.Nodes.Add( strTest.ToString() );

					strName.Remove( 0, strName.Length );
				}

				progressBar1.Hide();

				timerThread.Stop();
			}
		}
	}
}

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
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