C#.Net comes with a variety of useful controls: buttons, textboxes, checkboxes, treeviews, forms, etc. But perhaps the most important fact is that C# allows programmers to expand upon its control base with custom controls.
Custom controls are created in the simplest way by selecting "Add New Item..." under the Project menu and selecting a new User Control.
A brand-new user control will be very similar to a Windows Form. A closer look at the class shows however that instead of inheriting from the Forms class it inherits Forms.UserControl, which is in essence just a specialized type of Windows Form.
When writing the C# custom control there are several aspects to consider, two of the most important being drawing and events.
Drawing
The user control, like a normal Windows Form, can draw itself with either a Paint event, or an override of the OnPaint function. Either way, both have the object "e" which can reference the user control's Graphics object.
"e.Graphics" allows the programmer access to all the GDI+ drawing functions that the custom control will display.
A tip when using the Graphics object is to set the SmoothingMode property to AntiAlias. AntiAlias makes edges look smoother and thus makes the control look a lot more professional. However if for any reason you need to draw out sharp-edged shapes, change the SmoothingMode back to HighSpeed.
To keep the custom control from flickering every time it refreshes, add the following line on the constructor:
this.DoubleBuffered = true;
That way the .Net Framework will take care of the double buffering to ensure smooth flicker-free drawing.
Events
The second major aspect to consider is events. As far as user interaction with the user control goes, the UserControl class inherits all the Windows Forms events one is used to, including Load, MouseClick, KeyDown, etc.
Using those built-in events makes handling user interaction with the custom control a breeze. A tip to remember is that the Load event comes after the user control's size will have been set on the parent Form. That is an important fact to remember when you are adjusting drawing surfaces. (In other words, set the sizes of drawing surfaces during the Load event).
For more advanced functionality one needs to create custom events. Custom events will allow future programmers who use your custom control to be able work with it effortlessly. For example, if the custom control is in essence a slider. An event that triggers when the value of the slider changes would make the control more professional and easier to use.
To use custom events first declare the delegate:
public delegate void MyValueChangedDelegate();
From there you can use the delegate to declare the custom event:
public event MyValueChangedDelegate MyValueChanged;
Now, to finish the custom event integration, the user control needs to call MyValueChanged() when it changes the example value internally.
Example- Photoshop-Style Controls
There are countless examples of good and bad C# user controls on the internet. I recommend you look around and find different examples to get a feel of how the controls are structured and their usability handled.
Post a Comment