When I build a vb.net winforms application in VS2010, I can double click on a control on my form and it takes me to the code window. Then there are dropdown lists at the top left and top right of the code window that allow me to select the control and the event that I want to handle...
If the event handler already is already in use/defined/coded, it takes me there, otherwise it defines a new handler method with a standard naming convention (_click(blah blah blah))...
But for some reason when I build a c# application, there is no such "cheat"... call me lazy, but I don't like having to type code that I don't need to, and since the functionality exists in VS to "just do it", why can't I do it in a C# app...???
Maybe there is just a setting I need to find... anyone know of such a setting..???
Thanks
In VS2010's C# mode, similar functionality is enabled by default. Double-clicking on a control in the visual form designer will create a default event handler for that form in the code window. This may not always be the event that you would like to handle, however. If that is the case, right-click the control in the form designer and select Properties from the context menu. A property grid will appear that contains a list of all of the control's properties and events. Click on the lightning-bolt icon at the top of this grid to select the Events tab. From here, double-clicking on any of the event rows will automatically create a new event handler for the control in the code window.
Related
i have used VB.net on a web page and added an asp:button on the front page and in the code behind i can just click on events and the button name in the top drop downs and select click event. Visual studio's will auto create my function/sub for that event.
i am just learning to write everything in C# and i do not see this option to auto create my code.
it has the code of a declaration in the designer.cs file and says to move that line into the code behind of the page. but i need to either remove that or just add the sub to handle the click event.
protected global::System.Web.UI.WebControls.Button btnSubmit;
1) i do see that if i go into the designer of that page and double click the button i can get it to auto generate the code. BUT is there not a way to do it from the code behind like i do it in vb.net?
2) do i need to move that line of code above from the designer.cs to my code behind file?
If memory serves me correctly, you can follow the steps below to produce the same result in a c# web forms application:
Select the UI element on either a web form (.aspx) or a User Control (.ascx).
Select the Object Explorer or Properties Viewer (the higlighted control should be selected).
Select the events tab (lighning bolt).
Double click on the event that you want to have the server side event handler auto generated for.
I am starting with C# programming, I have a scenario, where I create a windows form application. I have created a button inside the form and double click it to generate its associated click function. The visual studio generates the function with default name.
private void button1_Click(object sender, EventArgs e)
{
}
Now when I rename the button in properties, the function name remains same, making it difficult to relate button and function, I have an old code where a lot of such cases are present. So, I cannot delete and recreate the buttons and other things.
How can I rename the functions when I rename the button?
Right click on the function name and select Rename. The name will be highlighted (it's green on my system, see picture below). Edit the name and then hit the Apply button. The button's click event will correctly point to the newly renamed method.
You can change the name of the method by simply typing the new name in place of the old one when editing the code. Just like changing any text.
Then you can validate that the control still uses that event by either:
Go to the Form designer and select the control. Check its properties/events and make sure the name still lines up.
Go to the code generated by the form designer and find where it assigns the method to the event, and change it if it needs to be changed.
Either one of these should also update the other one automatically, though I imagine it's generally preferred to use the first approach for consistency with the rest of the form design process.
Note that the method name can be anything you like, there's no steadfast rule that says it must be controlName_eventName(object sender, EvenArgs e). For example, if you have multiple controls which share an event handler then you don't have to decide which control gets to be the one in the method name. You can give it any common name, such as saveFormChanges(object sender, EvenArgs e) and still assign it as the handler for that event.
You need to change the name of Event from the property window.
go to your designer Right Click -> Properties (F4) -> Events -> ClickEvent -> Type in the name what so ever you want and click this will create Click event with the name you entered
change button1_Click method name in code to new name and then press ctrl+. (period) and chose Rename. Alternatively, click on light bulb and select Rename.
Name of the function does not necessary need to match button name, it's just the convention which makes it easier to understand which element is bound to which function on which event.
If you enter the FormName.Designer.cs file, you will find InitializeComponent function which contains the properties setup of all form elements, including their event binding. For example, you will probably have something like this over there:
this.button1.Click += new System.EventHandler(this.button1_Click);
where button1 is name of the element, Click is name of the event of that element and button1_Click is the name of the function handling that event.
If you're in the design mode of the form FormName.cs [Design] you can find the lightning icon inside the VisualStudio Properties window (one of the tabs next to your Solution Explorer tab). Under that icon, you will find the list of all the possible events of the currently selected element in your designer. On the example of your button it should look like this:
You can handle the event binding either here or inside your FormName.Designer.cs file, but it's preferable through this view because content of the Designer file is automatically generated. InitializeComponent function is then called inside the constructor of your form which initializes the form based on the setup of your designer file.
Here's the steps to reproduce the problem :
Create a c# project with a form with 1 control, let's say a textbox.
Using the properties window (lightning bolt thingy), add a "click" event.
Write some code in that method.
Using the properties window, remove the content of the "click" event cell.
I would like to know why is the code from step 3 has disapear (but not the method signature).
Normal behavior from the IDE ? Option in the "tools-options" menu ?
The method you created should remain even after it has been unhooked from the control's event. The only time VisualStudio will automatically remove the method is if the method is empty. Once you add custom code, the method should not be removed.
.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.
I recently switched over to C# from vb.NET and within visual studio found that hooking up events is extremely annoying. In VB I could select a control from a drop down on the top left and the event on the top right drop down and the method would automatically be created and attached to the control using "handles". I know that is not supported in C# but it seems I have to go through each control and add the events manually on the page and the codebehind. Is there an easier/faster way to do this like in VB or is it just how it is? Thanks!
Yes, there is! Click a control in Design view, then click on the "Events" button in the Properties window (see 1 in hand-annotated diagram below).
From here you can see a list of all the events available to that control. By typing a method name (see 2) and pressing enter, Visual Studio will create a method (if it doesn't already exist) and hook it up properly.
Alternatively, double-clicking in the field where you would type in the handler name causes Visual Studio to assign a default value.
You can setup events extremely fast in C# compared to VB. In the code window type the name of the instance and then event name then write += and press tab twice. That will hook up the event and create a method name accordingly that will handle the event.
For example write:
panel1.MouseClick +=
and then press tab once to insert the eventhandler and twice to both insert eventhandler and create the method for it.
alt text http://img136.imageshack.us/img136/7514/eventhandlercsharp.png