rk_1972
asked on
Event Shadowing/hiding in C#
Hi,
As i am extending a list control with additional properties and controls.
I am trying to hide a ListControl event "SelectedIndexChanged" using "new" Keyword as shown below
public new event SelectedIndexChanged (object sender, SelectedIndexChangedEventA
I am getting invalid token error at "("
If i try without any parameters it is not giving any error but the event is not invoked.
public new event SelectedIndexChanged;
Or you could do this (more similar to what you demonstrated in your question), when a user subscribes to YourListView.SelectedIndex Changed the request is essentially ignored:
public new event System.EventHandler SelectedIndexChanged
{
add
{
//base.SelectedIndexChange d += value;
}
remove
{
//base.SelectedIndexChange d -= value;
}
}
Note that the user could always cast your object back to a System.Windows.Forms.ListV iew and access the ListView.SelectedIndexChan ged event from there though.
public new event System.EventHandler SelectedIndexChanged
{
add
{
//base.SelectedIndexChange
}
remove
{
//base.SelectedIndexChange
}
}
Note that the user could always cast your object back to a System.Windows.Forms.ListV
An alternative possibliity would be to filter out the appropriate window messages by overriding the System.Windows.Forms.Contr ol.PreProc essMessage method which happens to be intended at filtering out unwanted messages - see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformscontrolclasspreprocessmessagetopic.asp
HTH
HTH
ASKER
Thanks for the quick response.
As in VB.NET we can shadow the same event, why cannot be it done in C# ?
Thank you
The second example I gave you ("public new event System.EventHandler SelectedIndexChanged") is the c# equivalent of a shadow. In neither case is the functionality or accessibility of the ListView.SelectedIndexChan ged event changed.
Does that answer your question rk_1972, or do you have any more questions?
ASKER
Sorry buddy,
that doesn't work. I am not able to Raise the event.
What is it exactly you are trying to do? Are you trying to disable the SelectedIndexChanged event, or are you creating a NEW event that happens to have the same name as SelectedIndexChanged, but actually has nothing to do with it?
ASKER
Yes, You are right !
I am trying to create a new event with same name and with different signature.
A similar code in VB is
Public Shadows Event SelectedIndexChanged
(ByVal sender As Object, ByVal e As SelectedIndexChangedEventA
please note the SelectedIndexChangedEventA
So when i write a event in my composite control , it should raise with this eventArgs, instead of regular empty eventArgs.
Thank you
What will cause this event to be raised?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, I am trying the same but i am not able to raise event ...
Below is the code:
//Add the items to the controls collection as linkbuttons
foreach(ListItem objItem in this.Items)
{
// create a new link button for each item and set the respective values.
ExtLinkButton objLink = new ExtLinkButton();
objLink.Text = objItem.Text;
objLink.Value = objItem.Value;
objLink.Selected = objItem.Selected;
objLink.Enabled = true;
objLink.EnableViewState = false;
// every link buttons click event is handled by new event handler.
objLink.Click += new EventHandler(ExtLnkButton_
this.Controls.Add(objLink)
}
void ExtLnkButton_Click(object sender, System.EventArgs e)
{
// sender is a Link Button, set that to a objLink
BasicControls.ExtLinkButto
// instantiate the class SelectedIndexChangedEventA
OnSelectedIndexChanged(new
}
// To raise the event, you pass two arguments: the sender (the control MyControl) and the //MyEventArgs object passed to the method.
protected virtual void OnSelectedIndexChanged(Sel
{
if (SelectedIndexChanged != null)
{
SelectedIndexChanged(this , e);
}
}
--------------------------
-- Event Args Code
--------------------------
public class SelectedIndexChangedEventA
{
public ExtLinkButton Link;
internal SelectedIndexChangedEventA
{
this.Link = objLink;
}
// shadow the Read only property "Empty" of the System.EventArgs
// The value of Empty is a read-only instance of EventArgs equivalent to the result of calling the EventArgs constructor
public new SelectedIndexChangedEventA
{
get
{
return new SelectedIndexChangedEventA
}
}
}
ASKER
Okay I got it !
Thanks for your answers.
Thanks for your answers.
The best you could do is effectively disable the event by placing the following code in your ListView subclass:
protected override void OnSelectedIndexChanged(Eve
{
}
i.e.: do not call base.OnSelectedIndexChange