Access to form control in an event handler

M

Mike Thompson

I have an event handler defined within a class. It has the following
signature:

static void XYZ_EventHandler (object sender, XYZEventArgs e)

From within this event handler, I want to access the contents of a textbox
on the form that is defined within the default Form1 class. Ordinarily, I
would pass the form to any class member function as an argument, but because
this is an event handler, I can't pass it.

I tried to create a new instance of Form1 within the event handler (Form1
frm = new Form1()), then access frm.textBox1.Text, but it's a new instance
of Form1 and doesn't return the contents of any change set in the text box
on the first instance of Form1.

So my question is, how can I access the contents of a textbox on the default
Form1 in an event handler defined in a class that is not part of the default
Form1 class?

The language is C#. Any help would be most appreciated. Thanks.
 
P

Peter Duniho

Mike said:
[...]
So my question is, how can I access the contents of a textbox on the default
Form1 in an event handler defined in a class that is not part of the default
Form1 class?

There are a variety of ways to do that. Which is best depends somewhat
on what the event handler is actually doing.

Is it subscribed to an event on a control that's a sibling of the
Textbox you want access to? That is, they are both on the same form?
If so, you can cast the "sender" parameter to Control and use the
Contro.FindForm() method. This will return a Form reference which you
can cast to the actual type of your form (eg "Form1"), from which you
can then get the Textbox instance in exactly the way you tried using a
new instance of the form, except of course using the correct instance. :)

If the event is unrelated to the form itself, then you'll have to access
the form instance via some other mechanism. You can do things like find
the form by the Name property from the collection returned by the
Application.OpenForms property, or simply keep an explicit reference to
the form somewhere (for example, in the class handling the event, or in
some other class accessible by that class, like perhaps a public static
member of the Program class).

Of course, if the event handler is in the form itself, then you already
have direct access to all of the members of that form from within the
event handler. :)

There are lots of different ways this could be done. It's hard to know
without more information exactly what the right way would be in your
situation, so if the above doesn't give you enough ideas to find a
solution, please post a clarification that would allow for more specific
advice.

For what it's worth, you're already most of the way there; many people
don't get past the "why doesn't the form I get with 'new Form1()' allow
me to change the text in my Textbox instance?" part. :)

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top