Button Added to Visual Studio C# Project Doesn't Recognize Click Event - c#

I am new to Visual Studio and C#. I have created my first project -- an inventory application using MySql -- and everything works. But I needed to add a new button to the form for deleting records, so I dragged it in from the toolbox, changed the text ("Delete Product") and changed the design name ("DelBtn"). Adding the tool did not create the event handler, so I added the following with the MessageBox for testing:
private void DelBtn_Click(object sender, EventArgs e)
{
MessageBox.Show("Delete button clicked");
}
Clicking the button, however, has no effect, no MessageBox or anything else. Can someone help, please?

Go to your form designer, right-click your button, and select "Properties". In the properties window, there will be an "Events" button (it looks like a lightning bolt). Click that, and look for the Click event of your button. Make sure that DelBtn_Click is listed there. At that point, the button should respond with your code.
Hope this helps!

You need to make sure you've wired up the handler correctly. Look for the designer file that gets generated along with your form (I'm making the assumption that this is winforms).
Then you'll need a line like this in your InitializeComponent method:
this.button1.Click += new System.EventHandler(this.DelBtn_Click);
You don't have to do this in your designer file, you can do it anywhere you want that runs before your form is shown, e.g. the constructor. But it seems to me like something you did -- maybe by manually-renaming your event handler method -- has mangled the automatically-generated designer code that I mentioned above, and you probably just need to fix what's there already.

You need to delegate the event in your designer.cs if you wish:
DelBtn.Click += new System.EventHandler(this.DelBtn_Click);

Just adding a method with the same name as VS would have made for you won't bind the event to that method. Open the properties for the button and at the top of the resulting panel you'll see a lightning bolt. Click that and find the event you want and type in your method name there.

You need to go on the form, click on the button, on the properties menu you need to click on event,find onclick and double click the blank space.
It will automatically create the onclick method for you.

Related

C# Form Application on click event

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

C#: change button name and click function name

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.

Registering MouseDown and MouseMove on Form

How and where can I register a mouse event on a form. When I doubleclick on the form it'll generate the Form_Load event for me and I can add code into there. However when I add something like
private void Form1_MouseDown(object sender, MouseEventArgs e{
Console.WriteLine("mouse down")
}
However when I do a mousedown event on the form I don't get anything on the console. I know something is missing where I register the event to the form or something of the sort. Any ideas?
Thanks,
In the designer view, select the form and then in the properties window, click the little lightning bolt (events).
Here you're able to select which delegate method is called for which event. If you haven't created the method already, just double click the empty space next to an event and it will generate the code for you.
If you are using VS.net then you should find all the events in the property panel. Just pick the ones you want.
If you want to grammatically register an event then the code would looks like:
Form1.Click += new MouseEventHandler(Form1_MouseDown);
in order to unregister it's
Form1.Click -= new MouseEventHandler(Form1_MouseDown);
The event needs to be "wired up" either from the designer or from code. You can wire up an event from Visual Studio by double-clicking the event in the properties window:
Which generates code like the following in the auto-generated .designer file:
theForm.MouseDown += new MouseEventHandler(Form1_MouseDown);
You can also use the code like the above to manually wire up events in your Form_Load method.
With WinForms, you'll want to add it via the design view in Visual Studio.
While in the design view, select your form. Then, click on the 'Events' button in the Properties panel (looks like a lightning bolt) and type in the function name under the appropriate event. You can also click on an event here to automatically generate a new function in the code-behind.
As you noticed, double-clicking the form will automatically generate a certain function in the code-behind. For forms, it is Load but for other things it may be MouseDown or some other event.
By your explanation it seems like you have registered the event properly through the designer... if you still don't see the string on the console try System.Diagnostics.Debug.WriteLine (Maybe you are looking on the wrong window)

Winforms - Visually remove button click event

.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.

Is it possible to automatically hook up events in C# using VS2008?

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

Categories