Doesn't contain a definition for XXX error - c#

If I accidentally double-click a button on a Winform in the visual studio designer an event handler is added. Deleting the event handler causes the compiler to throw an error indicating that the class definition does not contain the event handler. How do I fix this issue?

You need to delete the reference to the event in the designer code created by visual studio. I don't recommend going into the code itself, since you can mess stuff up.
Select the button in the designer, and open the properties toolbar. Go to the events tab in the properties toolbar. Find the "OnClicked" or "Clicked" event and remove the method associated with it. Now you can delete that event in your code and it shouldn't complain.

Select the button on which you double-clicked in the forms designer and hit the f4 key to bring up the properties window. Then ensure you click on the events filter at the top of the properties window and clear the event handler property for the appropriate event.

Related

How to keep WinForms designer from generating Click handlers?

I'm working on a big legacy WinForms app, and there's a form with a toolbar and all the buttons in the designer have names like ts* and code in the code behind designer file like this:
this.tsPlanner.Name = "tsPlanner";
this.tsPlanner.Size = new System.Drawing.Size(83, 38);
this.tsPlanner.Text = "Planner";
this.tsPlanner.Click += new System.EventHandler(this.tsPlanner_Click);
I'm trying to clean up this code so that instead of each button having its own Click event, there's a central dispatcher that handles all the buttons.
So I delete the Click handlers and the event hookup for them, but every time the designer file is regenerated, it adds back this line, and since the handler event is now removed, this causes compilation errors.
I can't find anywhere in the designer that tells Visual Studio that I want automatic event hookup. How can I disable this behavior?
Designer code is just c# partial class. Open Form.Designer.cs, delete event subscription, delete event handler funcion. Save all. Recompile all. Close all windows. Try to reopen designer.
It seems that you open in designer one form and edit code for another one.

KeyPress method does not work due to no reference

There are several KeyPress method in my code. All are working except the one due to no reference. I have checked the project for naming mistake. But there is no any mistake. How can I make reference for this method.
btnback_KeyPress is not working.
Looks like you just don't have the event hooked up. Go into the designer, select the btnback button, then in the Events tab of the Properties window, find the KeyPress event and subscribe the btnback_KeyPress method to it as a handler.
You need to wire the event to the button.
You can do so by selecting your button in the [Design] view and then selecting the Events tab (circled in red) from the properties view.
At KeyPress, enter your event name.
It looks like you're missing a line of code in the Form1.Designer (supposedly it's Form1). Look it up and add
this.btnback.KeyPress += new System.EventHandler(this.btnback_KeyPress);

Default visual studio always create event handlers in the same file - Form1.cs, how to change location?

I'm just wondering if anyone had annoying default automatic event handler function creating. For example, I'm creating button event automatically, by double click on event tab (lightning tab) for double click event - visual studio automatically generates event handler in Form1.cs file. But if I want to move this event handler to another file (but in the same object and same namespace), let's say called ButtonEvents.cs (partial Form1 class), visual studio automatically generates another empty event handler in Form1.cs file. Visual studio doesn't understand, that event handler already exists, but when compiling, it send error, so every time I have to delete these default event handlers - this is very annoying....
Thank You Guys.
You can set every method you want to handling events of your control. for example:
Button1.Click += new EventHandler(AnotherClass.GreetingBtn_Click);
If you want when you double click on your button editor shows AnotherClass.GreetingBtn_Click method, try to change event handler of your button in YourForm.Designer file (I'm not sure about it).

Event handlers in Visual Studio

When I try to create an event handler eg. button click in c#, if I write the code manually in code behind class, the event handler won't be called; Whereas if I double click the button on the form and the VS auto generates the event, it will work perfectly. What is the reason behind this behavior?
There is a Form.Designer.cs file that hooks up the events for each individual form (each form has it's own designer file).
Open that and you'll find lines like:
button1.Click += button1_Click;
..etc.
As below:
Your file isnt able to know that the event handler is to be linked to the function you have created.
If you want to link manually, click on the button once, and in the top section of the properties window, you'll see a lightning symbol(events). Click on that, and scroll down to the click event, and over there, add your function.

Specify Double-Click event for a Control in Visual Studio Designer

When you Double-Click on a Control in the Visual Studio Designer, you automatically subscribe to some event and an Event Handler is generated in the code behind file.
Double-Clicking on the following Controls subscribes to the corresponding event
UserControl - Loaded
Button - Click
TextBox - TextChanged
Grid - No event
etc.
How is this specified, is it a Visual Studio setting? Can this be overrideen and how can you specify which event you want to link to Double-Click for e.g. a Custom Control?
Thanks
There is a DefaultEventAttribute that controls can specify. The designer knows to read this attribute and uses it to determine which event to use as the default.
[DefaultEvent("DoubleClick")]
public class MyClass {
public event EventHandler DoubleClick;
}
There is also a DefaultPropertyAttribute which is significantly less useful. It just determines the default property name to select in the property grid when the control is selected in the designer.
If wanting to do this in the editor.
For VS2017 (and beyond I suspect). In Designer:
Select the control on the form
In the Properties Window, select the Events icon
The list of available events for that control will be shown
Double-click the name of the desired event.
This also works for deleting the auto-generated code. Remove the name of the event handler for that event. It will get removed from the *.designer.cs file. Note that you still need to remove the handler code in the form source.

Categories

Resources