Please help me
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
private bool isScanning = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Tetapkan fokus ke tombol "Start Scan" saat aplikasi dimulai
btnStartScan.Focus();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (isScanning)
{
string barcodeData = e.KeyCode.ToString();
// Tampilkan data di TextBox atau tempat yang sesuai
textBox1.Text = barcodeData;
// Pindahkan fokus ke TextBox setelah data masuk
textBox1.Focus();
// Mencegah event keyboard default yang mengarahkan fokus ke tombol "Print"
e.SuppressKeyPress = true;
}
}
private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
// Ambil data dari TextBox atau tempat penyimpanan data
string dataToPrint = textBox1.Text;
// Definisikan font dan posisi cetak
Font printFont = new Font("Arial", 12);
float yPos = 100;
// Cetak data ke kertas
e.Graphics.DrawString(dataToPrint, printFont, Brushes.Black, new PointF(100, yPos)); ;
}
private void button1_Click(object sender, EventArgs e)
{
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(PrintDocument_PrintPage);
// Munculkan dialog pencetakan
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument;
if (printDialog.ShowDialog() == DialogResult.OK)
{
printDocument.Print();
}
}
private void btnStartScan_Click(object sender, EventArgs e)
{
StartScanning();
}
private void btnStopScan_Click(object sender, EventArgs e)
{
StopScanning();
}
private void StartScanning()
{
// Set isScanning ke true
isScanning = true;
// Hapus fokus dari semua kontrol, termasuk tombol "Start Scan" dan "Stop Scan"
this.ActiveControl = null;
// Tetapkan fokus ke TextBox untuk menerima input dari barcode scanner
textBox1.Focus();
}
private void StopScanning()
{
// Set isScanning ke false
isScanning = false;
// Tetapkan fokus kembali ke tombol "Start Scan"
btnStartScan.Focus();
}
}
}
Yea that's not quite what I envisaged you'd do, but if it works for you...![]()
What specific problem are you running into with your current code?
Did you try implementing the suggested approach of using the key previews?
Have you checked with scanner's manufacturer to see if they support more than just keyboard emulation mode? Some scanner manufacturers have drivers where they can act like a keyboard because that is the easiest way to let people integrate with their product without having to any code, but they also support a mode where you write code to listen to a specific serial or USB port and the input is not sent out as keystrokes. If your scanner's driver supports this latter mode, then you don't even have to listen to key previews events or try to place games with focus like the code that you presented. Instead you will just open a port and listen for scanner input directly, and you can put the input straight into the textboxes and/or your data model directly.
Yes, we understood that from your original post. The questions are:
What problem do you have your current code?
Have you tried the key preview approach suggested by @cjard ?
Have you tried getting input directly from the scanner without in keyboard emulation mode?