Solved

Access and change  control from another class.

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

12 Experts available now in Live!

Get 1:1 Help Now