Accidentally added some event methods in C# from the form builder. If you delete them from the .cs file it throws an error. How do I get rid of them?
There are two parts to subscribing to an event.
You have the event method itself, which you tried deleting.
You have subscriptions to the event method. You can have any number of controls subscribing to a single event method.
If you just delete the event method, then you still have controls subscribed to that event. But it no longer exists, so you get an error.
You can delete the subscription to the event from the designer by right-clicking the event in the properties window and clicking "Reset":
Or you could open the Designer.cs file and delete event subscription from there. For example:
this.richTextBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.richTextBox1_KeyDown);
As well as deleting them from the code file, you will need to find the control that is referencing those event methods and remove the reference to the method.
You need to go to Form.Designer.cs and remove the red line which is subscription of the event handler.If you see an error screen like this:
Just click the link under the Instances of this error, and remove that line and it should be fine.
Go to the button or element that reference the Event and remove it from there, Desingn - > Right click on element -> Properties - > Events - > Remove from there what you dont need, or do it in the code, you can search in it for the name of the method you deleted.
And the next time just use Ctrl + Z.
Related
Please help me to fix this problem in c#. Every time i accidentally click any Button,Text Box etc. it will go to the source code and when i delete the code, debug wont run, but when i put comment sign /* comment*/ the application will run.
Any one can help me on how to remove on click event.
this is what i want to remove
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{ }
Every time i delete this debug will prompt me. but when i using VB i can easily delete this no need to remove the button or txtbox etc.
Basically when you double click a control, the designer automatically create a the event handler.
It also automatically assigns the event handler to the control. It makes this change in the designer.cs file of your form. This is handeled for you.
Unfortunately, when you remove the code you pointed out, the function no longer exists. Therefore, the solutions fails to build because the reference to the function still exist in the. Designer.cs file.
You need to remove the reference in the designer.cs file:
"If you click on a control in the Form Designer, you see a list of event handlers that the designer is associating with the control in the Properties Window. Click on the lightning bolt at the top of the Window to see a listing of events. You will see method names next to events that the Form Designer is writing code for in the designer.cs file. Simply erase the name of the method for the event you wish to disconnect, hit Enter, done."
- https://social.msdn.microsoft.com/Forums/vstudio/en-US/a6f25488-b761-437f-8a65-e7e51dd4b382/remove-event-handler?forum=csharpgeneral
You would not have to do this in VB.net, as VB.net uses the handles command whereas c# performs a method like so:
button1.Click += new EventHandler(button1_Click);
Just simply remove the event from your element before deleting the method in your code:
There will be a line in form.designer.cs class just remove that.
comboBox3.SelectedIndexChanged+=new eventargs(comboBox3_SelectedIndexChanged);
Moreover, windows forms use CodDom to write a code to you have to be careful hope this will help you to learn more about creating windows form application.
https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-add-controls-to-windows-forms
I wanted to add a GotFocus event to a Windows Forms Textbox, so I used the method described in this question; it works, but after I run my application a couple of times the piece of code deletes itself and I don't know why.
This is the code that keeps deleting itself:
txtID.GotFocus += txtID_GotFocus;
It disappears because you don't use conventions that are used by WinForms designer when you add event handlers.
It doesn't matter whether you use GotFocus or Enter event. If you (in your Designer.cs) manually add event handler this way:
txtID.Enter += txtID_Enter;
then it would always disappear from designer next time you move control on designer surface.
You must add event handlers this way:
txtID.GotFocus += new System.EventHandler(txtID_Focus);
txtID.Enter += new System.EventHandler(txtID_Enter);
and nothing would disappear because it's the way designer expects code to be.
Surely it's another evidence about why you should not touch designer generated code and should pay attention to this warning: do not modify the contents of this method with the code editor.
As a workaround use Enter event instead (which is recommended). Also you can assign the handler in your Load event of form.
EDIT
The reason is correctly mentioned by nikita, it's because you didn't use designer conventions. For more information see his answer.
Sometimes when you accidently double click on events it generates the events event method and add and event handler in the InitializeComponent() of the form.
So when I want to delete the event, I have to delete code in two places, the event method it self and the event handler in to InitializeComponent(). But is there away to do this without deleting code manually?
If you have have not edited the event handler body, you can simple remove the event using VS Designer. For this select Events in Properties window and remove the handler that you don't need.
However, if you have edited the event handler (i.e. the body contains some code or comments). You can still remove the event using above step. But from the cs file you have to manually delete the method. This helps to refrain from designer generated code (and possibility of messing it up).
In VS2008, if I double click on the event handler VS creates a default event handler with a default name, e.g. combobox1_SelectedIndexChanged.
Say, for example, i now rename combobox1 to cbStatus. It still has the same event handler, so i now change that to cbStatus_SelectedIndexChanged.
Is there a way, where VS can change the initial combobox1_SelectedIndexChange to cbStatus_SelectedIndexChange rather than generate a new cbStatus event handler in addition to the old event handler? Because every time i have to cut and paste the code to the new event handler and then delete the old one.
In addition, if i have defined the initial event handler and then no longer require the handler, i cannot simply delete the handler from code, as the form designer then complains that it cant find the original event handler. Is there a way where VS can automatically remove the assignment of the event handler from the form designer?
I seem to be spending all day cutting and pasting, and deleting event handler assignments from the forms designer code.
When you rename the control, you should rename the event handler too. The proper way to do this is by refactoring the code.
To do this, just right-click the name of the event handler in the Visual Studio code editor and choose Refactor -> Rename... That will allow you to automatically change the name of it everywhere it's used.
In the case of an event handler, it's probably only used in one other place (the point in code where it's added to the event), so it's not too much trouble to change it manually. You can apply this technique to pretty much anything, though, making it extremely useful when something you're changing is referred to from several different places.
You just have to find the place in the generated code where the combobox1_SelectedIndexChange method is declare and change the name to cbStatus_SelectedIndexChange.
After you change the method name, you also have to update the line where you register the handler:
cbStatus.SelectedIndexChange += new
SelectedIndexChangeEventHandler(cbStatus_SelectedIndexChange);
Just type the new name, then recompile. By this I mean - Change
protected void combobox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
to
protected void renamedcombobox_SelectedIndexChanged(object sender, EventArgs e)
{
}
and then recompile
Visual Studio will throw a compile-time error, because the method that is expected is no longer there.
Double-click on the error in the Output window to go to the assignment of the error handler, and change the error handler there to match the new function name.
Edit - added
The above step will jump you to the line of code described in Justin's answer...
End Edit
I know that's clear as mud, but try it and you'll figure it out with little or no difficulty.
If you single-click instead of double-clicking to automatically create the event handler, you can specify the handler name you want. You could make it something like "SelectedStatusChangedHandler", which is independent of the combobox's variable name. Then press 'enter' and let VS create the handler for you.
.NET newbie alert
Using Visual C# 2008 Express Edition I have accidentally created a click event for a button. I then deleted the automatically-created method code, which resulted in an error saying that the function, which had now been referenced in the form loading code, could no longer be found.
Deleting the following line from the Form1.Designer.cs file's InitializeComponent() function...
this.btnCopy.Click += new System.EventHandler(this.btnCopy_Click);
... seems to do the trick, however, it makes me feel very dirty because of the following warning at the beginning of the #region:
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
I haven't been able to find a way to do this using the form designer, which I assume is the means implied by this warning. What is the correct way to do this?
You do have to be careful when working in the designer.cs files but you don't have to feel dirty about it (when I make the same mistake it is just easier to fix it the designer.cs file). You can do it visually like this:
Open the form in the form designer.
In the form designer, click the button of interest.
Press F4 (or right click the button and then click properties). The properties pane should show up.
At the top of the properties pane, click the lightning bolt. This shows the events for the button.
Find the click event and clear its handler.
Okay, I am usually the one advocating the use of notepad2 or some other text editor to perform coding tasks.
But, since you ask how to do so in the Designer...
Open the form where the erroneous event was added to a control.
Select the control.
Right-click, select "Properties".
Change to "Events" by selecting the button with the lighting-bolt icon.
Select the event you need to remove.
After placing the mouse in the box which is showing the event handler method name, delete all of the text in that box and press enter. This will remove the event handler and the delegate assignment for this event on your control.
The only caveat being: if you wish to preserve your event handler method (i.e. it is not auto-generated by Visual Studio) - you probably want to avoid deleting the assignment in this manner. Because when I say that it removes the event handler - I should say that the declaration of the event handler method in "Form1.cs" (for example) will be deleted as well.