Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2007

Tip: Looking for answers? Try searching our database.

Access to form control in an event handler

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike Thompson - 11 Sep 2007 20:45 GMT
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.
Peter Duniho - 11 Sep 2007 21:03 GMT
> [...]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.