Solved

Access and change  control from another class.

Posted on 2010-09-17
5
648 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
[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
  • 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 500 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

740 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