Receive event in class from form in C#

Posted on 2008-11-08
Last Modified: 2012-06-21
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


Question by:dvplayltd
    LVL 26

    Expert Comment

    by:Anurag Thakur
    delegates and events are the way to go forward here
    have a look at the following link here
    LVL 85

    Accepted Solution

    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


    Author Closing Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    759 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

    10 Experts available now in Live!

    Get 1:1 Help Now