Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a form with alot of textboxes i am trying to loop controls in form to detect if textbox,combobox changes befor colsing form but not work!!
C#
 public partial class PatientFiles : Form, ILookup
    {
      bool NeedSaving = false;
      void CheckChanges(Control.ControlCollection cc)
    {
       foreach (Control ctrl in cc)
    {
        MaskedTextBox mtxtBox = ctrl as MaskedTextBox;
        TextBox txtBox = ctrl as TextBox;
        ComboBox cmb = ctrl as ComboBox;

        mtxtBox.TextChanged += new EventHandler(this.TextWasChanged);
        txtBox.TextChanged += new EventHandler(this.TextWasChanged);
        cmb.SelectedIndexChanged += new EventHandler(this.TextWasChanged);
        //CheckChanges(ctrl.Controls);

    }

}
  //formload
  private void frmPatient_Load(object sender, EventArgs e)
{
    EnableNavigation();
    //txtEngName.TextChanged += new EventHandler(TextWasChanged);
    CheckChanges(this.Controls);

}` 

 public void TextWasChanged(object sender, EventArgs e)
{
    NeedSaving = true;
}`

private void PatientFiles_FormClosing(object sender, FormClosingEventArgs e)
{
    //NeedSaving();
    // Disable Navigation On Form closing
    if (NeedSaving)
    {

        DialogResult dt = MessageBox.Show("Save Changes", "information", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
        if (dt == DialogResult.Yes)
        {
            SaveData();
            //DisableNavigation();
        }

        else if (dt == DialogResult.No)
        {

            DisableNavigation();
            NeedSaving = false;
            this.Close();
        }
        else if (dt == DialogResult.Cancel)

            e.Cancel = true;
    }

}
Posted
Comments
Kornfeld Eliyahu Peter 10-Aug-15 9:34am    
Handle Change event...

1 solution

C#
MaskedTextBox mtxtBox = ctrl as MaskedTextBox;
TextBox txtBox = ctrl as TextBox;
ComboBox cmb = ctrl as ComboBox;

mtxtBox.TextChanged += new EventHandler(this.TextWasChanged);
txtBox.TextChanged += new EventHandler(this.TextWasChanged);
cmb.SelectedIndexChanged += new EventHandler(this.TextWasChanged);


A control will not be both a MaskedTextBox, a TextBox and a ComboBox at the same time. Two of those variables will always be null, so you'll get a NullReferenceException when you try to add a handler to the relevant "changed" event.

Change your code to check for null before trying to add the handler:
C#
MaskedTextBox mtxtBox = ctrl as MaskedTextBox;
if (mtxtBox != null)
{
    mtxtBox.TextChanged += TextWasChanged;
}
else
{
    TextBox txtBox = ctrl as TextBox;
    if (txtBox != null)
    {
        txtBox.TextChanged += TextWasChanged;
    }
    else
    {
        ComboBox cmb = ctrl as ComboBox;
        if (cmb != null)
        {
            cmb.SelectedIndexChanged += TextWasChanged;
        }
    }
}


NB: For future reference, "it doesn't work" is not a good description of the problem. Always include the full details of any exception your code is throwing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900