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

RemoteConsole

Rate me:
Please Sign up or sign in to vote.
3.20/5 (2 votes)
27 Aug 2007CPOL2 min read 33K   674   26  
Remote console allows to send commands to a remote computer located on the Internet
namespace RemoteControl
{
	#region Reference

	using System;
	using System.Windows.Forms;
	using System.Text;
	using System.Collections.Specialized;


	#endregion

	public delegate void PressEnterDelegate(string command,string arguments);

	/// <summary>
	/// OutputWindow displays messsages on user interface
	/// </summary>
	public class OutputWindow : UserControl
	{
		#region Fields
		
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		
		public event PressEnterDelegate PressEnterEvent;
		
		private int _Index = -1;
		
		private StringCollection _CommandCollection;
		//public string _WorkingPath;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		#endregion

		#region Construction

		public OutputWindow()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			_CommandCollection = new StringCollection();
		}

		
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#endregion

		#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.textBox1 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.BackColor = System.Drawing.Color.Black;
			this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.textBox1.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.textBox1.ForeColor = System.Drawing.Color.LightGray;
			this.textBox1.Location = new System.Drawing.Point(0, 0);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.ReadOnly = true;
			this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.textBox1.Size = new System.Drawing.Size(307, 377);
			this.textBox1.TabIndex = 2;
			this.textBox1.TabStop = false;
			this.textBox1.WordWrap = false;
			// 
			// textBox2
			// 
			this.textBox2.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.textBox2.Location = new System.Drawing.Point(0, 377);
			this.textBox2.Multiline = true;
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(307, 17);
			this.textBox2.TabIndex = 1;
			this.textBox2.Text = "";
			this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox2_KeyPress);
			// 
			// OutputWindow
			// 
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.textBox2);
			this.Name = "OutputWindow";
			this.Size = new System.Drawing.Size(307, 394);
			this.ResumeLayout(false);

		}
		#endregion

		#region Methods

		public void AddLines(string[] lines)
		{
			StringBuilder sb = new StringBuilder();

			for(int i=0; i<lines.Length; i++)
			{
				if (lines[i] != String.Empty)
					sb.Append( lines[i].Replace("�"," ") + "\r\n" );
			}
						
			textBox1.Text = sb.ToString(); 
			textBox1.AppendText(" ");
		}

		public void AddLine(string message)
		{
			textBox1.AppendText( message.Replace("�"," ") + "\r\n" ); 
		}

		public void Clear()
		{
			textBox1.Text = string.Empty;
		}

		
		/// <summary>
		/// Creates a command and a list of arguments and fire PressEnterEvent evemt
		/// </summary>
		/// <param name="e"></param>
		/// <param name="stream"></param>
		private void KeyStroke(KeyPressEventArgs e, string stream)
		{
			if( e.KeyChar == (char)Keys.Enter && PressEnterEvent!=null && stream!=string.Empty ) 
			{
				string _command = string.Empty;
				string _arguments = string.Empty;
				stream = stream.Replace("\r\n", string.Empty);
				string[] _elements = stream.Split(' ');
												
				if (_elements[0] == string.Empty)
					return;
				
				if (_elements[0] == "dir")
				{
					_command = "cmd.exe";
					_arguments = "/c " + stream;
				}
				else
				{
					_command = _elements[0];
					if (_elements.Length>1)
						_arguments = stream.Remove( 0, _command.Length + 1);
				}

				PressEnterEvent( _command, _arguments );
				if (stream != string.Empty)
				{
					_CommandCollection.Add( stream );
					_Index =_CommandCollection.Count-1;
					textBox2.Text = string.Empty;
				}
			}
		}

		private void textBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if( e.KeyChar == (char)Keys.Enter )
			{
				KeyStroke( e, textBox2.Text );
			}
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			if (keyData==Keys.Up && _Index>0)
			{
				_Index--;
			}

			if (keyData==Keys.Down && _Index<_CommandCollection.Count-1)
			{
				_Index++;
			}

			if (keyData==Keys.Up || keyData == Keys.Down)
			{
				textBox2.Text = _CommandCollection[_Index];
				return true;
			}

			return base.ProcessCmdKey (ref msg, keyData);
		}


		#endregion
		
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions