Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 manipulate 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?

like image 658
Tim Avatar asked Feb 15 '11 03:02

Tim


5 Answers

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.

like image 94
marscode Avatar answered Nov 12 '22 10:11

marscode


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

like image 2
Srdjan Jovanovic Avatar answered Nov 12 '22 10:11

Srdjan Jovanovic


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

like image 1
Underdetermined Avatar answered Nov 12 '22 09:11

Underdetermined


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.

like image 1
iva2k Avatar answered Nov 12 '22 09:11

iva2k


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.)

like image 1
TechnoFrolics Avatar answered Nov 12 '22 09:11

TechnoFrolics