开发者

How do I use derived controls in my Windows Forms application

At best, I am a UI novice, but I am a pretty good OO developer (if I don't say so myself). I am confused as to how to work with the designer. I am developing a Windows Forms application. I have a control (TabPage) that I derived from System.Windows.Forms.TabPage that I want to place on a System.Windows.Forms.TabControl control.

public class MyTabPage : TabPage
{ ... }

I am not sure how to get the designer to use my control.

A fellow de开发者_StackOverflow社区veloper looked at this for me as suggested that I add more information.

I've prototyped a WinForms application. The application has a TabControl, with three tab pages on it. Each TabPage has a SplitterControl with a DataGridView control in each half of the splitter. The two DataGridView controls form a master-detail view of some data we need to edit. I've added a bunch of event handlers to each DataGridView control and each time the handler code is placed in the form. The form is getting pretty crowded and would like to use OO techniques to clean things up.

I would like to redo the prototype now and because each TabPage is behaviorally identical, I would like to use OO to abstract away some of the complexity. This is where my lack of experience with the designer comes in. There must be a way to use the designer for something more complex than my origonal prototype.


You aren't really changing the behavior of the TabPage, it's just a container after all :). The TabPage and TabControl are rather tightly coupled, and they probably should be. Your "control" is what should be placed on the TabPage. So define your user control as a panel with a splitter and two datagridviews and then just drop your user control on each TabPage in your app. The Tab Control/Page tandem will still work automatically and your custom code will be in the appropriate control.


Unfortunately, there's no real practical solution to your problem. You'd have to create a custom TabControl as well and give it its own designer so that it will create instances of your derived class instead of the default TabPage class.

Sadly, the TabControlDesigner class in System.Design.dll is internal and can't be derived from. You'd have to write a complete replacement for it. That's difficult, it is a pretty advanced designer. You could have a look-see with Reflector to find out what it takes.


UserControl

Simply create a user control that contains your control. Then it will be available in the ToolBox window of Visual Studio.

For more details, Understanding the User Control Designer...

A User Control is similar to any other class, but with the added ability to be placed in the Toolbox and displayed on a form.

The tutorial differs a bit from Visual Studio 2010 but I'm sure you will get through it.


TabPage Control

As for the TabPage control, you can't use it stand-alone in the designer. It must be part of a TabControl.

The TabControl is built to contain TabPage controls. If you use the designer to add/remove any TabPage, in the back-end it does "new TabPage", it can't determine that it needs to create a new instance of your derived class of TabPage.


Other Suggestion

A TabPage is just a container, I can't figure out why you need to override the TabPage control. I know you have added some information to your question but you may have to revisit it again so we can better understand.

Since the TabPage is a container, you should create a UserControl which contains your SplitContainer and DataGridViews. Therefore you would be able to reuse that UserControl on each TabPage.

The designer is just a complement to UI programming, it is not mandatory. You can add a UserControl to a TabPage without using the designer.

I may be off the track here, if so please provide more details. A visual prototype of what you need may help.


It's been a while, but I thought I would answer this question.

I was able to get the OO implementation I wanted by splitting the control up. The control is implemented pretty much the same as the prototype. I then created a control binder class hirearchy with derived classes to contain the specific behavior(s) I needed. The control has a reference to the binder base class and calls binder functionality as needed. The derived binders override properties and methods as necessary. Pretty much a textbook OO solution.

Where I was stumbling was in thinking that I needed to have a control hirearchy.

Thanks everyone for your help. I was under some pressure when I asked this question. It's amazing how much I've learned in the months since.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜