[dba-VB] C#: Raising events

Gustav Brock Gustav at cactus.dk
Mon Jan 25 13:38:38 CST 2010


Hi John

I lost you too somewhere down the line ...
However, here's an example where a manual update at one form (Main) raises an event which requeries another form (Employees). Perhaps that can guide you a little. It writes to the console too but for debugging only. Some code has been snipped. Note the custom attributes.

<FormMain.cs>
using System;
using System.IO;
using System.Windows.Forms;

namespace DLMail
{

    public partial class FormMain : Form
    {
        public DataSetDL DlDataSet = new DataSetDL();
        public FormEmployer EmployerForm;
        public FormEmployee EmployeeForm;
        public FormPaySlip PaySlipForm;
        public FormSystem SystemForm;

        private string _dropFolder = @"Drop";
        private string _dropPath;
        private string _execPath;
        private string _dataPath;
        private string _dataFileName = @"DLMail.xml";
        private string _dataBaseFile;
        private int _employerId = -1;
        private string _employerName = String.Empty;
        private string _pdfPassword = String.Empty;

        public string ExecPath
        {
            get { return _execPath; }
        }

        public string DataPath
        {
            get { return _dataPath; }
        }

        public string DataBaseFile
        {
            get { return _dataBaseFile; }
        }

        /// <summary>
        /// The path to the folder where to copy new pay slip PDF files.
        /// </summary>
        public string DropPath
        {
            get { return _dropPath; }
        }

        public int EmployerId
        {
            get { return _employerId; }
        }

        public string EmployerName
        {
            get { return _employerName; }
        }

        public string PdfPassword
        {
            get { return _pdfPassword; }
        }

        // Delegate declaration.
        public delegate void ChangingHandler(object sender, EmployerArgs employerArgs);
        // Event declaration.
        public event ChangingHandler EmployerChanged;

        public void ChangeEmployer()
        {
            // Call the event.
            EmployerArgs employerArgs = new EmployerArgs(_employerId, _employerName, _pdfPassword, _dataPath);
            // Running on Windows Vista, first time run of EmployerChanged 
            // raises a null reference error.
            try
            {
                EmployerChanged(this, employerArgs);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Source);
            }
        }    

        public FormMain()
        {
            InitializeComponent();
            InitializeDataSet();
            InitializeFolders();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            FillEmployerComboBox();
            this.EmployerChanged += new ChangingHandler(FormMain_EmployerChanged);
        }

        private void InitializeDataSet()
        {
            _execPath = Path.GetDirectoryName(Application.ExecutablePath);
            _dataBaseFile = Path.Combine(_execPath, _dataFileName);
            if (!File.Exists(_dataBaseFile))
            {
               //snip
            }
            else
            {
                DlDataSet.ReadXml(_dataBaseFile);
                DataSetDL .DataTablePaySlipTypeDataTable paySlipTypeDataTable = DlDataSet .DataTablePaySlipType ;
            }
        }

        private void InitializeFolders()
        {
            string dropPath = Path.Combine(_execPath, _dropFolder);
            //snip
            _dropPath = dropPath;
        }

        private void FillEmployerComboBox()
        {
            int selectedIndex = -1;
            if (EmployerComboBox.SelectedIndex > -1)
            {
                selectedIndex = EmployerComboBox.SelectedIndex;
            }
            DataSetDL.DataTableEmployerDataTable dataTableEmployer = DlDataSet.DataTableEmployer;
            EmployerComboBox.SelectedItem = "Id";
            EmployerComboBox.DisplayMember = "Organisation";
            EmployerComboBox.DataSource = dataTableEmployer;
            if (selectedIndex != -1)
            {
                EmployerComboBox.SelectedIndex = selectedIndex;
            }
        }

        private void EmployerComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox comboBox = (ComboBox)sender;
            if (comboBox.SelectedItem != null)
            {
                int employerId = Convert.ToInt32(comboBox.SelectedValue);
                if (!employerId.Equals(_employerId))
                {
                    _employerId = employerId;
                    _employerName = comboBox.Text;
                    SetDataPath();
                    // Raise event.
                    this.ChangeEmployer();
                }
            }
        }

        void FormMain_EmployerChanged(object sender, EmployerArgs employerArgs)
        {
            Console.WriteLine("Change: " + employerArgs.EmployerId().ToString());
        }

        private void SetDataPath()
        {
            DataSetDL.DataTableEmployerRow employerRow = DlDataSet.DataTableEmployer.FindById(_employerId);
            if (employerRow.IsPdfPasswordNull())
            {
                _pdfPassword = string.Empty;
            }
            else
            {
                _pdfPassword = employerRow.PdfPassword.Trim();
            }
            _dataPath = Path.Combine(_execPath, employerRow.DataPath);
        }

        private void EmployeeButton_Click(object sender, EventArgs e)
        {
            if (EmployeeForm == null)
            {
                EmployeeForm = new FormEmployee();
                EmployeeForm.Show();
                EmployeeForm.FormClosed += new FormClosedEventHandler(EmployeeForm_FormClosed);
            }
            else
            {
                EmployeeForm.Focus();
            }
        }

        private void EmployeeForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            EmployeeForm = null;
            SaveData();
        }

        private void SaveData()
        {
            this.DlDataSet.WriteXml(_dataBaseFile);
        }

    }

    // Custom attributes.
    public class EmployerArgs : System.EventArgs
    {
        private int employerId = 0;
        private string employerName = String.Empty;
        private string pdfPassword = String.Empty;
        private string dataPath = String.Empty;

        public EmployerArgs(int id, string name, string password, string path)
        {
            this.employerId = id;
            this.employerName = name;
            this.pdfPassword = password;
            this.dataPath = path;
        }

        public int EmployerId()
        {
            return employerId;
        }

        public string EmployerName()
        {
            return employerName;
        }

        public string PdfPassword()
        {
            return pdfPassword;
        }

        public string DataPath()
        {
            return dataPath;
        }
    } 

}
</FormMain.cs>

<FormEmployee.cs>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DLMail
{
    public partial class FormEmployee : Form
    {
        private string _dataBaseFile;
        private int _employerId;
        
        public FormEmployee()
        {
            InitializeComponent();
            InitializeDataSet();
        }

        private void InitializeDataSet()
        {
            FormMain formMain = (FormMain)Application.OpenForms[0];
            _dataBaseFile = formMain.DataBaseFile;
            _employerId = formMain.EmployerId;
            this.dataSetDL = formMain.DlDataSet;
            ChangeEmployer();
            formMain.EmployerChanged += new FormMain.ChangingHandler(formMain_EmployerChanged);
        }

        void formMain_EmployerChanged(object sender, EmployerArgs formArgs)
        {
            _employerId = formArgs.EmployerId();
            ChangeEmployer();
        }

        private void ChangeEmployer()
        {
            string filter = String.Empty;
            // Select only employees of the selected employer.
            filter = "EmployerId = " + _employerId.ToString();
            this.dataTableEmployeeBindingSource.Filter = filter;
            this.dataTableEmployeeBindingSource.DataSource = this.dataSetDL;
        }

    }
}
</FormEmployee.cs>

/gustav


>>> jwcolby at colbyconsulting.com 25-01-2010 17:50:02 >>>
Silly me, I thought that events would be easy in C#.  I guess once you figure them out they probably
are, but coming from VBA where all the behind the scenes stuff is done for you, it is kinda confusing.

What I want to do is execute stored procedures, and publish status information about each SP
executed on my form starts a process running.





More information about the dba-VB mailing list