Click here to Skip to main content
15,881,803 members
Articles / Programming Languages / C#

MyXAML--XAML-style gui generator (added styles)

Rate me:
Please Sign up or sign in to vote.
4.85/5 (76 votes)
9 Mar 2004CPOL11 min read 394.8K   165  
Generate controls, set property values, and wire up event handlers at runtime from an XML definition.
/*
Copyright (c) 2004, Marc Clifton
All rights reserved.
Usage restricted under the terms of the BSD License conditions:
http://opensource.org/licenses/bsd-license.php
*/

using System;
using System.Drawing;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;

/*
	This code needs to be refactored.  Compare to ControlCollectionModel.
	There's a bit too much redundancy here.
*/

namespace XmlGuiGenerator
{
	/// <summary>
	/// Manage the TabPageCollection data type.
	/// </summary>
	public class TabPageCollectionModel : PropertyModelBase
	{
		/// <summary>
		/// Add a tab page to the control's TabPages collection.
		/// </summary>
		/// <param name="obj">The Control instance with a TabPages property.</param>
		/// <param name="pi">The PropertyInfo instance.</param>
		/// <param name="val">The tab page name, used as the form name to load the page's controls.</param>
		/// <param name="target">Passed to the sub-form creation process.</param>
		/// <returns>The TabPage instance.</returns>
		public override object SetValue(object obj, PropertyInfo pi, string val, object target)
		{
			TabPage page=null;
			bool inline=false;
			try
			{
				// the parent is set to null, otherwise the page is automatically
				// added to the page list (that's OK, but I like to do it more
				// explicitly!)
				XmlAttribute attr=currentNode.Attributes["Canvas"];
				if (attr != null)
				{
					// the tab page is defined by a canvas!
					page=(TabPage)generator.CreateForm(attr.InnerText, null, target, null);
				}
				else
				{
					// the tab page is inlined!
					page=new TabPage();
					generator.ProcessAttributes(page, currentNode.Attributes, target);
					inline=true;
				}

				generator.ProcessCollections(page, currentNode, target);

				// nothing fancy here.  Since the TabPageCollection is only ever
				// a member of TabControl, there is no reason to make this a 
				// generic setter.
				((TabControl)obj).TabPages.Add(page);

				if (inline)
				{
					// when inlined, still invoke the Load event method
					generator.InvokeLoadEvent(currentNode, target, page);
				}
			}
			catch(Exception e)
			{
				Trace.WriteLine("TabPageCollection: Setter failed for: "+pi.ToString()+"   val="+val+"   "+e.Message);
			}
			return page;
		}
	}
}

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
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions