C# Source Code: Creating a runtime test form for your custom .NET controls
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
C# Source Code
Creating a runtime test form for your custom .NET controls
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Wednesday, August 11, 2004
Hits:
1113
Category:
Windows Forms/GUI/Controls
Article:
When creating a new control in .NET you would typically test it by adding the control to a form and then proceed to add a number of Buttons, CheckBoxes and ComboBoxes to enable you to change it's properties at runtime. This task in no longer neccessary, since .NET includes a property grid which can be simply added to your form. The windows form project below shows you how to use a property grid to enable you to change all the controls properties at runtime: using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Runtime.InteropServices; using System.Windows.Forms; using System.Threading; namespace WindowsApplication3 { ///
/// Summary description for Form1. ///
public class frmControlTestBed : System.Windows.Forms.Form { private System.Windows.Forms.TextBox myTestTextbox; private System.Windows.Forms.PropertyGrid ControlProperties; private System.Windows.Forms.Label lblTestControl; private System.Windows.Forms.Label lblPropertyHeader; ///
/// Required designer variable. ///
private System.ComponentModel.Container components = null; public frmControlTestBed() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } ///
/// Clean up any resources being used. ///
protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code ///
/// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///
private void InitializeComponent() { this.ControlProperties = new System.Windows.Forms.PropertyGrid(); this.myTestTextbox = new System.Windows.Forms.TextBox(); this.lblTestControl = new System.Windows.Forms.Label(); this.lblPropertyHeader = new System.Windows.Forms.Label(); this.SuspendLayout(); // // ControlProperties // this.ControlProperties.CommandsVisibleIfAvailable = true; this.ControlProperties.LargeButtons = false; this.ControlProperties.LineColor = System.Drawing.SystemColors.ScrollBar; this.ControlProperties.Location = new System.Drawing.Point(4, 38); this.ControlProperties.Name = "ControlProperties"; this.ControlProperties.PropertySort = System.Windows.Forms.PropertySort.Alphabetical; this.ControlProperties.SelectedObject = this.myTestTextbox; this.ControlProperties.Size = new System.Drawing.Size(300, 276); this.ControlProperties.TabIndex = 2; this.ControlProperties.Text = "PropertyGrid"; this.ControlProperties.ToolbarVisible = false; this.ControlProperties.ViewBackColor = System.Drawing.SystemColors.Window; this.ControlProperties.ViewForeColor = System.Drawing.SystemColors.WindowText; // // myTestTextbox // this.myTestTextbox.Location = new System.Drawing.Point(318, 152); this.myTestTextbox.Name = "myTestTextbox"; this.myTestTextbox.Size = new System.Drawing.Size(106, 20); this.myTestTextbox.TabIndex = 1; this.myTestTextbox.Text = ""; // // lblTestControl // this.lblTestControl.Location = new System.Drawing.Point(318, 114); this.lblTestControl.Name = "lblTestControl"; this.lblTestControl.Size = new System.Drawing.Size(104, 30); this.lblTestControl.TabIndex = 3; this.lblTestControl.Text = "Test Control the control below:"; // // lblPropertyHeader // this.lblPropertyHeader.Location = new System.Drawing.Point(6, 3); this.lblPropertyHeader.Name = "lblPropertyHeader"; this.lblPropertyHeader.Size = new System.Drawing.Size(298, 43); this.lblPropertyHeader.TabIndex = 4; this.lblPropertyHeader.Text = "Set the control properties here (note, the control is linked to the property grid" + " by the \"SelectedObject\" property):"; // // frmControlTestBed // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(432, 317); this.Controls.Add(this.myTestTextbox); this.Controls.Add(this.ControlProperties); this.Controls.Add(this.lblTestControl); this.Controls.Add(this.lblPropertyHeader); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Name = "frmControlTestBed"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Run Time Control Test Bed"; this.ResumeLayout(false); } #endregion ///
/// The main entry point for the application. ///
[STAThread] static void Main() { Application.Run(new frmControlTestBed()); } } }
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet