Re: Static event handler doesn't like forms

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Robert Levy [MS] (rlevy_at_online.microsoft.com)
Date: 10/16/04

  • Next message: Boris Nienke: "Re: .net cf sp2 problem"
    Date: Sat, 16 Oct 2004 13:02:28 -0700
    
    

    Your "OnMyEvent" is static but it refers to "mylist" which is not. All you
    have to do is either make "mylist" static or pass the control in to the
    event handler as a part of "MyObject"

    > public class MyForm : System.Windows.Forms.Form
    > {
    > private System.Windows.Forms.ListBox mylist;
    > }
    >
    ...
    >
    > public static void OnMyEvent(MyObject x)
    > {
    > mylist.Items.Add("Got here"); //Compiler fails on this line
    > }

    -- 
    Robert Levy
    Program Manager
    Mobile Devices Product Group
    http://blogs.msdn.com/windowsmobile
    This posting is provided "AS IS" with no warranties, and confers no
    rights.
    Tim Johnson wrote:
    > 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 
    

  • Next message: Boris Nienke: "Re: .net cf sp2 problem"

    Relevant Pages

    • Re: Static event handler doesnt like forms
      ... but in my form's event handler that catches the event I ... > reference my form controls: ... > public class MyForm: System.Windows.Forms.Form ...
      (microsoft.public.dotnet.framework.compactframework)
    • Re: Event Handler doesnt fire immediately upon postback?
      ... make its comparisons and work with the PostBack event handlers. ... It seems like such a waste of resources to me sometimes to use controls like ... I still prefer to do as much client-side processing as possible ... the event handler fires immediately. ...
      (microsoft.public.dotnet.framework.aspnet)
    • Re: Help me with threads.
      ... > controls from a worker thread, I disagree that dealing with this situation ... > public delegate void StringEventHandler(object worker, StringEventArgs e); ... > part, BeginInvoke needs a pointer to our event handler, plus an object ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: Help me with threads.
      ... What you need to do is to define an event that your worker thread can throw ... when it needs to send data back to the UI thread, create an event handler to ... sure that you're in the correct thread before you update your UI controls. ... StringEventArgs e); ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: Event Handler doesnt fire immediately upon postback?
      ... ASP.Net programming model, coming from Classic ASP, which is entirely ... Controls within Controls in the Page, which is, itself, a Control. ... previous state (prior to the PostBack), and only then does the Page (and ... the event handler fires immediately. ...
      (microsoft.public.dotnet.framework.aspnet)