Solved

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

Posted on 2007-04-08
7
1,011 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
ID: 18872418
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
ID: 18872448
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
ID: 18872467
Good point.  
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 4

Expert Comment

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

Dim Value As String = mytextbox.text
0
 
LVL 4

Expert Comment

by:Michael Krumpe
ID: 18877864
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 18878067
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
ID: 18880477
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

20 Experts available now in Live!

Get 1:1 Help Now