I have a form code behind and it has what are called Common Utilities.  Basically the original author made a super class that is embedded into the main form code behind.  What is the best way to refactor this?  And what methods can stay in the main form?  I think my issue is in each of these methods there are references to form objects.  For instance the LoadComboBoxes method... It makes a database call to another method that populates the comboboxes from the database... Would I, on form load, send a list of comboboxes from the form to this other class, operate on the objects and then send them back?  Here is the code: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;using System.Xml;using System.Xml.Linq;using Connolly.Lib.Controls;namespace VendorManager{    public partial class MainForm : Form    {        #region Declarations        private string updateXML;        private EmployeeEditMode editMode;        private bool rehire;        private string originalUserName;        private string originalType;        private enum EmployeeEditMode        {            Locked = 0,            NewEmployee = 1,            EditEmployee = 2        }        VM_DatabaseUtilities objConnect;        string conString;        string sqlStatement;        DataSet ds;        DataRow dRow;        int maxRows;        int inc = 0;        #endregion        #region FormEvents        private void MainForm_Load(object sender, EventArgs e)        {            cmdSave.Top = cmdEdit.Top;            cmdCancel.Top = cmdNew.Top;            cmdDuplicate.Top = cmdRehire.Top;            editMode = EmployeeEditMode.Locked;            ToggleFormObjects();            LoadComboBoxes();            LoadEmployeeList();        }        public MainForm()        {            InitializeComponent();        }        private void menuExit_Click(object sender, EventArgs e)        {            Application.Exit();        }        private void menuAbout_Click(object sender, EventArgs e)        {            CommonUtilities aboutConnolly = new CommonUtilities();            aboutConnolly.ShowHelpWindow(VendorManager.Properties.Settings.Default.version.ToString(), true);        }        private void cmdSave_Click(object sender, EventArgs e)        {            XmlUtilities xml = new XmlUtilities();            XDocument X = new XDocument();            Associate updateAssociate = new Associate();            updateAssociate = BuildAssociate();            X = xml.UpdateEmployeeXml(updateAssociate, updateXML);            DatabaseCall(3, X.ToString());            LoadAssociate(updateAssociate.AssocID);            if ((txtTerminationDate.Text ?? string.Empty).Length == 0)                radActive.Checked = true;            else                radTerminated.Checked = true;            cboTypeSelect.SelectedValue = cboType.SelectedValue;            LoadEmployeeList();            lstEmployees.SelectedValue = updateAssociate.AssocID;            LoadAssociate(lstEmployees.SelectedValue.ToString());        }        private void cmdEdit_Click(object sender, System.EventArgs e)        {            if (lstEmployees.SelectedValue.ToString().Length > 0)             {                editMode = EmployeeEditMode.EditEmployee;                ToggleFormObjects();            }        }        private void cboTypeSelect_SelectionChangeCommitted(object sender, System.EventArgs e)        {            int filter = Int32.Parse(cboTypeSelect.SelectedValue.ToString());            LoadEmployeeList(filter.ToString());            cboTypeSelect.SelectedValue = filter;        }        private void radioButton1_CheckedChanged(object sender, EventArgs e)        {            string filter = cboTypeSelect.SelectedValue.ToString();            DatabaseCall(0, );            if (filter.ToString() != VendorManager.Properties.Settings.Default.allEmployees)                DatabaseCall(1, filter.ToString());            else                DatabaseCall(1, );            cboTypeSelect.SelectedValue = filter;        }        private void lstEmployees_Click(object sender, EventArgs e)        {            if (lstEmployees.SelectedValue.ToString().Length > 0)                LoadAssociate(lstEmployees.SelectedValue.ToString());        }        #endregion        #region CommonUtilities        private void LoadComboBoxes()        {            try            {                DatabaseCall(0, );            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }        }        private void LoadEmployeeList(string filters)        {            try            {                DatabaseCall(1, filters);                if (lstEmployees.SelectedIndex < 0)                {                    ClearAssociate();                    cmdEdit.Enabled = false;                    cmdDuplicate.Enabled = false;                }                else                {                    LoadAssociate(lstEmployees.SelectedValue.ToString());                    cmdEdit.Enabled = true;                    cmdDuplicate.Enabled = true;                }            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }        }        private void LoadAssociate(string assocID)        {            CommonUtilities xmlEncoding = new CommonUtilities();            updateXML = xmlEncoding.VM_EmployeeUpdateXML();            DatabaseCall(2, assocID);            if (dRow != null)            {                txtAssocId.Text = dRow[AssocID].ToString() ?? string.Empty;                txtFname.Text = dRow[FName].ToString() ?? string.Empty;                txtLname.Text = dRow[LName].ToString() ?? string.Empty;                txtHireDate.Text = dRow[HireDate].ToString() ?? string.Empty;                txtContractEnd.Text = dRow[ContractDte].ToString() ?? string.Empty;                txtTerminationDate.Text = dRow[TerminationDate].ToString() ?? string.Empty;                txtUserName.Text = dRow[UserName].ToString() ?? string.Empty;                cboTitle.SelectedValue = dRow[TitleID].ToString() ?? string.Empty;                cboType.SelectedValue = dRow[EmpStatusID].ToString() ?? string.Empty;                cboPRole.SelectedValue = dRow[PrimaryRole].ToString() ?? string.Empty;                cboLocation.SelectedValue = dRow[LocationID].ToString() ?? string.Empty;                cboSuperPrincipal.SelectedValue = dRow[SuperPrincipal].ToString() ?? string.Empty;                cboCompany.SelectedValue = dRow[CoNum].ToString() ?? string.Empty;                cboLOB.SelectedValue = dRow[LOBID].ToString() ?? string.Empty;                cboDomain.SelectedValue = dRow[SystemDomain].ToString() ?? W;                cboFullPart.SelectedValue = dRow[FullPart].ToString() ?? 0;                txtNotes.Text = dRow[Notes].ToString() ?? string.Empty;                txtFirmName.Text = dRow[FirmName].ToString() ?? string.Empty;                txtAdpId.Text = dRow[AdpId].ToString() ?? string.Empty;                originalUserName = txtUserName.Text ?? string.Empty;                originalType = cboType.SelectedValue.ToString() ?? string.Empty;                updateXML = updateXML.Replace({ADP}, dRow[AdpId].ToString() ?? string.Empty);                updateXML = updateXML.Replace({AssocID}, dRow[AssocId].ToString() ?? string.Empty);                updateXML = updateXML.Replace({oFName}, xmlEncoding.XMLEncode(dRow[FName].ToString() ?? string.Empty));                updateXML = updateXML.Replace({oLName}, xmlEncoding.XMLEncode(dRow[LName].ToString() ?? string.Empty));                updateXML = updateXML.Replace({oSupervisor}, dRow[SuperPrincipal].ToString() ?? string.Empty);                updateXML = updateXML.Replace({oTitleID}, dRow[titleID].ToString() ?? string.Empty);                updateXML = updateXML.Replace({oLocation}, cboLocation.SelectedValue == null ? string.Empty : cboLocation.SelectedValue.ToString());                updateXML = updateXML.Replace({oPR}, cboPRole.SelectedValue == null ? string.Empty : cboPRole.SelectedValue.ToString());                updateXML = updateXML.Replace({oEmpType}, cboType.SelectedValue == null ? string.Empty : cboType.SelectedValue.ToString());                updateXML = updateXML.Replace({oTermDate}, dRow[TerminationDate].ToString() ?? 1900-01-01T00:00:00);                updateXML = updateXML.Replace({oCoNum}, dRow[CoNum].ToString() ?? string.Empty);                updateXML = updateXML.Replace({oNotes}, xmlEncoding.XMLEncode(dRow[Notes].ToString() ?? string.Empty));                updateXML = updateXML.Replace({oStartDate}, dRow[HireDate].ToString() ?? 1900-01-01T00:00:00);                updateXML = updateXML.Replace({ICContractEnd}, dRow[ContractDte].ToString() ?? 1900-01-01T00:00:00);                updateXML = updateXML.Replace({oFirmName}, xmlEncoding.XMLEncode(dRow[FirmName].ToString() ?? string.Empty));                updateXML = updateXML.Replace({oFT}, dRow[FullPart].ToString() ?? 0);                updateXML = updateXML.Replace({oLOB}, dRow[LOBID].ToString() ?? 0);                updateXML = updateXML.Replace({oSystemDomain}, dRow[SystemDomain].ToString() ?? W);                updateXML = updateXML.Replace({oUserName}, dRow[UserName].ToString() ?? string.Empty);            }            //BuildAssociate();        }        private void ClearAssociate()        {            txtAssocId.Text = null;            txtFname.Text = null;            txtLname.Text = null;            txtHireDate.Text = null;            txtContractEnd.Text = null;            txtTerminationDate.Text = null;            txtAdpId.Text = null;            txtUserName.Text = null;            cboTitle.SelectedIndex = -1;            cboType.SelectedIndex = -1;            cboPRole.SelectedIndex = -1;            cboLocation.SelectedIndex = -1;            cboSuperPrincipal.SelectedIndex = -1;            cboCompany.SelectedIndex = -1;            cboLOB.SelectedIndex = -1;            cboDomain.SelectedIndex = -1;            cboFullPart.SelectedIndex = -1;            txtNotes.Text = null;            txtFirmName.Text = null;        }        private void ToggleFormObjects()        {            bool isLocked = false;            lstEmployees.Focus();            cmdEdit.Visible = false;            cmdNew.Visible = false;            cmdSave.Visible = false;            cmdCancel.Visible = false;            cmdRehire.Visible = false;            cmdDuplicate.Visible = false;            switch (editMode)            {                case EmployeeEditMode.Locked:                    {                        isLocked = true;                        cmdEdit.Visible = true;                        cmdNew.Visible = true;                        cmdDuplicate.Visible = txtTerminationDate.Text.Length == 0;   //setup isnull type of thing                        break;                    }                case EmployeeEditMode.NewEmployee:                    {                        cmdSave.Visible = true;                        cmdCancel.Visible = true;                        break;                    }                case EmployeeEditMode.EditEmployee:                    {                        cmdSave.Visible = true;                        cmdCancel.Visible = true;                        cmdRehire.Visible = true;                        break;                    }            }            lstEmployees.Enabled = isLocked;            radActive.AutoCheck = isLocked;            radTerminated.AutoCheck = isLocked;            cboTypeSelect.Enabled = isLocked;            txtAdpId.Enabled = !isLocked;            txtAssocId.Enabled = !isLocked;            txtFname.Enabled = !isLocked;            txtLname.Enabled = !isLocked;            txtHireDate.Enabled = !isLocked;            txtTerminationDate.ReadOnly = (isLocked == true || cboTypeSelect.SelectedText.ToString() == Regular);            cboSuperPrincipal.Enabled = !isLocked;            txtContractEnd.Enabled = !isLocked;            cboCompany.Enabled = !isLocked;            cboTitle.Enabled = !isLocked;            cboLocation.Enabled = !isLocked;            cboType.Enabled = !isLocked;            cboPRole.Enabled = !isLocked;            cboLOB.Enabled = !isLocked;            cboFullPart.Enabled = !isLocked;            txtNotes.Enabled = !isLocked;            txtFirmName.Enabled = !isLocked;            txtUserName.Enabled = editMode == EmployeeEditMode.NewEmployee ? true : !isLocked;            cboDomain.Enabled = !isLocked;        }        private void DatabaseCall(int queryNumber, string filter)        {            objConnect = new VM_DatabaseUtilities();            if (queryNumber < 3)                conString = Properties.Settings.Default.ccaDatabaseConnectionString_DEV;            else                conString = Properties.Settings.Default.corpIntegrationDatabaseConnectionString_DEV;            objConnect.connectionString = conString;            //BindingList<DropdownValues> ddVals = new BindingList<DropdownValues>();            switch (queryNumber)            {                //Fill All Dropdowns                case 0:                    {                        //Fill Domain Dropdown                        cboDomain.DisplayMember = Text;                        cboDomain.ValueMember = Value;                        var domainItems = new[] {                             new { Text = Wilton, Value = W },                             new { Text = Atlanta, Value = A },                         };                        cboDomain.DataSource = domainItems;                        //Fill Full / Part Time Dropdown                        cboFullPart.DisplayMember = Text;                        cboFullPart.ValueMember = Value;                        var FullPartItems = new[] {                             new { Text = Part Time, Value = 0 },                             new { Text = Full Time, Value = 1 },                         };                        cboFullPart.DataSource = FullPartItems;                        //Fill Type Selector Dropdown                        sqlStatement = Properties.Settings.Default.cboTypeSelectRowSource;                        objConnect.sqlQuery = sqlStatement;                        ds = objConnect.GetConnection;                        cboTypeSelect.DataSource = ds.Tables[Table_Data_1];                        cboTypeSelect.ValueMember = EmpStatusID;                        cboTypeSelect.DisplayMember = EmpStatusDesc;                        //Fill Company Dropdown                        sqlStatement = Properties.Settings.Default.cboCompanyRowSource;                        objConnect.sqlQuery = sqlStatement;                        ds = objConnect.GetConnection;                        cboCompany.DataSource = ds.Tables[Table_Data_1];                        cboCompany.ValueMember = CoNum;                        cboCompany.DisplayMember = CoName;                        //Fill LOB Dropdown                        sqlStatement = Properties.Settings.Default.cboLOBRowSource;                        objConnect.sqlQuery = sqlStatement;                        ds = objConnect.GetConnection;                        cboLOB.DataSource = ds.Tables[Table_Data_1];                        cboLOB.ValueMember = CcaLOBID;                        cboLOB.DisplayMember = GPLOB;                        //Fill Title Dropdown                        sqlStatement = Properties.Settings.Default.cboTitleRowSource;                        objConnect.sqlQuery = sqlStatement;                        ds = objConnect.GetConnection;                        cboTitle.DataSource = ds.Tables[Table_Data_1];                        cboTitle.ValueMember = TitleID;                        cboTitle.DisplayMember = TitleName;                        //Fill Type Dropdown                        sqlStatement = Properties.Settings.Default.cboTypeRowSource;                        objConnect.sqlQuery = sqlStatement;                        ds = objConnect.GetConnection;                        cboType.DataSource = ds.Tables[Table_Data_1];                        cboType.ValueMember = EmpStatusID;                        cboType.DisplayMember = EmpStatusDesc;                        //Fill PrimaryRole Dropdown                        sqlStatement = Properties.Settings.Default.cboPRoleRowSource;                        objConnect.sqlQuery = sqlStatement;                        ds = objConnect.GetConnection;                        cboPRole.DataSource = ds.Tables[Table_Data_1];                        cboPRole.ValueMember = AssocType;                        cboPRole.DisplayMember = AssocDesc;                        //Fill Location Dropdown                        sqlStatement = Properties.Settings.Default.cboLocationRowSource;                        objConnect.sqlQuery = sqlStatement;                        ds = objConnect.GetConnection;                        cboLocation.DataSource = ds.Tables[Table_Data_1];                        cboLocation.ValueMember = LocationID;                        cboLocation.DisplayMember = LocationDesc;                        //Fill SuperPrincipal Dropwdown                        sqlStatement = Properties.Settings.Default.cboSuperPrincipalRowSource;                        objConnect.sqlQuery = sqlStatement;                        ds = objConnect.GetConnection;                        cboSuperPrincipal.DataSource = ds.Tables[Table_Data_1];                        cboSuperPrincipal.ValueMember = AssocId;                        cboSuperPrincipal.DisplayMember = Name;                        break;                    }                //Fill Employee List                case 1:                    {                        if (filter != )                            if (radActive.Checked)                                filter = Properties.Settings.Default.activeFilter                                    .PadLeft(Properties.Settings.Default.activeFilter.Length + 1)                                    .PadRight(Properties.Settings.Default.activeFilter.Length + 1) + filter;                            else                                filter = Properties.Settings.Default.termedFilter                                    .PadLeft(Properties.Settings.Default.termedFilter.Length + 1)                                    .PadRight(Properties.Settings.Default.termedFilter.Length + 1) + filter;                        else                            if (radActive.Checked)                                filter = Properties.Settings.Default.activeNoFilter                                    .PadLeft(Properties.Settings.Default.activeNoFilter.Length + 1);                            else                                filter = Properties.Settings.Default.termedNoFilter                                    .PadLeft(Properties.Settings.Default.termedNoFilter.Length + 1);                        sqlStatement = Properties.Settings.Default.loadEmployeeList + filter +  ORDER BY EmployeeName ASC;;                        objConnect.sqlQuery = sqlStatement;                        ds = objConnect.GetConnection;                        lstEmployees.DataSource = ds.Tables[Table_Data_1];                        lstEmployees.ValueMember = AssocId;                        lstEmployees.DisplayMember = EmployeeName;                        break;                    }                //Load individual employee data                case 2:                    {                        sqlStatement = Properties.Settings.Default.loadAssociatesRowSource + filter + ';                        objConnect.sqlQuery = sqlStatement;                        ds = objConnect.GetConnection;                        //No row means no employee selected - just exit                        if (ds.Tables[0].Rows.Count > 0)                            dRow = ds.Tables[0].Rows[0];                        break;                    }                //Execute UpdateAssociate Sproc                case 3:                    {                        bool isUpdated;                        sqlStatement = IndependentContractor.EmployeeUpdate;                        isUpdated = objConnect.VM_ExecuteSproc(sqlStatement, filter);                        break;                    }                //Execute NewAssociate Sproc                case 4:                    {                        break;                    }            }            maxRows = ds.Tables[0].Rows.Count;        }        private Associate BuildAssociate()        {            Associate associate = new Associate();            DateTime dateHolder;            associate.AssocID = txtAssocId.Text ?? string.Empty;            associate.FirstName = txtFname.Text ?? string.Empty;            associate.LastName = txtLname.Text ?? string.Empty;            associate.EmpNotes = txtNotes.Text ?? string.Empty;            associate.AdpID = txtAdpId.Text ?? string.Empty;            associate.FirmName = txtFirmName.Text ?? string.Empty;            associate.UserName = txtUserName.Text ?? string.Empty;            associate.FullPartTimeID = cboFullPart.SelectedValue == null ? string.Empty : cboFullPart.SelectedValue.ToString();            associate.PrimaryRole = cboPRole.SelectedValue == null ? string.Empty : cboPRole.SelectedValue.ToString();            associate.SystemDomain = cboDomain.SelectedValue == null ? string.Empty : cboDomain.SelectedText ?? string.Empty;            associate.CoNum = cboCompany.SelectedValue == null ? string.Empty : cboCompany.SelectedValue.ToString();            associate.LobID = cboLOB.SelectedValue == null ? string.Empty : cboLOB.SelectedValue.ToString();            associate.Location = cboLocation.SelectedValue == null ? string.Empty : cboLocation.SelectedValue.ToString();            associate.TitleID = cboTitle.SelectedValue == null ? string.Empty : cboTitle.SelectedValue.ToString();            associate.SuperPrincipal = cboSuperPrincipal.SelectedValue == null ? string.Empty : cboSuperPrincipal.SelectedValue.ToString();            associate.EmployeeTypeID = cboType.SelectedValue == null ? string.Empty : cboType.SelectedValue.ToString();            if (!string.IsNullOrEmpty(txtHireDate.Text))            {                DateTime.TryParse(txtHireDate.Text, out dateHolder);                associate.HireDate = dateHolder.ToString(o);            }            else            {                associate.HireDate = 1900-01-01T00:00:00;            }            if (!string.IsNullOrEmpty(txtContractEnd.Text))            {                DateTime.TryParse(txtContractEnd.Text, out dateHolder);                associate.ContractEndDate = dateHolder.ToString(o);            }            else            {                associate.ContractEndDate = 1900-01-01T00:00:00;            }            if (!string.IsNullOrEmpty(txtTerminationDate.Text))            {                DateTime.TryParse(txtTerminationDate.Text, out dateHolder);                associate.TerminationDate = dateHolder.ToString(o);            }            else            {                associate.TerminationDate = 1900-01-01T00:00:00;            }            return associate;            }        #endregion    } }
UI code-behind for managing employees
c#;.net;winforms