Mike Thompson wrote:
[...]
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