Moving Events for Organization

G

Guest

I have a Form which has a great number of events. I thought I would organize
the project by moving some of the code into multiple '.cs' files (since the
Form.cs was getting huge). When I move them, it seems that Visual Studio can
no longer find them when I open a Button object (for example) and double
click on the previously created event. On top of this, Visual Studio creates
a new event that is blank. I know the program can 'see' my moved event as the
program still works fine.

I am just trying to organize a huge project. Is there a way to have Visual
Studio 'rescan' or something so this doesnt happen or is it always
recommended that you just keep the events where Visual Studio puts them?
 
P

Peter Duniho

[...]
I am just trying to organize a huge project. Is there a way to have
Visual
Studio 'rescan' or something so this doesnt happen or is it always
recommended that you just keep the events where Visual Studio puts them?

How did you move them?

There's no problem moving pieces of code from one source code file to
another. But you have to make sure you correctly keep those pieces in
their respective places. If they belong in a particular class (and surely
they do :) ), they need to still be in that class in the new source code
file. You can use the "partial" keyword with the class declaration to
allow the class to be declared multiple places, combining the elements
from each place.

And of course, don't forget to put everything in the same namespace too.

This all assumes VS 2005. I gather from my discussions with other people
that the "partial" keyword is new, and that in VS 2003 you had to keep
everything in the same file. The only way to break things apart in that
situation would be to put them in an actual different class, and then use
that class from the main class.

Pete
 
G

Guest

Peter Duniho said:
[...]
I am just trying to organize a huge project. Is there a way to have
Visual
Studio 'rescan' or something so this doesnt happen or is it always
recommended that you just keep the events where Visual Studio puts them?

How did you move them?

There's no problem moving pieces of code from one source code file to
another. But you have to make sure you correctly keep those pieces in
their respective places. If they belong in a particular class (and surely
they do :) ), they need to still be in that class in the new source code
file. You can use the "partial" keyword with the class declaration to
allow the class to be declared multiple places, combining the elements
from each place.

And of course, don't forget to put everything in the same namespace too.

This all assumes VS 2005. I gather from my discussions with other people
that the "partial" keyword is new, and that in VS 2003 you had to keep
everything in the same file. The only way to break things apart in that
situation would be to put them in an actual different class, and then use
that class from the main class.

Pete


Thanks for responding Pete!
I am using VS2005 Express Edition... as for moving them, all i did was just
a cut and paste into a new codefile... and I placed them into a 'public
partial form' same as the one I moved it from... and under the same
namespace. The events work, so i know that on some level it knows where they
are... Also, when I use the combobox in the upper right hand corner of the
development environement, and I select the event that way, it has no porblem
taking me right to it.

It is only when I am going to the event from the graphical Form [Design]
page that it puts me in the wrong place (I assume it is sending me to the
place where it used to be before I moved it). The real annoying thing though
is that sometimes it automatically creates the event as if it hadn't been
created (a blank prototype)... and this of course forces me to select the
blank one and delete it for everything to build fine.

Is there another way to move them that you use? (because it sounds like I
shouldn't be having this issue)
 
P

Peter Duniho

Thanks for responding Pete!

Well, you're welcome. I wish I had something more useful to offer (as in,
an actual solution to your question :) ).
I am using VS2005 Express Edition... as for moving them, all i did was
just a cut and paste into a new codefile... and I placed them into a
'public
partial form' same as the one I moved it from... and under the same
namespace.

Sounds right to me.
[...]
Is there another way to move them that you use? (because it sounds like I
shouldn't be having this issue)

I agree you shouldn't be having the issue. As far as I know (knew?) the
VS designer doesn't cache the information about where the event handlers
are. Seems like it'd just use the same "references" database that the
other parts of VS use (used to be called the "browser database", but I'm
not sure what VS is calling it now). And that should get updated when you
build the project.

I assume you've tried doing a complete, clean build. But if you haven't,
you should definitely try that. I'm afraid that beyond that, I don't have
any useful advice. I don't know enough about the inner workings of VS to
be able to explain why it can't track your movements of the event
handlers. Seems like it ought to be able to do that just fine.

Pete
 
P

Peter Duniho

[...]
It is only when I am going to the event from the graphical Form [Design]
page that it puts me in the wrong place (I assume it is sending me to the
place where it used to be before I moved it). The real annoying thing
though is that sometimes it automatically creates the event as if it
hadn't
been created (a blank prototype)... and this of course forces me to
select
the blank one and delete it for everything to build fine.

Is there another way to move them that you use? (because it sounds like I
shouldn't be having this issue)

For what it's worth, I went and tried what you did, and ran into the same
problem. It's trivial to reproduce. I was unable to find any magic file
I could delete and get things to work. I did find that when I deleted the
hidden "cache" file that sits in the directory with the main solution
file, that VS forgot the offset into the original Form1.cs file where the
event handler was (before I did that, double-clicking on the event took me
to where the handler used to be).

So I suspect that the cache file is the correct magic file, but how to get
it to point to the right location, I don't know. It may be that deleting
all the event handlers and hooking them back up is the only way. :(

On top of all that, I found that when I added a new "Code File" where I
moved the event handler to, double-clicking the code file in the Solution
Explorer took me to a bogus form designer window. I say "bogus", because
it wasn't even my actual form...it was missing all the controls. And as
if that weren't bad enough, when I double-clicked on the form, it added a
new Load event handler in my new CodeFile1.cs file, _and created a whole
new "InitializeComponent()" method_! Duh.

It seems to me that VS has some serious bugs in dealing with the designer
versus "partial". Given that it does somehow manage to avoid getting
confused between Form1.cs and Form1.Designer.cs, I suspect they've
hard-coded some special cases or something, but the more general situation
trips it up. Boo!

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top