• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 329
  • Last Modified:

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

        }


    }
}
0
dvplayltd
Asked:
dvplayltd
1 Solution
 
Anurag ThakurCommented:
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 TomlinsonMiddle School Assistant TeacherCommented:
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
 
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now