Click here to Skip to main content
15,877,280 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello guys,

I am building a desktop database application. I want the menu items' click events to invoke methods that would fetch data from the the database. Here is the line of code:

//viewAllStaff.Click += new System.EventHandler(pdt.viewAllStaffMethod());


It prompt me that " error: method name expected".

Kindly help. I am a novice.
Posted

1 solution

When adding the event handler, you are expected to supply a method name, nothing more. Your code has brackets after the method name, as if you are trying to call the method. Change it to:
viewAllStaff.Click += new System.EventHandler(pdt.viewAllStaffMethod);


Edit:
The method handling the event cannot be just any old method. It needs to follow a specific pattern for .NET to be able to call it when the event happens. In the case of a menu item's click event, that pattern is:
1. The method's return type must be void.
2. The method must accept two parameters: an object named sender and an EventArgs named e.

As such, the method declaration must be:
public void viewAllStaffMethod(object sender, EventArgs e)
 
Share this answer
 
v2
Comments
Member 10645876 6-Mar-14 8:28am    
thanks again BotCar4K
I have done the changes of method parameters but still the same error.
BotCar 6-Mar-14 9:03am    
In that case, I'm not sure what went wrong.

Have you tried adding an event handler through Visual Studio's GUI builder? Just select the menu item you want to add an event to, go to the Events tab of the Properties pane and double-click on the Click event.
Member 10645876 7-Mar-14 6:57am    
more thanks BotCar4k
Kindly point out how to do this in code-behind. this menu item is defined for run time not in XAML.
BotCar 7-Mar-14 7:33am    
Oh, it's a WPF application? Sorry, I assumed it was a WinForms application. My WPF knowledge is a bit rusty, so I'm not sure how much help I can provide.

Have a look at this code that was auto-generated when I added an event to a control:
((System.Windows.Controls.MenuItem)(target)).MouseDown += new System.Windows.Input.MouseButtonEventHandler(this.MenuItem_MouseDown_1);

Notice that the namespace of the event handler is not the same as that of a WinForms event. This also means that the controls does not always support the same events as their WinForms equivalents (such as having a MouseDown event but not a Click event). Make sure what events your control supports and where the event handler delegates are.

With that in mind, your code would then be something along the lines of:
viewAllStaff.MouseDown += new System.Windows.Input.MouseButtonEventHandler(pdt.viewAllStaffMethod);
and
public void viewAllStaffMethod(object sender, MouseButtonEventArgs e)
{
// Your code.
}
Member 10645876 7-Mar-14 7:37am    
Hi BotCar4K

I solved the problem.I added the two parameters at declaration. when adding the event handler i simply supply the method name <pre> viewAllStaff.Click += pdt.viewAllStaffMethod; </pre>. Now the problem i am facing is to call the method from the main method for execution. what arguments to supply.
Sorry i am a novice ..1st timer..

MAJ

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900