Search code examples
c#formsdispose

TextBox.Text is being disposed, even when creating the form again


i´ve been working on a C# PDA. Recently I found a bug.

Steps

This are the steps to reproduce it:

  1. Enter the form, scan some data.

  2. Close the form.

  3. Reopen the form.

  4. When you scan data, some TextBox appear to be disposed.

Code

We create the Form:

 private void btp1_1_Click(object sender, EventArgs e) {            
            if(AppCore.core.pdaUser.isAuthorizedMovement(Movements.AltaPalet) == false) {
                MessageBox.Show("No estás autorizado para realizar esta operación.");
            } else {
                try{
                    //MessageBox.Show("Funcionalidad en desarrollo. Disculpe las molestias");
                    FormAltaPalet form = new FormAltaPalet(Movements.AltaPalet);
                    form.ShowDialog();
                    form.Dispose();
                }catch(WebException ex){
                    MessageBox.Show("Error técnico. " + ex.Message);
                }
            }
        }

The first line of the Form is InitializeComponent.

  public FormAltaPalet(Movements movement) {

           // GC.Collect();
           // GC.WaitForPendingFinalizers();
            InitializeComponent();
            //clear(FormStep.Step1);
           // this.FinishError(); // FIX MA 08.07.2019
            this.move = movement;
            this.tbp1_1.Focus();
            this.material = "";
            this.ean14 = "";
            context = this;

            // SCANNER
            if (AppCore.core.dispositivo_PDA ) { // BARCODE 2
                myBarcode2 = new Barcode2(Devices.SupportedDevices[0]);
                myBarcode2.Enable();
                myBarcode2.Scan();
                // Register a scan event handler to the barcode object
                myBarcode2.OnScan += new Barcode2.OnScanHandler(myBarcode_OnScan);
            }
        }

Here, we create the TextBoxs and labels. On the first run it works fine, it starts failing when closing and opening again.

   private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FormAltaPalet));
            this.pmm = new System.Windows.Forms.Panel();
            this.bt_mm_2 = new System.Windows.Forms.Button();
            this.bt_mm_1 = new System.Windows.Forms.Button();
            this.p1 = new System.Windows.Forms.Panel();
            this.label_error_1 = new System.Windows.Forms.Label();
            this.btp1_2 = new System.Windows.Forms.Button();
            this.lbp1_6 = new System.Windows.Forms.Label();
            this.tbp1_6 = new System.Windows.Forms.TextBox();
            this.tbp1_4 = new System.Windows.Forms.TextBox();
            this.tbp1_3 = new System.Windows.Forms.TextBox();
            this.tbp1_2 = new System.Windows.Forms.TextBox();
            this.tbp1_1 = new System.Windows.Forms.TextBox();
            this.lbp1_4 = new System.Windows.Forms.Label();
            this.lbp1_3 = new System.Windows.Forms.Label();
            this.lbp1_2 = new System.Windows.Forms.Label();
            this.lbp1_1 = new System.Windows.Forms.Label();
            this.pbp1_1 = new System.Windows.Forms.PictureBox();
            this.pmm.SuspendLayout();
            this.p1.SuspendLayout();
            this.SuspendLayout();

When we reach the method in the class, we get the current context:

  void myBarcode_OnScan(ScanDataCollection sd) {
            context = this;
            this.tbp1_1 = getTbl1_1();

But it's wrong, the Objects seems to be disposed:

https://i.sstatic.net/Z2yZE.jpg

EDIT: When I click the button shown in the first chunk of code, the form should be created. When I close the form it should be disposed. The code of the Dispose method is this:

   protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

It seems Initialize component works good on the second run too, but when it reaches the scan method, the TextBoxs are disposed.... Question

Im pretty new to C# developement, so I'm kind of lost.

My guess is that something is happening with the Dispose part.

Any way to fix this problem?

Thanks!


Solution

  • Finally, I found a solution.

    After figuring out what was going wrong, I found out that the SCAN event was kept alive.

    I added a Closing override:

     this.Closing += MyClosedHandler;
    

    And the MyClosedHandlerEvent as follows:

    
            protected void MyClosedHandler(object sender, EventArgs e)
            {
               this.myBarcode2.ScanCancel();
                this.myBarcode2.Disable();
                if (myBarcode2 != null) this.myBarcode2.Dispose();
            }
    

    Problem was detected with scanner event handler.

    Hope this will help somebody else!