开发者

Multiple classes in one form

开发者 https://www.devze.com 2023-02-16 02:07 出处:网络
I\'m using a TabControl in my form and it made me wonder. Right now I have only two tabs and I store procedures relating to both tabs (button handlers, &c.) in the code for the main form, so it lo

I'm using a TabControl in my form and it made me wonder. Right now I have only two tabs and I store procedures relating to both tabs (button handlers, &c.) in the code for the main form, so it looks like this:

public partial class MainForm : Form
{
    // ----------TAB1-----------
    tab1Search开发者_C百科ButtonCLick() {...}
    tab1AddButtonCLick() {...}
    // ----------TAB2-----------
    tab2EditButtonCLick() {...}
    tab2SearchButtonCLick() {...}
    tab2ClearButtonCLick() {...}
}

That's not a problem now with so little code, but it might be one in the future. Is this an acceptable way of doing this? What's the alternative? I believe I could put the tabs in their own classes, but I'm not sure how I'm going to do that exactly (there's lots of controls in each tab that I'd have to pass as arguments to constructors).


You should move the contents of each tab to a separate UserControl.

Each UserControl should be a self-contained unit that gets whatever data in needs from the main form and fires events to tell the main form to do things.


The tabs have event for a reason - the tab raises the event and doesn't want to handle it.
When you use the tabs (or other objects with events) you need to write the code that handles the event.

If you really want, you can write the class in separate files so you can keep your own logic in separate files.

If you'll notice a Form is generated as a partial class by Visual studio.
That's because the design code is generated in a separate file.
You can do that yourself by declaring other parts of the class as partial in other files.

More about partial in this link

0

精彩评论

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