EventArgs that return values... How do i replicate?

M

Michael.Suarez

Consider the TextBox Control. It has a KeyPress event of type
KeyPressEventHandler which passes a KeyPressEventArgs to whatever
method is assigned to the event.

When you set e.Handled = false within the assigned method, it disallows
the keypress.

Suppose I have a usercontrol consisting of a textbox, a button, and a
listview. When the user enters something in the textbox and clicks the
button, the textbox's text gets added to the listview as a new item.

Suppose I want to create an event that gets invoked on the button
click, called BeforeAddTextToListview, which will be of type
BeforeAddTextToListviewHandler and pass
BeforeAddTextToListviewEventArgs to whatever method is assigned to the
event.

Suppose BeforeAddTextToListviewEventArgs has a property called Allowed.
When Allowed=true, the addition to the listview will occur.

This gives the user of the usercontrol the ability to control what is
allowed to go into the listview.

So in my the usercontrols class I have:

public event BeforeAddTextToListviewHandler BeforeAddTextToListview;

public delegate void BeforeAddTextToListviewHandler (object sender,
BeforeAddTextToListviewEventArgs e);

private void btnAddTextToListView_Click(object sender, EventArgs e)
{

BeforeAddTextToListviewEventArgs e = new
BeforeAddTextToListviewEventArgs();

if (BeforeAddTextToListview!= null)
{
BeforeAddTextToListview(this, e);
}

if (e.Allowed)
{
//add text to listview
}

}

The problem here is that e is not passed by reference, so it wont know
the value of allowed set by the user.

I could just define BeforeAddTextToListviewHandler as:

public delegate void BeforeAddTextToListviewHandler (object sender, ref
BeforeAddTextToListviewEventArgs e);

and pass e by reference. Then I would get what i want.

However, the KeyPressEventArgs in KeyPressEventHandler is not passed
by reference.

So what I want to know and what i want to do is...
How does the KeyPressEventHandler get the e.Handled back when e does
not have the ref keyword preceding it?
How can i replicate this?

Any help, comments, suggestions greatly appreciated.

Thanks,
Mike
 
M

Michael.Suarez

nevermind...

I tried it without the ref and it worked, so I just answered my own
question.... for some reason i thought it wouldn't.

I guess the ref is assumed when you are dealing with eventhandlers.
 
N

Nicholas Paldino [.NET/C# MVP]

On a side note, for your class, I would not use an Allowed property.
Rather, I would derive the EventArgs class from CancelEventArgs, found in
the System.ComponentModel namespace.
 
M

Michael.Suarez

Thanks for the note.
The reason I create my own EventArgs class with an allowed property, is
that i also have a text property to pass the user the text that needs
to be analyzed to method so that the method can determine if this text
should be allowed to be added to the listview.

On a side note, for your class, I would not use an Allowed property.
Rather, I would derive the EventArgs class from CancelEventArgs, found in
the System.ComponentModel namespace.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

 
N

Nicholas Paldino [.NET/C# MVP]

Michael,

I understand that, but what I am saying is that instead of using the
EventArgs class as your base, use the CancelEventArgs and you won't have to
worry about the Cancel property, it's there for you in the base. Then you
can add the properties that you need in your specialization.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Thanks for the note.
The reason I create my own EventArgs class with an allowed property, is
that i also have a text property to pass the user the text that needs
to be analyzed to method so that the method can determine if this text
should be allowed to be added to the listview.

On a side note, for your class, I would not use an Allowed property.
Rather, I would derive the EventArgs class from CancelEventArgs, found in
the System.ComponentModel namespace.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

thanks.

Brian Gideon wrote:
Michael,

I think you may be a little confused about value and reference types
and how they behave with the different parameter passing mechanisms.
Maybe the following article will help clear things up.

http://www.yoda.arachsys.com/csharp/parameters.html

Brian

(e-mail address removed) wrote:
nevermind...

I tried it without the ref and it worked, so I just answered my own
question.... for some reason i thought it wouldn't.

I guess the ref is assumed when you are dealing with eventhandlers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top