Creating Custom - Net Controls With C#
Creating Custom - Net Controls With C#
1/6
Enabled
2/6
Parent Returns a reference to the container or parent of the control. For example, the parent of a control that is added to a form is the form itself; if we add Button1 to a form, we can change the title of that form to "Thank you": Button1.Parent.Text = "Thank you" Size The size of the control, as represented by a System.Drawing.Size object. Text The string that is associated with the control. For example, in a label control, the text property is the string that appears on the label body. The Methods of the Control Class Some of the frequently used methods of the Control class are: BringToFront Shows the entire control, in cases where some other control is overlaying it. CreateGraphics Obtains the System.Drawing.Graphics object of the control, on which you can draw using the various methods of the System.Drawing.Graphics class. For instance, the following code obtains the Graphics object of a button control called Button1, and then draws a diagonal green line across the button's body:
Imports System.Drawing Dim graphics As Graphics = Button1.CreateGraphics Dim pen As Pen = New Pen(Color.Green) graphics.DrawLine(pen, 0, 0, _ Button1.Size.Width, Button1.Size.Height)
Drawing on a control this way, however, does not result in "permanent" drawings. When the control is repainted, as it is when the form containing the control is resized, the graphics will disappear. The section "The RoundButton Control" below explains how to make the user interface redraw every time the control is repainted. Focus Gives the focus to the control, making it the active control. Hide Set the control's Visible property to False, so that it is not shown. GetNextControl Returns the next control in the tab order. OnEvent Raises the Event event; possible events include Click, ControlAdded, ControlRemoved, DoubleClick,
3/6
DragDrop, DragEnter, DragLeave, DragOver, Enter, GotFocus, KeyDown, KeyPress, KeyUp, LostFocus, MouseDown, MouseEnter, MouseHover, MouseLeave, MouseMove, MouseUp, Move, Paint, Resize, and TextChanged. For example, calling the OnClick method of the control will trigger its Click
event. Show Sets the control's Visible property to True, so that the control is shown. The UserControl Class The UserControl class provides an empty control that can be used to create other controls. It is an indirect child of the Control class. The object hierarchy of this control is as follows.
System.Object System.MarshalByRefObject System.ComponentModel.Component System.Windows.Forms.Control System.Windows.Forms.ScrollableControl System.Windows.Forms.ContainerControl System.Windows.Forms.UserControl
The UserControl class inherits all of the standard positioning and mnemonic-handling code from the ContainerControl class. This code is needed in a user control.
The RoundButton Control With Control and UserControl, it is very easy to develop a custom Windows control. Your custom control class inherits the UserControl class and, because the UserControl class is also a descendent of the Control class, your custom control will also inherit all of the useful methods, properties, and events from the Control class. Event handling, for example, is automatically inherited in your custom control, thanks to the Control class. How you draw the user interface is particularly important. Whatever shape your custom control has, be aware that the control is repainted occasionally. Therefore, the user interface must be redrawn whenever your custom control is repainted. Considering that the Control class's OnPaint method is called every time the control is repainted, you can ensure that your custom control has a permanent look by overriding this method with a new OnPaint method that draws your custom control's user interface. The code in Example 1 presents a custom control called RoundButton, which is a button that is, um, round. Figure 1 shows the RoundButton custom control on a form. The code for the form is given in Example 2. Basically, all you need to do is override the OnPaint method. The system passes a PaintEventArgs object to this method, from which you can obtain the control's System.Drawing.Graphics object. You can then use its methods to draw the user interface. Listing 1: The RoundButton Control
using System.Windows.Forms; using System.Drawing; namespace MyNamespace { public class RoundButton : UserControl { public Color backgroundColor = Color.Blue; protected override void OnPaint(PaintEventArgs e) {
4/6
The code in Listing 1 is a bit of a surprise, isn't it? It's too simple to be true. Your class has only one method: OnPaint. In a nutshell, this method passes a PaintEventArgs object, from which a System.Drawing.Graphics object can be obtained. This Graphics object represents the draw area of your custom control. Draw whatever you want on this Graphics object, and it will be displayed as the user interface of your custom control. In Windows programming, you need a pen to draw a shape, and sometimes a brush. To write text, you will also need a font. The following code in the OnPaint method creates a System.Drawing.Pen object with a tip width of 4.
int penWidth = 4; Pen pen = new Pen(Color.Black, 4);
The last bit of preparation is to instantiate a SolidBrush object having the same color as the value of the
5/6
field.
Now you can start drawing. For the base, you use the Graphics class' FillEllipse method. The width and height of the circle are the same as the width and height of the control.
graphics.FillEllipse(brush, 0, 0, Width, Height);
Then, you instantiate another brush that you will use to draw text.
SolidBrush textBrush = new SolidBrush(Color.Black);
For the circle, you use the DrawEllipse method of the Graphics class.
graphics.DrawEllipse(pen, (int) penWidth/2, (int) penWidth/2, Width - penWidth, Height - penWidth);
Finally, you draw the text on the Graphics object using the DrawString method.
graphics.DrawString(Text, font, textBrush, penWidth, Height / 2 - fontHeight);
Compile your control into a .dll file and it's ready for use. The code in Example 2 presents a Windows form called MyForm that uses the RoundButton control. Example 2: Using the RoundButton control
using using using using System.Windows.Forms; System.Drawing; System; MyNamespace;
public MyForm() { RoundButton roundButton = new RoundButton(); EventHandler handler = new EventHandler(roundButton_Click); roundButton.Click += handler; roundButton.Text = "Click Here!"; roundButton.backgroundColor = System.Drawing.Color.White; roundButton.Size = new System.Drawing.Size(80, 80); roundButton.Location = new System.Drawing.Point(100, 30); this.Controls.Add(roundButton);
} public void roundButton_Click(Object source, EventArgs e) { MessageBox.Show("Thank you."); } public static void Main() { MyForm form = new MyForm(); Application.Run(form); } }
6/6
The constructor instantiates a RoundButton object, creates an EventHandler object, and assigns the handler to the Click event of the RoundButton control.
RoundButton roundButton = new RoundButton(); EventHandler handler = new EventHandler(roundButton_Click); roundButton.Click += handler;
Note that we did not define any event in the RoundButton class. Event-handling capability is inherited from the Control class. The next thing to do is to set some of the properties of the RoundButton control.
roundButton.Text = "Click Here!"; roundButton.backgroundColor = System.Drawing.Color.White; roundButton.Size = new System.Drawing.Size(80, 80); roundButton.Location = new System.Drawing.Point(100, 30);
And finally, add the control to the Controls collection of the form.
this.Controls.Add(roundButton);
The Click event, when invoked by the user clicking the control, calls the roundButton_Click event handler, which simply displays a message box:
public void roundButton_Click(Object source, EventArgs e) { MessageBox.Show("Thank you."); }
Conclusion In this article, you have been introduced to the two important classes in the System.Windows.Forms namespace that you should understand when building a custom control: Control and UserControl. You have also learned to build your own custom control by directly extending the UserControl class and how to use your custom control in a Windows form. Budi Kurniawan is an IT consultant specializing in Internet and object-oriented programming, and has taught both Microsoft and Java technologies.
Return to the .NET DevCenter.
oreillynet.com Copyright c 2000 O'Reilly & Associates, Inc.