473,394 Members | 1,721 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,394 software developers and data experts.

Events With Feedback

Hello All,

Does anybody have an example to raise an event that may or may not give
feedback to the caller? I am thinking about something roughly like the
Cancel property of an CancelEventArgs or the Handled property on the KeyDown
events.

What I have is a user control that I want to raise an event that allows the
parent to set a property in the control, and set a flag in the event that
says it has set the property. The user control would then process
conditional code if the parent responds to the event (or does not).

Thanks,
pagates
Nov 17 '05 #1
2 1361
Hi,

You can create your own event, and you can send a class inheriting
EventArgs, by this way you can extend the argunments of the event. That's the
way it works on the examples that you have stated.

Cheers
Salva

--
Salvador Alvarez Patuel
Exony Ltd - London, UK
"pagates" wrote:
Hello All,

Does anybody have an example to raise an event that may or may not give
feedback to the caller? I am thinking about something roughly like the
Cancel property of an CancelEventArgs or the Handled property on the KeyDown
events.

What I have is a user control that I want to raise an event that allows the
parent to set a property in the control, and set a flag in the event that
says it has set the property. The user control would then process
conditional code if the parent responds to the event (or does not).

Thanks,
pagates

Nov 17 '05 #2
Pagates:

This just breaks into a few steps:
1. Define a custom event args class that contains your flag
2. Define a delegate that accepts your evetargs as a parameter.
3. A little extra work in your event invocation.
(you have some decisions to make with #3)

// warning: the code is off the cuff, might contain syntax errors.

public class MyEventArgs: System.EventArgs
{
public bool EventHandled; // here's your flag
}

public delegate void MyEventHandler(object sender, MyEventArgs e);

public class MyClass
{
public event MyEventHandler MyEvent;
protected bool RaiseMyEvent()
{
MyEventHandler myEvent = this.MyEvent;
if(handler == null)
return false;

MyEventArgs e = new MyEventArgs();
// remember you might have multiple event subscribers
// check your args for each subscriber
bool eventHandled = false;
foreach( MyEventHandler handler in myEvent.GetInvocationList())
{
handler(this, e);
if(e.EventHandled)
eventHandled = true;
}
return eventHandled;
}
}

Another way to do it would be to exit the foreach loop as soon as someone
handles the event. If you exit the foreach loop as soon as someone sets the
handled flag, it means that not all subscribers will receive the event.
If it is more appropriate that all subscribers receive the event, just set a
local bool so that you can remember that one of the subscribers set the flag

"pagates" <pa*****@discussions.microsoft.com> wrote in message
news:C1**********************************@microsof t.com...
Hello All,

Does anybody have an example to raise an event that may or may not give
feedback to the caller? I am thinking about something roughly like the
Cancel property of an CancelEventArgs or the Handled property on the
KeyDown
events.

What I have is a user control that I want to raise an event that allows
the
parent to set a property in the control, and set a flag in the event that
says it has set the property. The user control would then process
conditional code if the parent responds to the event (or does not).

Thanks,
pagates

Nov 17 '05 #3

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

Similar topics

0
by: Stephen Williams | last post by:
I am migrating a VB 6 PictureBox control to VB.NET. In VB 6, this control modified its border style during the mouse down and up events to provide user feedback that it was selected. Once...
9
by: Lachlan Hunt | last post by:
Hi, I've written some javascript to implement DOM 2 Events in IE (addEventListener, removeEventListener, etc) and I would like to know which browsers it works in. I've tested it in Firefox 1.0.6,...
10
by: Drakier Dominaeus | last post by:
This is my first time posting here, so please forgive me if I do anything incorrectly. I've been learning C# and working with different things and decided I wanted to get into Multi-Threading....
3
by: Sasha | last post by:
Hi everyone, Here is my problem: I have the following classes: - DataNode - this class is designed to hold some data and will be contained in a tree like data structure DataTree. When...
7
by: cider123 | last post by:
I'm coding a project using the following article as reference: http://www.codeproject.com/csharp/DynamicPluginManager.asp In this type of project, plugins are loaded dynamically into a Plugin...
4
by: DKode | last post by:
Hello, I have a question about delegates and events I have a basic understanding of each, but in the apps I'm building I'm never needing to use these tools. I'm wondering when and where you...
4
by: bboyle18 | last post by:
Hi, I am working with a table sorting script which can be found here http://www.workingwith.me.uk/articles/scripting/standardista_table_sorting This script works very nicely, but when there is a...
13
by: Scott M. | last post by:
In a VS 2005 ASP .NET Web Application Project (WAP), how can I see a list of the page (web form) events (i.e. new, init, loadviewstate, load, saveviewstate, etc.) and set up event handlers for them?
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.