title: barcode scanner programming in C#
body: <p>I need to develop a simple attendance system for a company. I am interested to develop this software in C# because I heard that .NET framework provides Barcode Scanner Libraries which makes this task easier. I have been given barcode scanner of AURORA. I have configured this scanner with my system and it reads when i swipe card before it.
  I have no idea how to capture barcode information!! This is a completely new task for me and I don't know the methods which I can use to read barcode.
  I know that Scaner usually read data as string, keeps it in clipboard and paste it in
  active editbox or whatever it active.
  For example, if I open notepad and scan card, in notepad I see the number 00004 (which i think is barcode)...<br/>
  I have few questions:<br/><b>
  1. What is the best way to read barcode value which appears on editbox (My application will have an editbox), I need to control the Scanner Event so that It should not paste barcode value in editbox by iteself, rather than I will use that value...<br/>
  2. What will be the code which will fire an event when someone swipe card?<br/></b>
  Kindly provide some working sample code(C#).Your help will be highly appreciated.</p>
  
  <pre><code>public partial class Form1 : Form
  {
      SerialPort _serialPort;
  
      // delegate is used to write to a UI control from a non-UI thread
      private delegate void SetTextDeleg(string text);
  
      private void Form1_Load(object sender, EventArgs e)
      {
          // all of the options for a serial device
          // can be sent through the constructor of the SerialPort class
          // PortName = "COM1", Baud Rate = 19200, Parity = None, 
          // Data Bits = 8, Stop Bits = One, Handshake = None
          _serialPort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
          _serialPort.Handshake = Handshake.None;
          _serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
          _serialPort.ReadTimeout = 500;
          _serialPort.WriteTimeout = 500;
          _serialPort.Open();
      }
  
      private void btnStart_Click(object sender, EventArgs e)
      {
          // Makes sure serial port is open before trying to write
          try
          {
              if (!_serialPort.IsOpen)
                  _serialPort.Open();
  
              _serialPort.Write("SI\r\n");
          }
          catch (Exception ex)
          {
              MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
          }
      }
  
      void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
      {
          Thread.Sleep(500);
          string data = _serialPort.ReadLine();
          this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
      }
  
      private void si_DataReceived(string data)
      {
          textBox1.Text = data.Trim();
      }
  
      private TextBox textBox1;
      private Label label1;
      private RichTextBox richTextBox1;
      private Button button1;
  }
  </code></pre>