WPF barcode generator - C# sample - ByteScout

WPF barcode generator – C# sample

WPF barcode tutorial shows how to generate WPF barcode in C# using Barcode Generator SDK.

WPF Barcode control is included with BarCode Generator SDK and can be used in .NET 3.5 – 4.5 applications. Full source code sample for this tutorial is also available in evaluation version of the SDK – browse Examples folder > WPF Control.

C#

private void UpdateBarcode()
       {
           SymbologyType symbology = (SymbologyType)Enum.GetValues(typeof(SymbologyType)).GetValue(this.cboSymbologyType.SelectedIndex);
           txtSymbologyDescription.Text = ctrlBarcodeControl.GetValueRestrictions(symbology);

           try
           {
               if (symbology == SymbologyType.EAN13 || symbology == SymbologyType.ISBN || symbology == SymbologyType.UPCA)
               {
                   txtSupplementalValue.IsEnabled = true;
                   lblSupplementalValue.IsEnabled = true;
                   txtSymbologyDescription.Text += " " + ctrlBarcodeControl.GetSupplementaryValueRestrictions();
               }
               else
               {
                   txtSupplementalValue.IsEnabled = false;
                   lblSupplementalValue.IsEnabled = false;
               }

               lblErrorMessage.Content = "";
               ctrlBarcodeControl.RegistrationKey = "XXXXXXXXXXXXXXXXXXx";
               ctrlBarcodeControl.RegistrationName = "YYYYYYYYYYYYYYYYYYYY";
               ctrlBarcodeControl.Symbology = symbology;
               ctrlBarcodeControl.SupplementValue = txtSupplementalValue.Text;
               ctrlBarcodeControl.Value = txtValueToEncode.Text;
               ctrlBarcodeControl.DrawCaptionFor2DBarcodes = chkDrawCaptionFor2D.IsChecked.Value;
               ctrlBarcodeControl.AutoFitToControlSize = chkAutoFitToContainer.IsChecked.Value;
               ctrlBarcodeControl.Caption = "";

               if (symbology == SymbologyType.PDF417 || symbology == SymbologyType.PDF417Truncated ||
                   symbology == SymbologyType.MacroPDF417 || symbology == SymbologyType.DataMatrix ||
                   symbology == SymbologyType.GS1_DataMatrix)
               {
                   ctrlBarcodeControl.BarHeight = PdfBarHeight;
               }
               else if (symbology == SymbologyType.MicroPDF417)
               {
                   ctrlBarcodeControl.BarHeight = PdfBarHeight / 2;
               }
               else
               {
                   ctrlBarcodeControl.BarHeight = BarHeight;
               }
           }
           catch (Exception)
           {
               lblErrorMessage.Content = "Value is invalid for current symbology. Please review value restrictions above.";
           }
       }

Tutorials:

prev
next