I have barcode scanner device I want detect and connect this device in my application (windows form c#) but ,I don't know how will be connect
please help me
You haven't to. Let me explain: your device (I think is USB) is already connected to windows, so it is working like another keyboard. Just set the cursor to a textBox or whatever might accept entry if you are on windows Forms or use Console.ReadLine() if you use a console app.
// for a console app: example (you'll see the entry twice of course!)
class Program
{
static void Main(string[] args)
{
string inputCode = Console.ReadLine();
Console.WriteLine(inputCode);
Console.ReadKey();
}
}
When you run this, just scan a barcode and it will show the scanned string (twice: one for the keyboard, the other from the variable output).
For a Windows.Forms app, I suggest your code sets the focus to an empty field as a TextBox and you use its KeyDown event to test if a string has come in. Note that your scanner should be able to perform a CR or CR-LF at the end of the scan (this was also applicable to the console app example for the Console.ReadLine() to exit).
Here's an example: put 2 text boxes (textBox1 and textBox2) in your form.
// This will give focus and place cursor on textBox2 at form start (normally, it would be on the first textBox created)
public Form1()
{
InitializeComponent();
textBox2.Select();
textBox2.KeyDown += TextBox2_KeyDown;
}
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// This will copy to the textBox1 the incoming scanned barcode
textBox1.Text = textBox2.Text;
}
}
Of course, these are only examples to show the flow. Don't use 2 text containers with the same content in real life!
After your first comment: Put a listBox in your Form and a button to trigger this:
private void button3_Click(object sender, EventArgs e)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from Win32_Keyboard");
foreach (ManagementObject keyboard in searcher.Get())
{
foreach (PropertyData prop in keyboard.Properties)
{
if (prop.Value != null)
{
listBox1.Items.Add(prop.Value);
// later, use: if (Convert.ToString(prop.Value).Contains("your device")) to check the presence
}
}
}
}
Add this at the beggining:
using System.Management;
This will show you the devices connected to your computer. By plugging/unplugging, note the reference of the scanner (or the USB dongle), and later you can test if it is connected or not.
Please comment again if you need further help.