Click here to Skip to main content
15,920,836 members
Home / Discussions / C#
   

C#

 
GeneralApplication displaying Japanese, German, and English text. Pin
jerrycainjr30-Jul-04 12:28
jerrycainjr30-Jul-04 12:28 
GeneralRe: Application displaying Japanese, German, and English text. Pin
Heath Stewart30-Jul-04 12:59
protectorHeath Stewart30-Jul-04 12:59 
GeneralHighlighting a object selected & dynamic resizing Pin
smartyosu30-Jul-04 12:18
smartyosu30-Jul-04 12:18 
Generaltab control problem Pin
blankg30-Jul-04 10:59
blankg30-Jul-04 10:59 
Generaldifferentiating between MouseDown and single click Pin
smartyosu30-Jul-04 9:28
smartyosu30-Jul-04 9:28 
GeneralRe: differentiating between MouseDown and single click Pin
Heath Stewart30-Jul-04 9:32
protectorHeath Stewart30-Jul-04 9:32 
GeneralEvent Handler Pin
eggie530-Jul-04 7:42
eggie530-Jul-04 7:42 
GeneralRe: Event Handler Pin
Heath Stewart30-Jul-04 8:25
protectorHeath Stewart30-Jul-04 8:25 
The event pattern in .NET is quite easy. The correct approach is define a delegate (using EventHandler or some EventHandler-like syntax (like delegate void MyEventHandler(object sender, MyEventArgs e)). Then you declare your event on your class, as well as define an OnEventName method.

The naming convention follows: for an event name "MyEvent", you define a delegate and members like so:

delegate: MyEventHandler
arguments: MyEventArgs
event name: MyEvent
handler: OnMyEvent

So an example class would look like this:
public delegate void SentHandler(object sender, SentArgs e);
public class Example
{
  public event SentEventHandler Sent;
  protected virtual void OnSent(SentEventArgs e)
  {
    if (Sent != null)
      Sent(this, e);
  }
  public void Send(string message)
  {
    // ...
    OnSent(new SentEventArgs(message));
  }
}
public class SentEventArgs : EventArgs
{
  string message;
  public SentEventArgs(string message)
  {
    this.message = message;
  }
  public string Message
  {
    get { return message; }
  }
}
You define a protected virtual member named OnEventName so that derivative classes can override it (and they should call base.OnEventName) without handling the event, which require far more IL statements and doesn't allow for as much handling (like the ability to not have the base handler called, which is actually what raises the event).

This is also documented in the .NET Framework SDK.

What you want to do for a ListBox isn't at all easy. While you can extend ListBox and ListBox.ObjectCollection to override ListBox.CreateItemCollection and return your own ListBox.ObjectCollection derivative, you can't override Add, Remove, etc. While you could hide them using new, your definition won't be called because the ListBox internally refers to the ListBox.ObjectCollection class.

With member hiding, the call is not virtual. This means that in order for your implementation to be called, your class must be the class that's referred to. The following example should demonstrate better:
public class Test
{
  static void Main()
  {
    Test t = new Test();
    t.A();
    t.B();
    t = new Test2();
    t.A();
    t.B();
    ((Test2)t).A();
    ((Test2)t).B();
  }
  public void A()
  {
    Console.WriteLine("Test.A");
  }
  public virtual void B()
  {
    Console.WriteLine("Test.B");
  }
}
public class Test2 : Test
{
  public new A()
  {
    Console.WriteLine("Test2.A");
  }
  public override void B()
  {
    Console.WriteLine("Test2.B");
  }
}
This would print the following to a console:
Test.A
Test.B
Test.A
Test2.B
Test2.A
Test2.B
So, as you see, unless it's Test2.A being called instead of Test.A when hiding the member, Test.A is called. This is the difference between the call IL instruction that is executed for A() where the callvirt IL instruction is executed for B().

 

Microsoft MVP, Visual C#
My Articles
QuestionWhere is the Activitybar? Pin
matthias s.30-Jul-04 4:36
matthias s.30-Jul-04 4:36 
Questionhow can i navigate between controls on a form using enter key in c# Pin
ch_faf30-Jul-04 4:01
ch_faf30-Jul-04 4:01 
AnswerRe: how can i navigate between controls on a form using enter key in c# Pin
Michael P Butler30-Jul-04 4:21
Michael P Butler30-Jul-04 4:21 
AnswerRe: how can i navigate between controls on a form using enter key in c# Pin
Heath Stewart30-Jul-04 7:36
protectorHeath Stewart30-Jul-04 7:36 
GeneralArray´s via reflection... Pin
Norman-Timo30-Jul-04 3:09
Norman-Timo30-Jul-04 3:09 
GeneralRe: Array´s via reflection... Pin
leppie30-Jul-04 7:30
leppie30-Jul-04 7:30 
GeneralRe: Array´s via reflection... Pin
Norman-Timo1-Aug-04 19:42
Norman-Timo1-Aug-04 19:42 
GeneralConsole.writeline not working from a windows app. Pin
Admiral Ackbar30-Jul-04 3:00
Admiral Ackbar30-Jul-04 3:00 
GeneralRe: Console.writeline not working from a windows app. Pin
Wender Oliveira30-Jul-04 4:41
Wender Oliveira30-Jul-04 4:41 
GeneralRe: Console.writeline not working from a windows app. Pin
Michael P Butler30-Jul-04 4:54
Michael P Butler30-Jul-04 4:54 
GeneralRe: Console.writeline not working from a windows app. Pin
Heath Stewart30-Jul-04 7:32
protectorHeath Stewart30-Jul-04 7:32 
GeneralRe: Console.writeline not working from a windows app. Pin
Admiral Ackbar30-Jul-04 8:19
Admiral Ackbar30-Jul-04 8:19 
GeneralDrag n drop treeview Nodes between two different applications Pin
misterbear30-Jul-04 2:02
misterbear30-Jul-04 2:02 
GeneralRe: Drag n drop treeview Nodes between two different applications Pin
Heath Stewart30-Jul-04 5:58
protectorHeath Stewart30-Jul-04 5:58 
GeneralRe: Drag n drop treeview Nodes between two different applications Pin
misterbear31-Jul-04 4:59
misterbear31-Jul-04 4:59 
GeneralRe: Drag n drop treeview Nodes between two different applications Pin
Heath Stewart31-Jul-04 5:26
protectorHeath Stewart31-Jul-04 5:26 
GeneralMessage Removed Pin
30-Jul-04 1:01
wibblewibblewibble30-Jul-04 1:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.