Re: Static event handler doesn't like forms
From: Robert Levy [MS] (rlevy_at_online.microsoft.com)
Date: 10/16/04
- Previous message: Tim Johnson: "Re: MessageWindow question"
- In reply to: Tim Johnson: "Static event handler doesn't like forms"
- Next in thread: Alex Feinman [MVP]: "Re: Static event handler doesn't like forms"
- Messages sorted by: [ date ] [ thread ]
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
- Previous message: Tim Johnson: "Re: MessageWindow question"
- In reply to: Tim Johnson: "Static event handler doesn't like forms"
- Next in thread: Alex Feinman [MVP]: "Re: Static event handler doesn't like forms"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|