开发者

Different class for different tab in c#

I wrote some code and then decided that i needed another tab to give the user some more options that way. My problem is that any buttons or textboxes i put in the second tab must be coded in my main class th开发者_开发知识库at starts with the program. Is there a way to associate a different classs with the second tab so i can keep each tab's methods separately in different classes?

Please let me know if this can be done

Thanks


A few options:

  1. Create a UserControl for the tab (has some downsides in WinForms, as UserControls can cause poor performance if nested too deeply.) This fully isolates both the code and contained controls.

  2. Implement the UI controller logic in a separate class and only write very shallow glue code in the Form code-behind. You wind up with a bunch of ugly glue, but it's cleaner than having more complex UI logic bloating the Form.

  3. You can create additional partial classes (in separate files) for the Form code-behind with the event handlers for your new tab's controls. This can get a bit ugly, though and isn't very nicely supported by the WinForms designer.

  4. You can create code regions in the Form code-behind to group together the logic behind each tab. This organizes the complexity slightly, but it can actually be a bit misleading (the code looks short when collapsed, but is actually very much a 'God class' and there is no guarantee that tight coupling between the regions hasn't been introduced into the code, despite the organization. It also requires some discipline to maintain the region grouping; developers can accidentally add methods to the wrong locations, creating confusion later.


You could create your own control derived from TabPage/TabItem. This will allow you to have separate class for your tab.

Or you could create "partial" class and you can split one class into multiple files.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜