Click here to Skip to main content
15,884,099 members
Articles / Artificial Intelligence / Machine Learning

Genetics Dot Net Two - Adaptive Programming

Rate me:
Please Sign up or sign in to vote.
4.83/5 (20 votes)
16 May 200722 min read 82.3K   1.6K   85  
A Look at Adaptive Programming with Genetic Algorithms
/*
 * Created by SharpDevelop.
 * User: pseudonym67
 * Date: 23/03/2006
 * Time: 11:27
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
 
// wrapper to allow thread safe use of textboxes
// Copyright (C) 2006 pseudonym67
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


using System;
using System.Windows.Forms;
using System.Drawing;

namespace UsefulClasses
{
	/// <summary>
	/// Description of ThreadSafeTextBox.
	/// </summary>
	public class ThreadSafeTextBox
	{
		private TextBox tbTextBox;
		private RichTextBox rtbTextBox;
		
		/// delegates
        /// 
        delegate void SetColorCallBack( Color color );
        delegate void SetTextCallBack( string text );

        public TextBox GetTextBox
        {
            get
            {
                return tbTextBox;
            }
        }

        public RichTextBox GetRichTextBox
        {
            get
            {
                return rtbTextBox;
            }
        }
		
		public ThreadSafeTextBox( TextBox textBox )
		{
			tbTextBox = textBox;
			rtbTextBox = null;
		}
		
		public ThreadSafeTextBox( RichTextBox textBox )
		{
			tbTextBox = null;
			rtbTextBox = textBox;
		}
		
		public void AppendText( string text )
		{
			if( tbTextBox != null )
			{
				if( tbTextBox.InvokeRequired == true )
				{
					SetTextCallBack t = new SetTextCallBack( SetText );
					tbTextBox.Invoke( t, new object[]{ text + "\n" } );
				}
				else
				{
					tbTextBox.AppendText( text + "\n" );
				}
			}
			
			if( rtbTextBox != null )
			{
				if( rtbTextBox.InvokeRequired == true )
				{
					SetTextCallBack t = new SetTextCallBack( SetText );
					rtbTextBox.Invoke( t, new object[]{ text + "\n" } );
				}
				else
				{
					rtbTextBox.AppendText( text + "\n" );
				}
			}
		}
		
		public void AppendTextWithColour( string text, Color color )
		{
			if( tbTextBox != null )
			{
				if( tbTextBox.InvokeRequired == true )
				{
					SetTextCallBack t = new SetTextCallBack( SetText );
					tbTextBox.Invoke( t, new object[]{ text + "\n" } );
				}
				else
				{
					tbTextBox.AppendText( text + "\n" );
				}
			}
			
			if( rtbTextBox != null )
			{
				if( rtbTextBox.InvokeRequired == true )
				{
					SetColorCallBack c = new SetColorCallBack( SetColour );
					rtbTextBox.Invoke( c, new object[]{ color } );
					SetTextCallBack t = new SetTextCallBack( SetText );
					rtbTextBox.Invoke( t, new object[]{ text + "\n" } );
				}
				else
				{
					rtbTextBox.SelectionColor = color;
					rtbTextBox.AppendText( text + "\n" );
				}
			}
		}
		
		public void AppendTextWithColour( string text, Color color, bool newLine )
		{
			if( tbTextBox != null )
			{
				if( tbTextBox.InvokeRequired == true )
				{
					SetTextCallBack t = new SetTextCallBack( SetText );
					if( newLine == true )
					{
						tbTextBox.Invoke( t, new object[]{ text + "\n" } );
					}
					else
						tbTextBox.Invoke( t, new object[]{ text } );
				}
				else
				{
					if( newLine == true )
					{
						tbTextBox.AppendText( text + "\n" );
					}
					else
						tbTextBox.AppendText( text );
				}
			}
			
			if( rtbTextBox != null )
			{
				if( rtbTextBox.InvokeRequired == true )
				{
					SetColorCallBack c = new SetColorCallBack( SetColour );
					rtbTextBox.Invoke( c, new object[]{ color } );
					SetTextCallBack t = new SetTextCallBack( SetText );
					if( newLine == true )
					{
						rtbTextBox.Invoke( t, new object[]{ text + "\n" } );
					}
					else
						rtbTextBox.Invoke( t, new object[]{ text } );
				}
				else
				{
					rtbTextBox.SelectionColor = color;
					if( newLine == true )
					{
						rtbTextBox.AppendText( text + "\n" );
					}
					else
						rtbTextBox.AppendText( text );
				}
			}
		}

        private void SetColour( Color color )
        {
        	if( rtbTextBox != null )
        		rtbTextBox.SelectionColor = color;
        }

        private void SetText( string text )
        {
        	if( tbTextBox != null )
        	{
        		tbTextBox.AppendText( text );
        	}
        	
        	if( rtbTextBox != null )
        	{
        		rtbTextBox.AppendText( text );
        	}
        }  
        
	}
}

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