C++/CLI, Winforms: New form under sub-folder in VS2008
When I create a new form under a sub-folder in VS2008 in a C++/CLI Winforms project, the .h and .cpp are correctly created under the sub-folder. However, when the form is opened in the designer and the title is changed, I get an error message saying that the .resx (AWOL till this particular moment) is already part of the project. One can then locate the .resx in the root folder of the solution. How do I get around this? (...without having to file a bug with MS who aren't gonna take this too seriously since this is the VS2010 season)
One option would be to create the form in the project's root folder and then move all three files (.cpp, .h and .resx) to the desired sub-folder once all change开发者_如何学Cs have been made. In this case, I find that "View Designer" is no longer available once a form has been moved to a sub-folder.
Save me. Please. I have done nothing to deserve this.
This fits my doctor's usual advice: if it hurts, don't do it. The C++ IDE isn't smart enough to create the .resx in the correct folder. It is fixable though. When you get the error message box, switch to Explorer and move the just created .resx file from the project directory to the subdirectory yourself. Switch back and hit OK, everything appears to work correctly after that.
Hans answer is right on, but didn't solve my problem entirely.
I want my source (.cpp, .h, even the .resx) files to be in a subdirectory ("SourceCode"), and not mixed in with all the mess in the project folder. VS makes it hard to move the Form (Form1) out of the project directory -- you can move the .h and .resx files (in Windows Explorer) and add them (in Soln Explorer) as existing items, but then VS doesn't recognize them as a Form. I could not find a way to tell VS that the form.h file was part of a Form item (is there a way?)
I found a work around: right click on Header Files (in Solution Explorer)->Add->New Item->Windows Form. Browse to the source code subdirectory and name the form (myForm). Then, copy the source code from the original form1.h file and paste it over the source in the new myForm.h. Do the same for form1.cpp / myForm.cpp file. Also, do a global search and replace from the old form name (Form1) to the new one (myForm) in all project files (if you changed the form name). At this point you can remove the original form1.h and form1.cpp in Solution Explorer (but don't delete the files until you are sure all is well with the new versions).
This seemed to work, but at first I didn't see a myForm.resx file under myForm.h in solution explorer. After rearranging the controls in the new form (myForm) I got the ".resx is already part of the project" message. The message box just wouldn't take OK for an answer. I saw myForm.resx file in the subfolder(?), but I noticed that there was also one in the project folder. I think I deleted the one in the project folder. There were then two myForm.resx files in Soln Explorer (both .resx files had the same name). Somehow I determined that the first one corresponded to the project folder, and I removed it. Now all is working fine.
BTW, I'm working with VS2010. I expect that my work around will also apply to VS2008. I used a similar trick with VS2003 when a form would crash (which was often).
精彩评论