?
Solved

Event Shadowing/hiding in C#

Posted on 2004-09-01
13
Medium Priority
?
648 Views
Last Modified: 2012-05-05

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, SelectedIndexChangedEventArgs e);

 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;






0
Comment
Question by:rk_1972
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 10

Expert Comment

by:eternal_21
ID: 11955651
You will not be able to "hide" this event; The .NET language ensures that this member is accessable (all public parent members are accessable from child objects).

The best you could do is effectively disable the event by placing the following code in your ListView subclass:

  protected override void OnSelectedIndexChanged(EventArgs e)
  {
  }

i.e.: do not call base.OnSelectedIndexChanged(e), and the SelectedIndexChanged event will not be processed.
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 11956017
Or you could do this (more similar to what you demonstrated in your question), when a user subscribes to YourListView.SelectedIndexChanged the request is essentially ignored:

  public new event System.EventHandler SelectedIndexChanged
  {
    add
    {
      //base.SelectedIndexChanged += value;
    }
    remove
    {
      //base.SelectedIndexChanged -= value;
    }
  }

Note that the user could always cast your object back to a System.Windows.Forms.ListView and access the ListView.SelectedIndexChanged event from there though.
0
 

Expert Comment

by:_dataking_
ID: 11956855
An alternative possibliity would be to filter out the appropriate window messages by overriding the System.Windows.Forms.Control.PreProcessMessage 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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:rk_1972
ID: 11958214

Thanks for the quick response.

As in VB.NET we can shadow the same event, why cannot be it done in C# ?

Thank you
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 11959720
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.SelectedIndexChanged event changed.
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 11968795
Does that answer your question rk_1972, or do you have any more questions?
0
 
LVL 1

Author Comment

by:rk_1972
ID: 11969004

Sorry buddy,
that doesn't work. I am not able to Raise the event.
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 11973748
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?
0
 
LVL 1

Author Comment

by:rk_1972
ID: 11975116

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

please note the SelectedIndexChangedEventArgs derived from eventArgs class. And stores few variable values and objects.

So when i write a event in my composite control , it should raise with this eventArgs, instead of regular empty eventArgs.

Thank you





 




0
 
LVL 10

Expert Comment

by:eternal_21
ID: 11975223
What will cause this event to be raised?
0
 
LVL 10

Accepted Solution

by:
eternal_21 earned 250 total points
ID: 11975369
Well, here is another example anyway:

    class MyListView: System.Windows.Forms.ListView
    {
      // Custom event arguments class
      public class SelectedIndexChangedEventArgs: System.EventArgs
      {
        // Define your custom EventArgs class
      }
      // Custom event handler
      public delegate void SelectedIndexChangedHandler(object sender, SelectedIndexChangedEventArgs e);

      // Declare your custom event
      public new event SelectedIndexChangedHandler SelectedIndexChanged;

      // This method will raise the custom event
      private void RaiseSelectedIndexChangedEvent(object sender, SelectedIndexChangedEventArgs e)
      {
        if(SelectedIndexChanged!=null)
          SelectedIndexChanged(sender, e);
      }
    }
0
 
LVL 1

Author Comment

by:rk_1972
ID: 12000198

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_Click);
   this.Controls.Add(objLink);
                          
 }            

  void ExtLnkButton_Click(object sender, System.EventArgs e)
  {
  // sender is a Link Button, set that to a objLink
  BasicControls.ExtLinkButton objLink  = (BasicControls.ExtLinkButton)  sender  ;
  // instantiate the class SelectedIndexChangedEventArgs.
  OnSelectedIndexChanged(new  SelectedIndexChangedEventArgs(objLink));
            }

// 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(SelectedIndexChangedEventArgs e)
            {      
                     if (SelectedIndexChanged != null)                          
                  {
                        SelectedIndexChanged(this , e);
                  }
            }      




---------------------------------------------------------------------------------
-- Event Args Code
-------------------------------------------------------------------------------
public class SelectedIndexChangedEventArgs: System.EventArgs
      {                        
            public ExtLinkButton Link;
            internal SelectedIndexChangedEventArgs(ExtLinkButton objLink)
            {
                  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 SelectedIndexChangedEventArgs Empty
            {
                  get
                  {
                        return new SelectedIndexChangedEventArgs(null);
                  }                        
            }            
      }            
0
 
LVL 1

Author Comment

by:rk_1972
ID: 12001570
Okay I got it  !

Thanks for your answers.

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question