Solved

Access and change  control from another class.

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
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…

860 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