This project is read-only.

Object Viewer HOW-TO

Apr 11, 2010 at 1:49 AM
Edited Apr 15, 2010 at 3:09 AM

Object Viewer Control


Updated.  4/14/2010.  The event handlers (Serialize and Deserialize) are now optional.  If they are not specified, the default serializer / deserializer procedure(s) will run.  Also the EditXML event handler are introduced to allow custom XML editing.


 

The object viewer (ObjectViewer introduced in 2.2.0.3) is a user control that allow the user to view and modify an object of any class. The ObjectViewer's user interface consists of three standard controls: a Textbox, WebBrowser, and PropertyGrid. 

  • The text box allows the user to make changes the XML document.

  • The web browser shows the XML document in a color-coded and formatted view.

  • The property grid allows the user to make changes to the property's value at the property's level.

 There are a few additional controls to help the user with identification and usage: 

  • Caption Label (appear on the upper left corner) is a short description to inform the user the purpose of this control.

  • Context menu strip, attached to the base user control, to allow the user to toggle between the tab view and split view. 

Quick-Starter Instruction:

Here is a quick instruction to get started.  Scroll to the bottom for the full code sample.

  1. Drag-and-drop the ObjectViewer control to your Windows Form or User Control.
  2. Switch to the code view.

  3. (OPTIONAL STEP) Hookup the Serialize and Deserialize event handlers on the constructor:

  4. (OPTIONAL STEP) Add your custom procedure to serialize and deserialize.

  5. Create an instance of your class and set it to the objectViewer1.PropertyObject property.  (Alternatively, pass in the XML Text via objectViewer1.Text property)

  6. Run the application.

Class Design

 ObjectViewer exposes the following properties and event handlers: 

  • PropertyObject – get or set the object used by the viewer. For ease of design, this object is set directly to the PropertyGrid.SelectedObject property. This design insure the PropertyGrid and this object to be always synchronized. This object may be re-instantiated numerous times throughout the life of this control; therefore, always get the latest object via this property to get the most up-to-date object. When a set occur, it will update the XML Text.

  • Text – the XML text. When a set occur, the value will be reformatted, and will update the PropertyObject. (The Text and PropertyObject are always synchronized throughout the life of this control.)

  • Caption – an optional property to display a brief description on the upper-left corner.

  • TextBoxVisible – show or hide the text box control.

  • ViewMode – toggle between the tab view and the split view.

  • Serialize – event handler to allow custom serialization process.

  • Deserialize – event handler to allow custom deserialization process.

  • EditXML - event handler to allow custom XML editor.

Full Code Sample

  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;

using crudwork.Utilities;
using crudwork.Controls.DataTools;

namespace ControlTestBed
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
			objectViewer1.Serialize += new EventHandler<SerializationEventArgs>(objectViewer1_Serialize);
			objectViewer1.Deserialize += new EventHandler<SerializationEventArgs>(objectViewer1_Deserialize);
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			var foo = new Foo()
			{
				First = "John",
				Middle = "H",
				Last = "Doe",
				Address = "123 Main St",
				City = "New land",
				State = "CA",
				Zipcode = "90001",
				CurrentAddress = true,
				LastUpdated = DateTime.Parse("8/17/2008"),
				AddressType = AddressType.Home,
			};
			objectViewer1.PropertyObject = foo;
			objectViewer1.ViewMode = crudwork.Controls.DataTools.ViewMode.Split;
		}

		void objectViewer1_Deserialize(object sender, crudwork.Controls.DataTools.SerializationEventArgs e)
		{
			e.TheObject = Serializer.Deserialize<Foo>(e.XmlString, Serializer.SerializeMethods.Xml);
		}
		void objectViewer1_Serialize(object sender, crudwork.Controls.DataTools.SerializationEventArgs e)
		{
			e.XmlString = Serializer.Serialize(e.TheObject, Serializer.SerializeMethods.Xml);
		}

		public enum AddressType
		{
			Home,
			Business,
		}

		public class Foo
		{
			[Category("Contact Info"), Description("First Name")]
			public string First
			{
				get;
				set;
			}
			[Category("Contact Info"), Description("Middle Name")]
			public string Middle
			{
				get;
				set;
			}
			[Category("Contact Info"), Description("Last Name")]
			public string Last
			{
				get;
				set;
			}

			[Category("Address Info")]
			public string Address
			{
				get;
				set;
			}
			[Category("Address Info")]
			public string City
			{
				get;
				set;
			}
			[Category("Address Info")]
			public string State
			{
				get;
				set;
			}
			[Category("Address Info")]
			public string Zipcode
			{
				get;
				set;
			}

			[Category("Other Info")]
			public AddressType AddressType
			{
				get;
				set;
			}
			[Category("Other Info"), Description("Specify whether this is the current address")]
			public bool CurrentAddress
			{
				get;
				set;
			}
			[Category("Other Info"), Description("The date/time record was last updated")]
			public DateTime LastUpdated
			{
				get;
				set;
			}
		}
	}
}