views:

89

answers:

6

I'm writing a program in Visual Basic 2010. It's a HMI (Human-Machine Interface) and therefore has a whole whack of buttons that just send commands to other devices. As a result, there are a huge pile of event handlers for clicking buttons that can't really be broken down into modules (unless my understanding of modules is wrong).

Essentially, I'd like to be able to move all the event handlers, for, say, button presses to a different file. Can this be done or is it important that they stay in "MainWindow.xaml.vb"? (All my buttons reside in one fullscreen window, some are hidden by tabs).

Thanks,

--Erik T

+4  A: 

Sure use partial class. Check this article,

http://visualbasic.about.com/od/usingvbnet/a/partclses.htm

A_Nablsi
+3  A: 

You can use Partial Classes to split these into separate files, if required.

Reed Copsey
+10  A: 

You can use Partial Classes to break up your class definition into multiple files which might help organize your code.

The link above will help explain exactly how to use Partial Classes. The following link will show you the VB.NET Syntax (and how to use Class Designer to split things up):

How to: Split a Class into Partial Classes

Justin Niessner
I should be able to migrate a decent portion of my code into a separate file like this. Awesome!
evilspoons
FWIW, this worked, but if you move event handlers to a separate file via Partial Classes the XAML designer in Visual Studio 2010 can no longer give you a convenient link to the event handler via the Properties window (it shows up as blank and if you double-click it will create a new, blank event handler... and then compile will of course complain about multiple identical signatures.)
evilspoons
+3  A: 

I like to use regions to hide code (the code highlighter dose not do this kinda thing so ill use an image) alt text

Keep in mind that this is only hiding the code in VS the file is still compleatly intact and the compiler will just ignore the #Region and #End Region lines

giodamelio
I have been using regions, BUT they don't really help when you have a 400 foot long page that you really just want to have multiple sections of in different tabs of Visual Studio. Still a good tip though.
evilspoons
ahh, true it really depends on the person i guess.
giodamelio
+1  A: 

Could you separate some of the functionality into custom controls instead? Possibly even create the buttons on the fly based on some external data?

MarkJ
+1  A: 

For the modularity of the UI, you could explore the composition capacities of WPF/SL described here and with helpers and examples here . But it`s a really different architecture, and might be a major refactoring if you already have a lot of code.

For the buttons, you should be able to use commanding that should hide most of your event handlers. Read about commanding here.

Matthieu
I actually AM using WPF, but I'm sort of using it with a Windows Forms mindset. I mostly switched to WPF so my app would scale to different screen resolutions with minimal effort. Commanding looks fascinating, and the views thing could help me out too. Thanks!
evilspoons
@evilspoons : You`re welcome ! I agree that the transition from Windows Forms to MVVM is a big jump, but it really worth it. Try it on a small scale project, and you`ll see the benefits rapidly, if you search SO, you`ll find tons of ressources about MVVM :)
Matthieu