Solved

Referencing a Form's control from a static function in another class

Posted on 2007-04-08
7
1,008 Views
Last Modified: 2013-11-07
So, I just stumped myself here.

Basically I have a static function in a class which needs to add text to a TextBox in my main form. Can anyone suggest how I would go about referencing that TextBox control from the static class?
0
Comment
Question by:Esulin
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 2

Accepted Solution

by:
virsum earned 250 total points
Comment Utility
Several thoughts here:

1.  Pass the form into the static method as an argument like so:
using System;
using System.Windows.Forms;

namespace FormControl
{
    public class StaticClass
    {
        public static void FillTextBox(Form frm)
        {            
            TextBox txt = (TextBox)frm.Controls["TextBox1"];
            txt.Text = "Hi there";
        }
    }
}

2.  Pass the TextBox control itself into the static method (same as above only without the first line of the method).

3.  Get the open form using the Application.OpenForms FormsCollection (NOTE:  the form must be shown for this to work.  I tried calling the static method from the form's constructor but since the form wasn't loaded yet it didn't show up in the FormsCollection yet):
using System;
using System.Windows.Forms;

namespace FormControl
{
    public class StaticClass
    {
        public static void FillTextBox()
        {
            Form frm = Application.OpenForms["Form1"];
            TextBox txt = (TextBox)frm.Controls["TextBox1"];
            txt.Text = "Hi there";
        }
    }
}


Hope this helps.  Happy Sunday.

TheManFromSql

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Or go the other way and make the class raise a custom event that the form traps and then uses to update the textbox.
0
 
LVL 2

Expert Comment

by:virsum
Comment Utility
Good point.  
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

by:Michael Krumpe
Comment Utility
Dim mytextbox As TextBox = Form.FindControl("mycontrolid")

Dim Value As String = mytextbox.text
0
 
LVL 4

Expert Comment

by:Michael Krumpe
Comment Utility
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Yes mkrumpe...but in your code you ALREADY have a reference to the form itself (called "Form").  Esulin is working from a different class so he doesn't have "Form"...thus the lengthy post by visrum.


0
 

Author Comment

by:Esulin
Comment Utility
Thanks virusm and idle mind for the great feedback. Method 1 looked like a winner, but I had a problem with the controls collection only returning one member, while in debug it was clear that all the controls that I was looking for where in fact in the collection.

When I was researching the code to find the problem I came across an even better solution and I created an interface which I then inherited to the form I was trying to access. This works like a beauty.

Again, thanks for the help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now