Solved

Access and change  control from another class.

Posted on 2010-09-17
5
649 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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