Tutorial: Inserting a Dynamically Created Barcode Control into TX Text Control

While the tutorial uses a visual instance of TXBarcodeControl on a form, this sample shows how to add a dynamically created TXBarcodeControl to the BarcodeCollection of TX Text Control.

The source code is contained in the following directories:

  • Samples\WPF\CSharp\TextControl\Simple
  • Samples\WPF\VB.NET\TextControl\Simple

1. Creating the Project and Controls

Start Visual Studio .NET and create a new project. Select either Visual Basic or C# as a project type, and WPF Application as a template.

Image

Find the TX Text Control 19.0 toolbox tab that has been created automatically. All usable TX Text Control controls or components are listed in this tab.

Image

Click on the TextControl icon and draw it on the form.

Image

Choose Add Reference... from the main menu Project to open the Reference Manager dialog. Select Assemblies -> Extensions and find the TXBarcode assemblies TXBarcode and TXBarcode.WPF. Check both and confirm with OK.

Image

In the Solution Explorer, open the licenses.licx which is located in the project's Properties. Add the following string:

TXTextControl.WPF.Barcode.TXBarcodeControl, TXBarcode.WPF, Version=2.0.100.500, Culture=neutral, PublicKeyToken=6b83fe9a75cfb638

Add a MenuStrip to the form and add the menu Insert with a submenu item Barcode to this MenuStrip. The following XAML code is required to add this menu:

<Menu IsMainMenu="True" Name="menuBar" DockPanel.Dock="Top">
    <MenuItem Header="Insert" >
    <MenuItem Header="Barcode" Click="MenuItem_Click" />
    </MenuItem>
</Menu>

Image

Navigate to the just created submenu item Barcode to insert the Clicked event handler.

Add the following code to the Click procedure of the menu item:

private void MenuItem_Click(object sender, RoutedEventArgs e)
{
    TXTextControl.WPF.Barcode.TXBarcodeControl txBarcode1 =
        new TXTextControl.WPF.Barcode.TXBarcodeControl();

    txBarcode1.BarcodeType =
        TXTextControl.Barcode.BarcodeType.QRCode;
    txBarcode1.UpperTextLength = 19;
    txBarcode1.Text = "www.textcontrol.com";

    TXTextControl.DataVisualization.BarcodeFrame bf =
        new TXTextControl.DataVisualization.BarcodeFrame(txBarcode1);

    textControl.Barcodes.Add(bf, -1);
}
Private Sub MenuItem_Click(sender As Object, e As RoutedEventArgs)
    Dim txBarcode1 As New TXTextControl.WPF.Barcode.TXBarcodeControl()

    txBarcode1.BarcodeType = TXTextControl.Barcode.BarcodeType.QRCode
    txBarcode1.UpperTextLength = 19
    txBarcode1.Text = "www.textcontrol.com"

    Dim bf As New TXTextControl.DataVisualization.BarcodeFrame(txBarcode1)

    textControl.Barcodes.Add(bf, -1)
End Sub

Start the application, open the main menu and choose Barcode from the Insert menu. A new TXBarcodeControl is created dynamically, its' type is set to QRCode and the Text is set. Finally, the barcode is added to TextControl's BarcodeCollection.