Receive event in class from form in C#

Hi experts!

I created WinForm application with C# 2008. I have one WinForm on which I have control (FarPoint Grid) and one class. Here I have two question:

1.      How to refer control from form to class, so when I change some property of control in class  to see this changed in Windows Form. Control is from type Spread.

2.      I need events like Leave, TextChanged for this control to be received in class automatically. Of course I can received this event in form and then to call function in class, but I will have many, many forms which will used this class and want to avoid to write code for this event in form which will actually do in class.



Here some code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using FarPoint.Win.Spread;


namespace AC
{
    public class DataSpread
    {

        public Form frm;
        public Panel pa;
        public FpSpread grd;

        public Boolean mbFormView;
        public Boolean mbGridView;

        public int lCurrentRow;

        public void Init(FpSpread grdInit,  Panel fraTab,Form frmQSearch,string sDataObjectName,string sTable,string sPrimaryKey,string sreqFields, bool bFormView, Label lblRecordCount)
        {
       
            grd = grdInit; // Will this work for question 1

        }


    }
}
dvplayltdAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anurag ThakurTechnical ManagerCommented:
delegates and events are the way to go forward here
have a look at the following link here http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_23736959.html
0
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Yes that will work...assuming created the Instance of DataSpread in your Form and then called Init() and passed in the correct references for the Controls from the Form.

In your Init() method, simply "wire up" the grid.

Here is a simplified example using a TextBox:
(I typed "tb.TextChanged +=" and then hit Tab, Tab and the method stub was added to the class for me)
namespace AC
{
    public class DataSpread
    {
 
        public TextBox tb;
 
        public void Init(TextBox tb)
        {
            this.tb = tb;
            tb.TextChanged += new EventHandler(tb_TextChanged);
        }
 
        void tb_TextChanged(object sender, EventArgs e)
        {
            
        }
        
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dvplayltdAuthor Commented:
It work! It work ! Thank you very much ! P.S: I'm from 8 years professional programmer, but new to C# and such help are very needed to me.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.