Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access and change  control from another class.

Posted on 2010-09-17
5
Medium Priority
?
653 Views
Last Modified: 2013-12-16
Hello.
Say i have a Form1(). and a Textbox1 in it. Now i have a class named "myclass" in another myclass.cs file.  (myclass is public). and myclass has a function named foo()

myclass is performing some asynchronous operations in the background.  I want foo() to update
textbox1.  
I know i need to use delegates, but can you help me how to do that with a  sample ?
Thanks.
 
0
Comment
Question by:parabellum
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33700986

        private MyClass myclass;
        public Form1()
        {
            InitializeComponent();

            myclass = new MyClass();
            myclass.OperationComplete += new Action<string>(myclass_OperationComplete);
        }

        void myclass_OperationComplete(string obj)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string>(myclass_OperationComplete), new object[] {obj});
                return;
            }

            textbox1.Text = obj;
        }

    class MyClass
    {
        public event Action<string> OperationComplete;

        public void Foo()
        {

            if (OperationComplete != null)
                OperationComplete("OperationCompleted");
        }
    }

Open in new window

0
 
LVL 8

Accepted Solution

by:
Gururaj Badam earned 2000 total points
ID: 33700997
Sorry there was missing code in the first snippet
    class MyClass
    {
        public event Action<string> OperationComplete;

        public void Foo()
        {

            if (OperationComplete != null)
                OperationComplete("OperationCompleted");
        }
    }

    public partial class Form1 : Form
    {
        private MyClass myclass;
        public Form1()
        {
            InitializeComponent();

            myclass = new MyClass();
            myclass.OperationComplete += new Action<string>(myclass_OperationComplete);
        }

        void myclass_OperationComplete(string obj)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string>(myclass_OperationComplete), new object[] {obj});
                return;
            }

            textbox1.Text = obj;
        }
}

Open in new window

0
 

Author Comment

by:parabellum
ID: 33701487

Thanks. But how will i raise the event from Foo() ?
For example,  say Foo is accepting a input parameter int input:

Foo(int input).
if(input==5)
Invoke  the textbox ====> How will il invoke it ?

0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33701525
Foo is firing an event which is subscribed by Form1 through myclass_OperationComplete method. It then set the value in the required textbox.
public void Foo(int input)
        {

            if (input == 5 && OperationComplete != null)
                OperationComplete("OperationCompleted");
        }

Open in new window

0
 

Author Comment

by:parabellum
ID: 33701589
Oooh  how could i miss that ... Thanks a lot :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses

972 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