Solved

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

Posted on 2007-04-08
7
1,014 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

839 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