Static event handler doesn't like forms

From: Tim Johnson (tjohnson_at_high-point.com)
Date: 10/16/04


Date: Sat, 16 Oct 2004 12:25:03 -0700

I set up a custom event which I fire from another class upon certain extenal
circumstances, but in my form's event handler that catches the event I can't
reference my form controls:

The firing class looks like this:

public class MyStuff
{
public delegate void MyHandler(MyObject x);
public static event MyHandler MyEvent;
...
//Later fire the event as needed
MyEvent(myobjectx);
}

Over in a form I set up the handler like this:
public class MyForm : System.Windows.Forms.Form
{
    private System.Windows.Forms.ListBox mylist;

    private MyForm_Load(...)
    {
        MyStuff.MyEvent += new MyStuff.MyHandler(OnMyEvent);
    }

    public static void OnMyEvent(MyObject x)
    {
        mylist.Items.Add("Got here"); //Compiler fails on this line
    }
}

This won't compile, complaining about the reference to mylist with
"MyForm.mylist denotes a 'field' where a 'class' was expected". If I
comment it out I can get to the handler just fine in the debugger at runtime
and my data looks good. I just can't use my form's controls.

I think this might be because the OnMyEvent method is static (which I think
it has to be to be an eventhandler), even though it's part of the MyStuff
formclass? Obviously if I get an event in my form I'd like to use it in my
form controls! How do I do it? I tried "this.mylist.Items.Add" but it
doesn't like that either.

-- 
Tim Johnson
High Point Software
www.high-point.com
(503) 312-8625


Relevant Pages

  • Re: WM_QUERYENDSESSION and saving data through a worker thread
    ... By returning FALSE, you would abort the shutdown, so that is not really a viable option. ... to save was the last few change requests, and you could re-create the state directly. ... When WM_QUERYENDSESSION comes fire the thread and wait for it to end. ... Only then return TRUE from WM_QUERYENDSESSION handler. ...
    (microsoft.public.vc.mfc)
  • Problem assigning event handler to object.event
    ... public class ModeStateChangedEventArgs: EventArgs ... Gen.DataState myState; ... EventHandler<ModeStateChangedEventArgs> handler = ... Now is a list class I want to create instances of BizClass, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: DragDrop event vs. OnDragDrop(.) - whats the diff?
    ... essence, "hey, I fire event X!"), but it's up to MS to implement it. ... >> The OnDragDrop method is, in essence, a stubbed out event handler ... >> delegate for the DragDrop event. ... It does nothing, unless you override it. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Is RaiseEvent Synchronous or Asynchronous in VB .Net ??
    ... if the event returned a value how would each handler return its ... Object & EventArgs parameters and make a custom EventArgs that has the ... Public Class GetDataEventArgs ... Public Delegate Sub GetDataHandler(ByVal sender As Object, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to Remove an Event Handler without knowing the name of the handler
    ... Public Class ClassA ... AddHandler MyEvent, AddressOf ClassBInstance.MyEventHandler ... >> Is there a way to dynamically remove an event handler from an event ... >> Public Sub AddHandler ...
    (microsoft.public.dotnet.languages.vb)