Search code examples
c#zxing

Call a method inside an event handler function


I'm trying to implement an application in C# which generates me a QR Code. I've managed to do this, but I don't know how to call CreateQRImage(string inputData) inside the genButton_Click(object sender, EventArgs e).

I inside the windows form I have a textbox and a button, and I think that the function CreateQRImage must be called with the name of the textbox

Here is the code:

private void genButton_Click(object sender, EventArgs e)
{

}

public void CreateQRImage(string inputData)
{
    if (inputData.Trim() == String.Empty)
    {
        System.Windows.Forms.MessageBox.Show("Data must not be empty.");
    }

    BarcodeWriter qrcoder = new ZXing.BarcodeWriter
    {
        Format = BarcodeFormat.QR_CODE,
        Options = new ZXing.QrCode.QrCodeEncodingOptions
        {
            ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.H,
            Height = 250,
            Width = 250
        }
    };

    string tempFileName = System.IO.Path.GetTempPath() + inputData + ".png";

    Image image;
    String data = inputData;
    var result = qrcoder.Write(inputData);
    image = new Bitmap(result);
    image.Save(tempFileName);

    System.Diagnostics.Process.Start(tempFileName);
}

Solution

  • This should do the trick:

    private void genButton_Click(object sender, EventArgs e)
    {
        // Assuming your text box name. Also assuming UI disables button until text is entered.
        this.CreateQRImage(myTextBox.Text);
    }