开发者

Error message in MFC/RC on VS2008 - "Add/Remove operation is impossible, because the code element 'Cxxxx' is read only"

I have searched online and get inconsistent answers that don't fix the problem for me. I created a dialog box and then clicked ont he wizard to create a class for me. Unfortunately it named the class and file badly badly so I renamed the file. Now visual studio won't let me do anything through the gui to the dlg resource - like hook up a button even handler, etc. I get the message box:

"Add/Remove operation is impossible, because the code element 'Cxxxx' is read only"

This is a real problem because adding handlers and things manually through the code is tedious and one of the developers just isn't able to do it that way - he needs the GUI to ma开发者_C百科nipulate the events.

I have tried deleting the ncb file and rebuilding the project, but no luck at all. MS apparently has not fixed this problem after 4 years or so (based on the searches I have seen online.

I can't add variables, event handlers or anything else useful through the MFC app wizard gui. I can only do so by editing the cpp and h files.

Does anyone have any suggestions?


I added existing files in a VS2008 project and had the problem too. I fixed it by deleting the .suo file and recompiling the solution.

Maybe it helps somebody who comes across this problem.


I have managed (accidentally) to reproduce the same problem. I have defined DECLARE_EVENTSINK_MAP() and full

BEGIN_EVENTSINK_MAP(CDlgMessage, CDialog)
ON_EVENT(CDlgMessage, IDC_GM_VIEW1, 1, CDlgMessage::GMEventGmView1, VTS_I2 VTS_BSTR VTS_BSTR)
END_EVENTSINK_MAP()

and than I have deleted manually whole BEGIN-END section, but I have left DECLARE undeleted. When I later on tried to make add event handler by GUI I have received mentioned response. Simple deletion of the DECLARED part have solved the problem.

I believe that this can be copied to all similar problems.

Srdjan


I know this might be coming a bit late, but have you renamed the class itself too? If so, you may have forgotten to rename the

DECLARE_DYNAMIC(RandomClass, CDialog/alternative)  

and

IMPLEMENT_DYNAMIC(RandomClass)

The documentation for these calls is not very thorough, but when I tried to create a class to which I could add an event I got a similar error when trying to add an event to that class:

http://msdn.microsoft.com/en-us/library/ywz9k63y%28v=vs.90%29.aspx


How I fixed this (deleting .ncb/.suo/.user files and rebuilds did not help)...

I noticed the following peculiarity when this error was happening in my VS 2008 project:

  1. It happened in one dialog+class, not in the others.
  2. In the .cpp class file for the broken dialog, the scope selector had only "(Global Scope)" item, no other lines (expected class was not present)
  3. I also checked the .h class file, and scope selector there was ok.

I fixed this problem in #2 first (explained below), then exited VS, deleted *.ncb, *.suo files, restarted VS, rebuilt the project and the wizards started working again.

To fix the scope selector problem in #2 I first narrowed it down to the headers not included into the class's .h file, i.e. I had some types used inside the class declaration, but no header in the .h file that declared these types. So if I were to include the class's .h file into a clean .cpp file, it won't compile. The rest of the project was compiling OK, because all needed files were added to all corresponding .cpp files before the class's .h. After I added all necessary headers to the top of class's .h (so clean .cpp file would compile), the scope selector in .cpp file filled up correctly and in turn it fixed the wizard.

On a sidenote: there is a school of thought to not include any other .h files into any of .h files, and Microsoft seems to stick to this way quite frequently (luckily not always). I typically follow different inclusion policy - to always include headers with all used declarations into .h file, so when I need to use a module, I have to include only its .h file. In the rare occasion that I had when this did not happen, VS wizard chocked (or IntelliSense chocked that wizard relies on). It makes my comittment to this inclusion policy stronger. The only exclusion to this policy is not putting any stdafx.h, windows and MFC files into module's .h file - it is done differently for the reason of precompiled headers.


Just an FYI that I encountered the same issue in Visual Studio 2010 SP1.

Deleting the SDF file (the Visual Studio 2010 equivalent of the NCB of prior versions) fixed the problem.

(I had tried many other things first, including cleaning the project and rebuilding, deleting the .suo file, etc., to no avail. However whether those actions were needed in ADDITION to finally deleting the SDF file, I do not know.)


Deleting the solution .ncb file and rebuild worked for me too in Visual studio 2008. I should mention I started facing this problem after migrating my code to a new PC.


I had the same problem with VS2005. I tried deleting *.ncb and *.suo but to no avail.

In the end, I just need to close the file that is being complaint (because it's being opened, VS can't automatically add new code to the file when we perform some actions on the GUI resources such as adding an Event Handler to a Menu Item).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜