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