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

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.

Related

How do I rename a form object (button, textbox, etc.) after Visual C# has autocreated the code?

Okay, so here's my problem: Whenever I create a button, textbox, listbox, then double-click to view the source code:
private void quitToolStripMenuItem_Click(object sender, EventArgs e)
{
}
After this has been auto-created by Visual Studio, it won't allow me to change it later.
So when I go into properties and want to change "quitToolStripMenuItem" (in the Name property field) to "mnuQuit," it will show up in the properties window properly, and will change the name (for all intents and purposes), but when I double-click to view source - it still shows the 'quitToolStrip..." name.
If I rename it to
private void mnuQuit_Click(object sender, EventArgs e)
It will throw a big hissy fit, and then my form design will be gone and a (basically) 404 message will appear instead of the form.
How can I do it without deleting the item and then recreating?
If you want to rename the event handler method, go to the designer, select the object (the menu item in your case), and in the properties window click the events button (it looks like a lightning bolt), and rename the event handler method from there.
In C#, the event handler is linked to the object that raise the event by a delegate, so the name of the method does not matter. You can have a button called Jack and event handler called Jill_Click that will actually handle the resize event of Jack. If you open the designer code you will see something like:
this.Jack.Resize += new System.EventHandler(this.Jill_Click);
Click on the Events (lightning) icon at the top of the Properties window and delete the text in the Click field.
Just refactor the method name? This will update it on the .designer.cs, in your designer and of course in your code.
Select the method name, right click, Refactor → Rename.

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