Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-04-08
7
Medium Priority
?
1,020 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 2

Accepted Solution

by:
virsum earned 1000 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 86

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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 4

Expert Comment

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

Dim Value As String = mytextbox.text
0
 
LVL 86

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

664 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