Solved

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

Posted on 2007-04-08
7
1,018 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 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 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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

734 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