Visual Studio 2013 and C# - Unable to add event handler - c#

I am working on WPF project, and after I made everything in Xaml file I wanted to start doing on my CS file.
Now, I was trying to add event using that little bolt in properties, but everytime i click on any event (for example I select button in XAML and try to add -> MyButton_Click) i get a pop-up message "UNABLE TO ADD EVENT HANDLER".
I hope you are able to help me.

yes, #will is right, I also suffered from the same problem,when i was new in WPF. Please check your x:Class in your XAML file, it should include the namespace used in code behind.
e.g.,
x:Class="MainWindow"
should be some thing like
x:class="namespace.MainWindow"
if it doesn't work,try to restart the VS or try to recreate the project with correct name, hope it will solve your problem.

I suffered from the same problem like you. Error occurs when you have any syntax error in your XAML file like misspell, missing tag, comma, lack of method/event handler or difficulties with references.
Exactly the same reason is when you are unable to add event or change any property in Property Window, but without any message or error. Check your syntax carefully.
Regards.

yes. I also face this problem, I fixed it. the problem is on x:Class="MainWindow". just like #Abdullah Said.

I had wrong namespace in XAML file after copying/pasting the code. Namespace in XAML and underlaying code must match.

This can also happen if a method already exists in the .cs file whose name is identical to the method name the IDE is attempting to create.

Once I added a name to the button, I was able to double click it the error went away, opening the code view to the event handler.

Related

Deleted an important line in the designer script

I'm studying computer science and working on a project.
Within my project, I made an ABSOLUTELY stupid mistake and deleted a line of code in the designer.cs, and now I have nearly 400 errors. Unfortunately for me, it ended up saving and closing which means I can't revert any changes made, I looked for backups but can't find any.
I have no idea what that line of code was, and I don't want to redo the entire project by copying and pasting. Any help if possible is much appreciated! I hope I explained it enough
The missing line is the first one after initialize component
Edit: Errors list
You removed your system refrence, Add it back by right click References -> Add Reference
Under Assemblies , search for System
Based on errors in the screenshot, I think you simply need to add using System; to the top of the file.

How to compile Item Template Project for Form with substitute parameters

I am trying to make an ItemTemplate for a Form. I create a "c# Item Tamplate"-Project, make my Form, and create my Template by Building the project. That works fine.
Now I want to add Templateparameters but as soon as I do, the project wont compile any more. It throws many different exceptions because the code is no longer valid. How can I solve this problem?
Here is what I tried so far:
Replacing every occurrence of my Classname with $safeitemname$. This, as I already said, throws exception because now the code is invalid and the project can not be built.
Creating CustomParameters in my .vstemplate to change a valid class name to my desired parameter (Classname -> $safeitemname$). This does nothing, even though I set "Replaceparameters="True"".
Ignoring the compiler warnings and implementing the project in my .vsix project anyway. This does nothing either, because the .zip for my template still can not be.
Setting DependentUpon of my Classname.Designer.cs to my Classname.cs. This does nothing either.
Trying a simple ItemTemplate with one class that's divided in two partial classes that have one Property each. Same thing as with the Form. It can no longer be build.
As Reza Aghaei suggested in his comment, the problem was, that I didn't set the build action for the files in my project to 'None'.

How to remove a property from a custom user control

A property was added to a custom user control. Since then, Visual Studio has inserted generated code to every form that uses that user control. Now we want to remove that property because it is not going to be used. But obviously doing so causes compile errors. Is there a way to tell Visual Studio to remove the property from all that generated code? It seems to me that putting something like [Obsolete()] should be enough to tell Visual Studio to remove it from the generated code. Is there some other way?
Look at DesignerSerializationVisibilityAttribute, especially the DesignerSerializationVisibility.Hidden option.
Caveat: Unfortunately you will have to open every form/control in design mode so the code can be regenerated (if it does not do so already).
Regex to the rescue! What I usually do is open Replace in Files dialog (Ctrl+Shift+H), check Use Regular Expressions checkbox and replace instances of .+\.SomeProperty.+ with empty string.
Be sure to commit your changes to repository before this, so you can revert to working state if anything goes wrong.
You can try to use some refactoring tool, like Resharper.
http://www.jetbrains.com/resharper/
Adding the "Obsolete" atribute will only generate a compiler warning, but wont remove any code.
Just leave it and mark it as Obsolete.

Renaming custom control in VS 2010, receiving err: "The name 'InitializeComponent' does not exist in the current context"

I'm trying to create a customized Windows Forms control by following these MSDN instructions:
http://msdn.microsoft.com/en-us/library/7h62478z.aspx
(Article title: How to: Inherit from Existing Windows Forms Controls; the instructions are for Visual Studio 2010)
These instructions do seem to work, but if I change the name of the control from CustomControl1 to any other name (by changing the class name and constructor name), I receive the message:
"The name 'InitializeComponent' does not exist in the current context"
I've looked at the similar questions and answers here, and I don't think any of them answer this, but I could be wrong. If I am, then I didn't understand the answer.
I hope someone can help me on this. I've wasted a lot of time on it already.
Thanks.
A few minutes after posting this question, I figured out the answer. I imagine that for someone with even moderate experience with Visual Studio, the answer is obvious, but it wasn't for me, so maybe I can save somebody some time if they happen to read this.
To rename a newly created custom control which has been created by following the MSDN instructions in the link provided in the question:
Rename the generated source file from CustomControl1.cs (for C#) to the desired name
Say yes when Visual Studio asks you if you want to change all the references
That's it. The custom control name is changed, and no more error.
When changing the class name you'll notice that a red line appears under the end of the new class name.
Hover over it until a little box appears, then click on that box, and then click on "Rename...". (A shortcut to it is: Ctrl+. <-that's the period key.)
That's all.
The reason for the error is that the InitializeComponent method is in a separate file with the old class name. By choosing the "Rename..." as mentioned above - Visual Studio will rename the class in that file as well. It also changes the constructor's name.

Displaying Error Message from C# AfterInstall Event

Can someone please tell me how to display error message in C# during execution of AfterInstallEvent?
My project uses the Microsoft set-up and deployment project and then I have created a class that is called when the AfterInstall event is fired.
MessageBox.Show(); doesn't work..."The name 'MessageBox' does not appear in the current context".
If it was that simple, I wouldn't be asking!?
The real solution is to use scope. Something like this:
global::System.Windows.Forms.MessageBox.Show(ex,"Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
Cheers
This is very old but I'll answer anyway:
It's just a missing reference.
Add a reference to System.Windows.Forms to the project containing the class.
Also add "using System.Windows.Forms;" to the top of your class file.
As far as I know there shouldn't be any issues with displaying message boxes from a custom install action.

Categories