Hello fellow programmers!
I`m developing a Windows Forms .NET Compact Framework 2.0 for a Windows Mobile 6.1 device that has a barcodereader hardware.
I can use the barcodereader to read barcodes, and I can activate and deactivate it as well. Except that when I try to read something and go to the next form I get a objectdisposedexception. That happens (I guess) because I have to dispose the instance of the barcode reader and then create a new one in the next form.
The problem is: when I use a button to go to the next form, using the same code to dispose the barcodereader I don`t have the objectdisposedexception. When I simply put the form load on the textchanged event the error rises, but is not caught by any try/catch statements, making the application crash.
I can`t debug it either, because the VS emulator to windows mobile does not work with the device barcodereader DLL.
Can someone help me?
Here`s the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
//DLL that controls the barcodereader
using Intermec.DataCollection;
namespace WOPT_Coletor.view.ConsultarPosicao
{
public partial class frmConsultarPosicao_2 : Form
{
public BarcodeReader leitor;
public frmConsultarPosicao_2()
{
InitializeComponent();
ShowHide.ShowTopStatusbar(false);
//code to work with the barcode reader
model.LeitorCodigoDeBarras classeLeitor = new model.LeitorCodigoDeBarras();
leitor = classeLeitor.LerCodigoDeBarras();
leitor.BarcodeRead += new BarcodeReadEventHandler(this.eventoLeitorCodigoDeBarrasArmazenagem1);
}
//Event to receive the barcode reading information
void eventoLeitorCodigoDeBarrasArmazenagem1(object sender, BarcodeReadEventArgs e)
{
tbCodMaterial.Text = e.strDataBuffer.Trim();
}
private void tbCodMaterial_TextChanged(object sender, EventArgs e)
{
try
{
if (tbCodMaterial.Text.Length == 23)
{
Cursor.Current = Cursors.WaitCursor;
Cursor.Show();
//disposal of the barcodereader instance
leitor.ScannerOn = false;
leitor.ScannerEnable = false;
leitor.Dispose();
leitor = ((BarcodeReader)null);
//processing of the information read.
char[] auxcodMaterial = new char[9];
using (StringReader str = new StringReader(tbCodMaterial.Text))
{
str.Read(auxcodMaterial, 0, 8);
}
string codMaterial = new string(auxcodMaterial);
//loads next form
Form destino = new frmConsultarPosicao_3(codMaterial);
destino.Show();
Cursor.Current = Cursors.Default;
Cursor.Show();
//closes and dispose of the current form
this.Close();
this.Dispose(true);
}
}
catch (ObjectDisposedException ex)
{
MessageBox.Show(ex.Message);
}
}
}