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

Extending user control and IDE/VS.NET Toolbox

Rate me:
Please Sign up or sign in to vote.
4.00/5 (15 votes)
27 Aug 20034 min read 68.6K   1.1K   21  
Extending user control and IDE Toolbox
<html>

<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<title>TextBox ctxtbx </title>
<style>
<!--
 
-->
</style>
</head>

<body link=blue vlink=purple>

<div>

<h1>Subject: Extending
user control and IDE Toolbox</h1>

<p><b>Category: </b>Events/Delegates, Windows Form,
Custom Controls, IDE Toolbox Extension</p>

<p><b>&nbsp;</b></p>

<p><b>Author: </b>Norman Fung</p>

<p><b>Contact: </b><a
href="mailto:norman.fung@applied-algorithms.com">norman.fung@applied-algorithms.com</a>,
<a href="mailto:norman_lm_fung@hotmail.com">norman_lm_fung@hotmail.com</a>� </span></p>

<p><b>Submission date: </b>12 August 2003</p>

<p>&nbsp;</p>

<p><b>&nbsp;</b></p>

<p><b>I.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</b><b>WHAT IS IT:</b></p>

<p>1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&quot;ctxtbx&quot;
inherits from TextBox (<i>System.Windows.Forms</i>). Control extension: If
length of character string in textbox (ctxtbx.Text.Length) exceeds a specified
number of characters (ctxt.FlashLength), textbox starts flashing, indicating to
user that entered character string is invalid (too long).</p>

<p>&nbsp;</p>

<p>2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Purpose
of this project is to demonstrate the following:</p>

<p>a.To
extend user control via delegates and events.</p>

<p>b.To add
custom control to Toolbox. Access control property via Property Browser.</p>

<p>&nbsp;</p>

<p><b>3.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</b>The
control is packaged in one single dll. It can be consumed by any .NET/WIN32
client. </p>

<p><b>&nbsp;</b></p>

<p>4.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>APPLICATION
NOTE </b>provides
necessary information to deploy this module to C#/VB.NET client. Please be
reminded that RAD tools is only available to C#/VB.NET. If you intend to
consume this package in a managed C++/COM/WIN32 client, procedures provided in
APPLICATION NOTES does not readily apply.</p>

<p>&nbsp;</p>

<p>5.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>P</b>erquisite to this tutorial: C#/VB.NET,
Windows Forms, UEM (unified events modeling). A good understanding of the
following subjects would help, but not entirely necessary: assemblies, strong
names, PPK, GAC.</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p><b>II.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b><b>APPLICATION
NOTE:</b></p>

<p>&nbsp;</p>

<p>����������� 1.
To install module into GAC (Global Assembly Cache):</p>

<p>a.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Key
file: <b><i>txtbxlib.snk </i></b>(This step is already done. Key file is in
your solution folder.) Key file is generated with sn.exe, and it�s referenced
by �AssemblyInfo.cs�. For those of you who�re not entirely familiar with the
procedure, read up on the following topic: GAC, sn.exe, assembly, strong name.</p>

<p>&nbsp;</p>

<p>For your
information, here�s the command prompt entry: </p>

<p>�sn
-k txtbxlib.snk�</p>

<p>&nbsp;</p>

<p>b.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Visual
Studio.NET command prompt: gacutil -i txtbxlib.dll (You need to do this
yourself.)</p>

<p>����������������������� �� (/photo/Fig 5)</p>

<p>&nbsp;</p>

<p>2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
To add
control to <b><i>Toolbox</i></b>:</p>

<ol start=2 type=1>
 <ol start=1 type=a>
  <li>right click anywhere on Toolbox. (/photos/Fig
      1)</li>
 </ol>
</ol>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p><img border=0 width=492 height=369
src="./ctxtbx.ReadMe_files/image002.jpg"></span></p>

<p>&nbsp;</p>

<ol start=2 type=1>
 <ol start=2 type=a>
  <li>Select �Customize Toolbox� (/Photo/Fig 1,2)</li>
 </ol>
</ol>

<p>&nbsp;</p>

<ol start=2 type=1>
 <ol start=3 type=a>
  <li>Select �.NET Framework components� (/photo/Fig
      2)</li>
 </ol>
</ol>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p><img border=0 width=528 height=397
src="./ctxtbx.ReadMe_files/image004.jpg"></span></p>

<p>&nbsp;</p>

<ol start=2 type=1>
 <ol start=4 type=a>
  <li>Browse, and select txtbxlib.dll (/photo/Fig 3)</li>
 </ol>
</ol>

<p>&nbsp;</p>

<p><img border=0 width=504 height=379
src="./ctxtbx.ReadMe_files/image005.jpg"></span></p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>To use the control
in your Windows Form, just drag from Toolbox and drop it onto your form.
(/photo/Fig 1)</p>

<p>����������� </p>

<p>3.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Three
control properties:</p>

<p>&nbsp;</p>

<p>a.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b><i>ctxtbx.FlashLength</i></b>(Length of character string in textbox to trigger
flashing. Default: 10)</p>

<p>b.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b><i>ctxtbx.flashColor</i></b> (Flash color. Default:
System.Drawing.Color.Red)</p>

<p>c.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b><i>ctxtbx.flashfreq
</i></b>(Flash
frequency. Default: 1)</p>

<p>&nbsp;</p>

<p>You can access these
properties from &quot;Properties Browser&quot;�
(/photo/Fig 4), or in class <b><i>ctxtbx </i></b>constructor. Now,
select the editbox and pay attention to browser and Fig 6 (class hierarchy).
Please note that <i>ONLY <b>properties </b></i>can be found under �Properties
Browser� � other class attributes do not get displayed here. </p>

<p>&nbsp;</p>

<p><img border=0 width=528 height=396
src="./ctxtbx.ReadMe_files/image007.jpg"></span></p>

<p>&nbsp;</p>

<p>That�s all for client implementation, just drag the
control from the Toolbox and drop it on your Windows form. A sample application
is included in the package for demonstration purpose (SomeClient). Before you
run the executable, make sure:</p>

<ol start=3 type=1>
 <ol start=1 type=a>
  <li>control assembly (txtbxlib.dll) has been
      installed into GAC.</li>
  <li>Toolbox references to txtbxlib.dll</li>
 </ol>
</ol>

<p>&nbsp;</p>

<p>Now, that�s the easy way � which implies that there�s
an alternative: Just cut and paste the control class �<b><i>ctxtbx� </i></b>from
library source file <b><i>�txtbxlib.cs�</i></b>, and instantiate <b><i>ctxtbx </i></b>in
your Windows Form�s constructor. Be sure to include the following namespace: <i>System.Threading</i>,
<i>System.Drawing</i>, <i>System.Windows</i>, <i>System.Windows.Forms</i>, <i>System</i>.</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p><b>III.&nbsp;&nbsp;&nbsp; </b><b>Architecture
and implementation:</b></p>

<p>&nbsp;</p>

<p>Everything
in <b>c<i>txtbx </i></b>is packaged in one single dll (Managed C# class
library): <b><i>txtbxlib.dll</i></b></p>

<p>Please note that references to System.Drawing,
System.Windows.Forms was added to the solution.</p>

<p>&nbsp;</p>

<p><img border=0 width=256 height=220
src="./ctxtbx.ReadMe_files/image009.jpg"></span></p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>At the top of the package is <b><i>txtbxlib
</i></b>namespace. <b><i>ctxtbx </i></b>class is the one and only one class
implemented in the dll.</p>

<p>&nbsp;</p>

<p><img border=0 width=254 height=359
src="./ctxtbx.ReadMe_files/image011.jpg"></span></p>

<p>&nbsp;</p>

<p>Perhaps the easiest way to explain
the code is to trace the sequence of events which is fired when user click the
textbox. So, here it is:</p>

<p>&nbsp;</p>

<p>1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
User enter into textbox.</p>

<p>&nbsp;</p>

<p>2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b><i>OnKeyDown
</i></b>is triggered.</p>

<p>&nbsp;</p>

<p>protectedoverridevoid OnKeyDown(KeyEventArgs e)</p>

<p>����������������������� {</p>

<p>����������������������������������� CheckLength();</p>

<p>����������������������������������� base.OnKeyDown(e);</p>

<p>}</p>

<p>&nbsp;</p>

<p>The method will invoke CheckLength</p>

<p>&nbsp;</p>

<p>3.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b><i>CheckLength
</i></b>is triggered.
The method checks the length of the character string in ctxtbx everytime user
enter into the textbox. If length of character string <b><i>ctxtbx.Length </i></b>exceeds
<b><i>ctxtbx.FlashLength</i></b>, event <b><i>OutOfRangeEvent </i></b>is fired.
If not, nothing happens. </p>

<p>&nbsp;</p>

<p>publicdelegateint OutOfRangeDelegate(int
num);</p>

<p>publicevent
OutOfRangeDelegate OutOfRangeEvent;</p>

<p>&nbsp;</p>

<p>Now, the association between
OutOfRangeEvent and its handlers can be found in the constructors:</p>

<p>&nbsp;</p>

<p>OutOfRangeDelegate
d1 = new OutOfRangeDelegate(this.OnOutOfRange_1);</p>

<p>OutOfRangeDelegate
d2 = new OutOfRangeDelegate(this.OnOutOfRange_2);</p>

<p>����������������������������������������������� </p>

<p>this.OutOfRangeEvent
+= d1; </p>

<p>this.OutOfRangeEvent += d2;</p>

<p>&nbsp;</p>

<p>The above code snippets maps handler <b><i>OnOutOfRange_1
</i></b>and <b><i>OnOutOfRange_2 </i></b>to <b><i>OutofRangeEvent </i></b>through
delegate <b><i>OutOfRangeDelegate</i></b>.</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>4.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Event
handlers invoked.</p>

<p>&nbsp;</p>

<p>4-a. <b><i>OnOutOfRange_1</i></b></p>

<p>This method spins up a thread to execute FlashIt(..) � which
gives you the flashing.</p>

<p>&nbsp;</p>

<p>4-b<b><i>
OnOutOfRange_2</i></b></p>

<p>This method changed the border style from <b>fixed-single </b>to
<b>fixed-3D</b>. </p>

<p>&nbsp;</p>

<p>5.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Let�s
say the user realize that she made a mistake and truncate the character strings
to less than <b><i>ctxtbx.FlashLength</i></b>. Once again,<b><i> OnKeyDown </i></b>is
triggered. </p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>6.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CheckLength
is triggered in turn. But, this time, since the number of characters in the textbox
<b><i>ctxtbx.Length </i></b>is less than <b><i>ctxtbx.FlashLength</i></b>, <b><i>OutOfRangeEvent
</i></b>is NOT fired and the thread that executes the flashing (m_FlashThread)
is aborted.</p>

<p>&nbsp;</p>

<p>That�s
pretty much it. </p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p><b>IV.&nbsp;&nbsp;&nbsp;
</b><b>Project Package: </b>ctxtbx.zip</p>

<p>&nbsp;</p>

<p><b>1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b><b>ctxtbx
Control</b></p>

<p>Project
Type: C# class library</p>

<p>Project
Name: txtbxlib</p>

<p>Binary:
txtbxlib.dll (Release folder)</p>

<p>Key file:
txtbxlib.snk</p>

<p>&nbsp;</p>

<p><b>2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b><b>SomeClient</b></p>

<p>Project
Type: C# Windows Application</p>

<p>Binary:
SomeClient.exe (You need to install control assembly to GAC first!)</p>

</div>

</body>

</html>

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
Hong Kong Hong Kong
Me too, I'm a developer.

Comments and Discussions