How to fix "Cannot add event handler because code element marked as read-only" in VS2005 and VS2008

Today VS2008 SP1 pop up an error dialog when I was editing MFC dialog box in VS2008 SP1. The error was saying something like "Cannot add event handler because code element CMainDlg marked as read-only". Clearly neither .cpp nor .h file for this class was not marked as read-only. Actually none of files in directory were marked as read-only. I have tried fixing the proble with several tricks, but what finally worked for me is the following:

  1. Save all files and close solution
  2. Delete <your project name>.suo file
  3. Delete <your project name>.<username>.user file
  4. Delete <your project name>.ncb
  5. Delete <your project name>.aps
  6. Load solution in Visual Studio again and re-try the action.

Exactly these steps fixed the problem for me. Now I believe it may be sufficient to only do steps#1 to step #5 to step#6. .aps file is most likely the root cause of the problem here. If anyone tries deleting .aps file only and it fixes the problem, please let me know in comments.

Comments