Solved

Access and change  control from another class.

Posted on 2010-09-17
5
645 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

919 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now