473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Access to form control in an event handler

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.

Sep 11 '07 #1
1 2835
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
Sep 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
5
by: Andrei Pociu | last post by:
Inside a method some controls are created at runtime. I added an event handler to one of these controls: tracker.Scroll += new System.EventHandler(this.tracker_Scroll); So in the event handler...
5
by: Dave | last post by:
How do I check in a Windows Forms app if any controls have changed? I have a form that collects data, and I want to prompt the user if they try to exit the app, or load a new file, without saving...
9
by: Anders K. Jacobsen [DK] | last post by:
Hi I have this that adds some usercontrol (UCTodays.ascx) to a placeholder foreach(A a in B){ UCTodays ucline = (UCTodays )LoadControl("UCTodays.ascx");...
2
by: Bernie Yaeger | last post by:
I'm launching one window from another. I have a close button in the first window and when I click it, it executes me.close. However, the form also has a treeview control. If I launch the second...
1
by: Mark Hoffy | last post by:
Hello, I have an MDI app. There are many child forms and they all have several things in common. They all have a Save button, a public variable boolNavigate, and they all have a TreeView...
19
by: zacks | last post by:
I have a .NET 2.0 MDI application where the child form has a Tab Control. Each of the Tab in the Tab Control has a Validating event to handle what it should do when the user changes tabs. But...
3
by: bsturg21 | last post by:
Hello, I have a windows form that has a series of linklabels on it, and I need to have each linklabel, when clicked, open a separate windows form that has a single paramter passed into it. The...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.