How to keep WinForms designer from generating Click handlers? - c#

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.

Related

Events list in Visual Studio 2015

Using VS2013 it was possible, at least with VB.NET, to double click on a control and then the default event would show up in the code file.
Above this there was a pull down list of the other possible events for this control.
Now I'm working in VS2015 and in C#, but that list is not there.
I can still double click on a control to get the default event, but I cannot add another event. I don't think I'm supposed to edit the designer file.
How do I do this now?
Do I need to add events in the code file now?
for example I want to be able to drop a file on my windows application.
So somewhere I need to add the event for this.
Winforms :
Wpf:
To see the properties window:
Using VS2013 it was possible, at least with VB.NET, to double click on
a control and then the default event would show up in the code file.
Above this there was a pull down list of the other possible events for
this control.
This is known as the Navigation Bar. You can toggle it on/off in Tools --> Options --> Text Editor --> {Select Language} --> Navigation Bar.
BUT...the Navigation Bar behaves differently in C# than it does in VB.Net. It won't do what you want in C#, sorry!
To wire up an event using the IDE in C#, you have to first select the thing in question, then go to the Properties Pane and switch it to the Events view with the "Lightning Bolt" icon as Empereur Aiman has shown in his post.
C#, however, can do something that VB.Net cannot. With C#, you can wire up an event by writing a line of code in the editor and have the IDE generate the event stub for you. For instance, in the snippet below, a dynamic button is being created:
Button btn = new Button();
If you want to wire up its Click() event, you'd type in:
btn.Click +=
After the equals sign = is typed, you'd press {Tab} and the event stub will be generated for you:
private void Btn_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
If you keep the mouse on the Button key word in xaml code and then click on the lightning icon, you will be able to see the click event.

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.

Form.cs not being updated by changes to Form.cs[Design]

I am new to Visual Studio and I am just messing around with the controls to see how things work. I made one form that had a single button that, when pushed, simply printed "Hello World" to the screen. To try something more complicated I deleted that button and added various other tools to the Form. However the code in the Form.cs file was not updated to reflect these changes to the design and I can find no way to update it manually.
Any advice is appreciated.
Regards.
If you look at Form1.Designer.cs (assuming your form is called Form1) you will see a list of all the code that was generated by building your app, within there you will see your button name, if the button is deleted you should be able to safely delete the code between the comments.
You can see the changes in Form1.Designer.cs file
the code is below these lines....
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
To update manually just call manual_update().
manual_update()
{
Button b = new Button();
b.Text = "new button";
b.Size = new Size(100, 20);
b.Location = new Point(20, 20);
this.Controls.Add(b);
}
You can do almost everything manually.
If your Button had an OnClick event, it will NOT be erased when the Button is erased. Visual Studio assumes that this code may be used somewhere else and is not data-destructive.
Every time you delete a control that you had events, they will remain. You must manually go through and clean up your code.
EDIT:
To make the "new code" appear for the newer controls, you must either double click the button to generate the default event for that control, or go into the Properties of that control and generate the code for the events there.
Also Try rebuilding.. if you removed it from the form sometimes VS does not remove that code from the designer.. and you will have to manually remove it / update it.. also make sure you don't have any compile errors as well
if you have errors .. they changes may not be shown until the error(s) have been resolved.
Since you mentioned that you added other tools in the front-end Form file, make sure that your webcontrols in the Form are connected to whatever event handlers you have.
Also, make sure you're re-compiling/re-building your page, just for sanity check and if needed.
If I understand your question correctly then the code that adds controls and changes control properties is all auto-generated and resides, for your example, in Form1.Designer.cs. This file should generally never be touched except by Visual Studio.
If you want to add controls manually you should do it in the Form1.cs after the InitializeComponent() call or in an event like the Form_Load event. Here is an example of adding a button in a form load event:
private void Form_Load(object sender, EventArgs e)
{
Button b = new Button();
b.Left = 10;
b.Top = 10;
b.Text = "Button!";
this.Controls.Add(b); //'this' would be the form self-pointer
}
Events are different. These are what will change in Form1.cs, but changes made to the designer will not always be reflected in the code-behind file for events. This is the nature of VS. Sometimes deleting a button will not delete the events from the code and vise-versa.
You may have to remove events from controls manually in the code file.
Code for added controls can usually be forced by changing the control in the designer, but at least one thing that won't change is event names when the control name changes. For instance, if a button is called button1 and links to the click event button1_click(object sender, EventAgrs e), changing the button name to button12345 will not change the event name.
You can change linked events in the designer by opening the control properties and clicking on the lightning bolt. This shows all events for a control. Double clicking in an event field will either take you to the linked event or generate an event if the field is blank. This dialog will also allow multiple controls to link to a single event.
Here is an example of the event properties dialog:
You may just have to fiddle with adding controls, linking events, removing controls, etc. to get a feel for when changes are updated across both designer and code-behind and when changes are not updated.
There is a Form1.Designer.cs file, the designer code lies in this file. If u cant see the file in solution explorer than there is a button on the solution explorer pane on top, that is view files. Click it.
Visual studio provides you drag and drop functionality of controls, and to change the properties just right click on the control and click 'properties'. So actually you may not need to change the designer code. But you can as u like...

Doesn't contain a definition for XXX error

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.

Categories

Resources