473,395 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

EventHandler & Return Types

I have auto-generated some code for a button by double clicking it.
By default, the code is created with a return type of void and
assigned to a click event. Now, I want the function to return a
DialogResult instead, so I go and replace 'void' with DialogResult in
the function header, but then I get an error stating it's the wrong
return type.

Double clicking the error in Visual Studio takes me to where the
EventHandler is assigned to the click event, but since only asks for
the name of the function and not a return type, I don't see how the
return type can be wrong.

The auto-generated function looked like:
private void button1_Click(object sender, EventArgs e)

I changed it to:
private DialogResult button1_Click(object sender, EventArgs e)

And the error takes me inside the InitializeComponent() function on
the line:
this.button1.Click += new System.EventHandler(this.button1_Click);

What am I doing wrong here?

Nov 6 '07 #1
2 8478
The Event Handler for an Event must match the signature of the delegate that
handles the Event. The Button.Click event is designed to be handled by a
method with a void return signature, taking 2 arguments, an object and an
EventArgs instance. This is because the Button.Click event is a
System.EventHandler delegate method. System.EventHandler is a delegate class
that defines the signature of the event handler.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

<ma******@gmail.comwrote in message
news:11**********************@q3g2000prf.googlegro ups.com...
>I have auto-generated some code for a button by double clicking it.
By default, the code is created with a return type of void and
assigned to a click event. Now, I want the function to return a
DialogResult instead, so I go and replace 'void' with DialogResult in
the function header, but then I get an error stating it's the wrong
return type.

Double clicking the error in Visual Studio takes me to where the
EventHandler is assigned to the click event, but since only asks for
the name of the function and not a return type, I don't see how the
return type can be wrong.

The auto-generated function looked like:
private void button1_Click(object sender, EventArgs e)

I changed it to:
private DialogResult button1_Click(object sender, EventArgs e)

And the error takes me inside the InitializeComponent() function on
the line:
this.button1.Click += new System.EventHandler(this.button1_Click);

What am I doing wrong here?

Nov 6 '07 #2
Hi,

In addition to Kevin's answer, if you are using button1 to close the dialog, use

private void button1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}

If you are calling button1_Click from somewhere you wish to return a DialogResult to do

private void button1_Click(object sender, EventArgs e)
{
CommonClickMethod();
}

private DialogResult CommonClickMethod()
{
return DialogResult.Cancel;
}

and call CommonClickMethod instead of button1_Click
On Tue, 06 Nov 2007 18:02:33 +0100, <ma******@gmail.comwrote:
I have auto-generated some code for a button by double clicking it.
By default, the code is created with a return type of void and
assigned to a click event. Now, I want the function to return a
DialogResult instead, so I go and replace 'void' with DialogResult in
the function header, but then I get an error stating it's the wrong
return type.

Double clicking the error in Visual Studio takes me to where the
EventHandler is assigned to the click event, but since only asks for
the name of the function and not a return type, I don't see how the
return type can be wrong.

The auto-generated function looked like:
private void button1_Click(object sender, EventArgs e)

I changed it to:
private DialogResult button1_Click(object sender, EventArgs e)

And the error takes me inside the InitializeComponent() function on
the line:
this.button1.Click += new System.EventHandler(this.button1_Click);

What am I doing wrong here?



--
Happy coding!
Morten Wennevik [C# MVP]
Nov 6 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

192
by: Kwan Ting | last post by:
The_Sage, I see you've gotten yourself a twin asking for program in comp.lang.c++ . http://groups.google.co.uk/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=45cd1b289c71c33c&rnum=1 If you the oh so mighty...
0
by: dominosly | last post by:
Okay, this is a rather complicated problem, so here is the short of it: I am using a custom designed user control that simply writes out a plain old html submit button to the page. When the...
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
12
by: aplaxas | last post by:
Hi! "CDemo.Call" eventHandler is added to "button3.click" Event when both button1 and button2 are clicked. However, I want to add "cd.Call" only one time even though I clicked both button1 and...
4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
16
by: Ed A | last post by:
Hi all: I'm using a struct of type Point that is being passed on to a method as a refrence type (boxing), the UpdatePoint method changes the coordinates of this point as such: Point p = new...
8
by: Alex Vinokur | last post by:
Here is a code from http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8 -------------------------------------- class Shape { public: virtual ~Shape() { } // A...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.