Creating Windows Form User Controls Part -1
Add Comment<div align="center"> <table border="0" width="70%" class="outline"> <tr> <td width="50%" class="outline"><b>Download File</b></td> <td width="50%" class="outline"><b>SDK</b></td> </tr> <tr> <td width="50%" class="outline"><a href="../../file/labeledtextbox.zip" class="wbox">labeledtextbox1.zip</a> (7kb)</td> <td width="50%" class="outline">Beta2</td> </tr> </table> </div> <p> <span class=wboxheado>Introduction</span><br> As we all know that the .NET Platform provides us with a Rich <b> Windows Form</b> API to create Windows Applications. Not only this model rich, but its also very <i>extensible</i>! Developers can quickly create their own controls and extend the API as they want!<br> In this article we will see how to create our own Windows Form User Control to help us create a new '<b>LabledTextBox</b>' control.</p> <p><span class=wboxheado>User Controls</span><br> In the Windows Form API, you can build your own controls using other controls. These can be thought of as composite controls. Such controls are called User Controls. They essentially are composed out of previously existing controls. <br> Say, you have a database driven application and you use the same form components a number of times in your application. Instead of defining all the individual controls again and again, you can define a User Control containing the form contents and later you can use this User Control over and over again. This saves you a lot of development time! Also incase, in the future a new column gets added to the database you just have to update the User Control and all the forms will show the updated form (lot of time saved here!!)! You can also create a User Control that validates the text entered in the TextBox...</p> <p>In this example, I create a LabledTextBox control. Many times while we create a Form to accept user data, we place a Label describing the data to be entered and then we place a TextBox next to it to enter the data, right!!<br> This is a very tedious process and managing all those labels and textboxes .....awggghhh..<br> So we create a new User Control composing of a Label and a TextBox, so you just have to add one control per field of your form!<br> Note: I am using notepad as my editor, so there is no need for Visual Studio.Net to create this User Control!</p> <p><span class=wboxheado>Code</span><br> <b> 1) LabeledTextBox.cs</b> - The User Control file</p> <table border="0" width="100%" class="code"> <tr> <td width="100%"><pre>using System.Windows.Forms; namespace MasterCSharp.WebSite.Saurabh.UserControls { <span class=cmt>//LabledTextBox class that extends the class UserControl</span> public class LabeledTextBox: UserControl { <span class=cmt>//Define our components</span> private Label myLabel; private TextBox myTextBox; <span class=cmt>//Constructor</span> public LabeledTextBox() { InitializeComponent(); } public void InitializeComponent() { <span class=cmt>//Label Definition</span> myLabel = new Label(); myLabel.Location = new System.Drawing.Point(0,0); myLabel.Size = new System.Drawing.Size (50, 20); <span class=cmt>//TextBox Definition</span> myTextBox = new TextBox(); <span class=cmt>//Set the location to Label width //plus 5 pixels more</span> myTextBox.Location = new System.Drawing.Point(105,0); myTextBox.Size =new System.Drawing.Size (100, 20); <span class=cmt>//Always set the Size of the Control or you will get unexpected results!!</span> this.Size =new System.Drawing.Size (205, 20); <span class=cmt>//Add the controls </span> this.Controls.Add(myLabel); this.Controls.Add(myTextBox); } <span class=cmt>//Override the <b>Text</b> property to get/set the Text of the Textbox</span> public override string Text { get { return myTextBox.Text; } set { myTextBox.Text = value; } } <span class=cmt>//Create a new property to //get/set the Text of the Label</span> public string LabelText { get { return myLabel.Text; } set { myLabel.Text =value; } } } }</pre></td> </tr> </table> <p>Save the above file as '<b>LabeledTexBox.cs</b>'. The use the <b> csc</b> command-line compiler to compile this file into a library. The compilation string should be:<br> <b> csc /t:library /r:System.Windows.Forms.dll LabeledTextBox.cs</b><br> This will produce a library Dll with the file name '<b>LabeledTextBox.dll</b>'.<br> Your user control is ready !!</p> <p><b>2) ControlConsumer.cs</b> - A Sample LabeledTextBox Control Consumer Application</p> <table border="0" width="100%" class="code"> <tr> <td width="100%"><pre>using System.Windows.Forms; using MasterCSharp.WebSite.Saurabh.UserControls ; using System; public class ControlConsumer: Form { <span class=cmt>//Declare some Controls</span> protected LabeledTextBox firstName, lastName, email ; protected Button showContents; <span class=cmt>//Constructor</span> public ControlConsumer() { InitializeComponent(); } <span class=cmt>//Main Method</span> [STAThread] public static void Main() { Application.Run(new ControlConsumer()); } public void InitializeComponent() { <span class=cmt>//Create the controls</span> firstName = new LabeledTextBox(); lastName = new LabeledTextBox(); email = new LabeledTextBox(); showContents= new Button(); <span class=cmt>//Set the various Properties of the Controls</span> firstName.Location = new System.Drawing.Point(5,5); firstName.LabelText="First Name :" ; lastName.Location = new System.Drawing.Point(5,35); lastName.LabelText="Last Name :" ; email.Location = new System.Drawing.Point(5,70); email.LabelText="Email :" ; showContents.Location = new System.Drawing.Point(5,100); showContents.Text="Show Contents"; showContents.Size=new System.Drawing.Size (100, 20); <span class=cmt>//Wire the Click event of the button</span> showContents.Click += new System.EventHandler(showContents_Click); this.Text="Control Consumer"; <span class=cmt>//Add the controls to the Form this.Controls.Add(firstName);</span> this.Controls.Add(lastName); this.Controls.Add(email); this.Controls.Add(showContents); } <span class=cmt>//Button Click Event Handler</span> protected void showContents_Click(object sender, EventArgs e) { string message ="First Name :"+firstName.Text; message+="\nLast Name :"+lastName.Text; message+="\nEmail :"+email.Text; <span class=cmt>//Show the Contents of the Windows Form</span> MessageBox.Show(message); } }</pre></td> </tr> </table> <p>Save the above code as '<b>ControlConsumer.cs</b>'. The again use the <i> csc </i> command-line compiler with the command:<br> <b> csc /t:winexe /r:Systm.Windows.Form.dll;LabeledTextBox.dll ControlConsumer.cs</b><br> This should create a '<b>ControlConsumer.exe</b>' file. Click on this file to run the application.<br> Remember that the 'labeledTextBox.dll' we created in the last part should be in the same directory as this application, or your code won't compile and run!</p> <p align="center"><img border="0" src="../../img/windowsformusercontrol1.gif" width="300" height="300"><br> <b> Figure 1:</b> Control Consumer Application</p> <p align="left"><span class=wboxheado>Conclusion</span><br> In this example I have just displayed limited functionality for simplicity sake (may be I was a bit lazy :) ), anyway feel free to add more properties to make this a jazzy control! <br> This example should have given rise to many possible implementations of User Controls in your mind! So go ahead and try your make your wild control!<br> Next time I might look at modifying this simple user control to add more functionality!!<br> Till then ... keep coding :)</p>