Steve from adzac

Simple case: form named frmMain is coded in frmMain.cs. If I use the IDE to place a control on the form and then double click the control, the IDE creates an event handler stub in frmMain.cs.

I'd really like to move the auto-generated event handlers when frmMain grows large enough to become undwieldy. However, I now lose the ability to double click the control to easily find the control's event handler.

Is there a way to let the IDE know I've moved handlers It seems like a refactoring issue, but I didn't find any clue there.

If this is not possible, what's the best practice for overly large form code files Moving the non auto-generated stuff is only a partial answer. The use of TabPages can make a form's code file huge even with only the event handlers.

Steve



Re: Visual C# IDE moving auto generated event handlers?

Steve from adzac

Steve from adzac wrote:

If this is not possible, what's the best practice for overly large form code files Moving the non auto-generated stuff is only a partial answer. The use of TabPages can make a form's code file huge even with only the event handlers.

Steve

Well, other than...

private void someControl_Click(object sender, EventArgs e)

{

handleSomeControl_Click(object sender, EventArgs e); // which is in some other file

}

Steve





Re: Visual C# IDE moving auto generated event handlers?

Adam Bretz

You could make the frmMain.cs a partial class. Implement all the logic in one part and all the events in another. I don't know how the IDE will react to that, but it will at least keep the file shorter. Hope this helps.





Re: Visual C# IDE moving auto generated event handlers?

Steve from adzac

Adam Bretz wrote:

You could make the frmMain.cs a partial class. Implement all the logic in one part and all the events in another. I don't know how the IDE will react to that, but it will at least keep the file shorter. Hope this helps.

Thanks, that's what I'm doing now. frmMain is already a partial class when the IDE is used to add a new form so that part is already taken care of for me. It's just that even with event handlers alone, some form files get quite large.

Steve





Re: Visual C# IDE moving auto generated event handlers?

Adam Bretz

Perhaps I'm just not understanding you fully. It's already a partial class because the IDE seperates the autogenerated stuff out an leaves you just to code up the logic. So that's two files. I'm saying make a third file that's also a partial class of frmMain. If you've got 3 files all implementing the frmMain class, then I'm out of ideas!





Re: Visual C# IDE moving auto generated event handlers?

Steve from adzac

Adam Bretz wrote:
Perhaps I'm just not understanding you fully. It's already a partial class because the IDE seperates the autogenerated stuff out an leaves you just to code up the logic. So that's two files. I'm saying make a third file that's also a partial class of frmMain. If you've got 3 files all implementing the frmMain class, then I'm out of ideas!

You understand correctly. I'd like the flexibility of moving the autogen'd handlers into the third file. If I do so, then the ide can not always find them. Builds are still successful -- it's only the convenience of finding the handler by double-clicking the control in the designer that is lost.

Steve





Re: Visual C# IDE moving auto generated event handlers?

Ji Cheng Wang - MSFT

Hi Steve,

Based on my understanding for your issue, I think Adam's suggestion should help you reduce some behind codes of the FrmMain.cs. That is to say to move the business logic and event handlers of the FrmMain form to two seperate files,and then call them in the behind codes of the FrmMain.cs. You can double click the control in the FrmMain to get its corresponding event handler. If you think you still get a large file with FrmMain.cs file, you can try to consider to dispatch your FrmMain form into some different small forms and call them through the FrmMain form. Is it suitable to your case

Hope this helps,

Regards,






Re: Visual C# IDE moving auto generated event handlers?

Sam Pearson

Steve,

Other than your handler-calls-handler-in-another-file idea (not to mention that double-clicking the control would only take you to a handler call, not any handling code), there is no way to do this. I suggest using regions to compartmentalize your code.





Re: Visual C# IDE moving auto generated event handlers?

Steve from adzac

Sam Pearson wrote:

...there is no way to do this...

Yeah, that's too bad. Thanks for the answer.

Frequent use of the outlining compress to definitions option helps. Regions...I may try creating additional regions. I will have to see how the region settings are retained over time and over source control checkin/checkout with a couple developers.

Ji Cheng Wang wrote:

...into some different small forms ...

That's how I've always done it in the past. This time I decided to try out tabbed pages. My few users do like the tabs better than (in their words) "constantly opening and closing forms". Four or five tabs of related options for an external custom device do result in a lot of event handlers, which translates to a very large file of handlers even after moving out all other code and reducing the handlers to nothing more than a call to a secondary handler.

I'll just have to deal with it, but it would sure be good to have the ability to divide those handlers amongst partial form classes and still keep all the IDE features.

Steve