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();
}
}
}