Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

.NET Building Blocks: Custom User Controls in Practice  : Page 4

Learn the differences between the various types of custom controls, and find out how to integrate your controls into Visual Studio and make them easy to use.


advertisement
Implementation Details: The Raw Control
The raw control provides the most flexibility to handle custom rendering or custom functionality, but also requires the most work to implement, deriving from the base class of all controls, the Control. (Remember, MSDN documentation calls this type of custom control a custom control). Table 4 shows Raw Control usage.
Table 4. Raw Control Usage
Raw Control Manifestation Description
Add User Control Dialog Use the Custom Control template.
Generated Code public partial class CustomControl1 :
Control
{
   public CustomControl1()
  {
     InitializeComponent();
  }
  protected override void
     OnPaint(PaintEventArgs pe)
  {
     base.OnPaint(pe);
  }
}
The OnPaint event allows you complete flexibility over how the control will render.
Designer Canvas The canvas is available but does not provide a drawing surface. You are limited to setting properties for components you add.
Runnable in test container? No  

Implementation Details: The Abstract Parent Control
 
Figure 3. An Abstract Parent: Here, UserControl2 inherits from an abstract control, UserControl1, which prohibits the visual designer from showing UserControl2 in design view.
There is one more control variation that bears mention, though it is not really a separate type. This type of control is an inherited user control that inherits from an abstract parent. The class diagram in Figure 3 shows an example. In the diagram, UserControl2 inherits from the abstract UserControl1, which inherits from a concrete control. That base concrete control may be either a user control or a standard .NET control.

To create this abstract parent control, you begin the same way as you would a composite control, with the UserControl template. Then, like an inherited system control, change the parent class in code to your abstract class. If you then attempt to open the control (UserControl2) in the designer, Visual Studio responds with an error message similar to Figure 4. Interestingly, you can open UserControl1 in the designer just fine! Both of these simple controls are available in the downloadable ControlLib sample project.

 
Figure 4. Abstract Problems: Attempting to open the designer for a control that inherits from an abstract control produces this error.
While VS2008 has made this error report much more stylish than in VS2005, if you click on the suggested link to the MSDN forums you'll end up in the Windows Form Designer forum—not in any specific thread. Navigating to the MSDN help link shown, on the other hand, takes you here which provides one sentence that doesn't help much: "This error occurred because the base class of the object being passed to the designer is abstract, which is not allowed."

For controls such as this, you'll need to work with the control strictly in code view. One workaround for this issue involves instrumenting your code for conditional compilation, and making the class conditionally concrete. Perhaps a more straightforward workaround is to design the control as a composite control instead of an inherited control. Your composite control could instantiate a concrete version of the abstract control as one of its constituent controls, avoiding the issue.





Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap