# Charts and Barcodes

> Charts and barcodes in a Windows Forms TextControl: the chart gallery, Chart Layout tab, barcode types and dialog, and changing a selected ChartFrame in C#.

- **Product:** TX Text Control .NET for Windows Forms
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for Windows Forms > Document Editor > Charts and Barcodes
- **3451 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/charts-barcodes/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/charts-barcodes/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/charts-barcodes/llms-full.txt

---

A column chart of quarterly sales, a QR code with a link to an online invoice, a Code 128 barcode with an order number: charts and barcodes are frames that a Windows Forms `TextControl` places in the text like images. Users insert them from the **Insert** tab and change them on their own ribbon tabs, and your code changes the chart or barcode the user has selected.

The code goes into *Form1.cs* of the application from [Create Your First Windows Forms Application](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-editor/first-application/llms-full.txt), in methods you call from the `Click` handler of a button. Charts and barcodes placed with `ServerTextControl` are covered in [Charts and Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/charts-barcodes/llms-full.txt) in Document Processing, and templates whose barcodes are filled with data in [Images and Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/mail-merge/images-barcodes/llms-full.txt).

### How Charts and Barcodes Work

Each chart or barcode in a document consists of a control that holds its data and appearance, a chart control of `System.Windows.Forms.DataVisualization` or a [TXBarcodeControl](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-barcode/txbarcodecontrol/llms-full.txt), and a frame that places it in the document, a [ChartFrame](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-datavisualization/chartframe/llms-full.txt) or a [BarcodeFrame](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-datavisualization/barcodeframe/llms-full.txt). [What Each Object Is Made Of](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/integration/charts-barcodes/#what-each-object-is-made-of) lists the classes, the collections and the assemblies. The frames are placed inline, anchored to a paragraph or at a fixed position like every frame, as [How Frames Are Placed](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/images-shapes/#how-frames-are-placed) describes.

The TX Text Control format and RTF keep charts and barcodes as objects that are rebuilt when the document is loaded; other formats, such as DOCX and PDF, contain an image of them. Without the chart or barcode assembly on the system, a loaded chart or barcode appears as its image.

### Charts

The chart control of `System.Windows.Forms.DataVisualization` is part of the .NET Framework, but Microsoft didn't port it to .NET. A .NET project gets it from a package built from Text Control's fork of a .NET port, as [Migrating from .NET Framework to .NET](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/installation/upgrading/from-dotnet-framework/#charts) describes step by step. Once the package is installed, the chart commands of the ribbon work. [Integrating Charts and Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/integration/charts-barcodes/llms-full.txt) covers the setup of both kinds of objects.

![Class diagram of the chart classes, marked as Windows Forms only: TextControl with Charts, ChartLayoutDialog and the chart events, ChartCollection, ChartFrame and the System.Windows.Forms.DataVisualization.Chart it holds.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/charts-class-diagram.ff05f52b.webp)

#### Insert and Format Charts in the Ribbon

**Chart** in the **Illustrations** group of the **Insert** tab opens a gallery of chart types in the categories **Column**, **Line**, **Pie**, **Bar**, **Area**, **X Y (Scatter)**, **Stock** and **Radar**. A click inserts a chart of that type, filled with default values:

![TX Text Control Words with the Chart gallery of the Insert tab open, showing the categories Column, Line, Pie, Bar, Area, X Y (Scatter), Stock and Radar with their chart types.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/insert-tab-chart-gallery.56b57700.webp)

A selected chart is changed on two tabs:

- The **Chart Layout** tab, a [RibbonChartLayoutTab](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-windows-forms-ribbon/ribbonchartlayouttab/llms-full.txt), with the groups **Data** (**Edit Data**, **Set Data Relation**), **Labels** (**Legend**, **X-Axis Values**, **Y-Axis Values**), **Titles** (**Chart Title**, **X-Axis Title**, **Y-Axis Title**), **X-Axis** and **Y-Axis**, which set lines, gridlines, minimum, maximum and interval.
- The **Frame Formatting** tab, whose **Type & Appearance** group sets the **Chart Type**, switches between **3D** and 2D and sets **Rotation**, **Right Angle Axes**, **Inclination** and **Perspective** of 3D charts.

![TX Text Control Words with a selected 3-D line chart and the Chart Formatting tab of the Chart Tools group, whose Major Gridlines menu of the X-Axis group is open.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/chart-formatting-tab.2168bb4f.webp)

TX Text Control Words calls the chart layout tab **Chart Formatting** and shows it with the **Frame Formatting** tab in a contextual **Chart Tools** group. **Set Data Relation** connects a chart in a template to the tables and columns of a data source, which a mail merge then fills; it needs a [RibbonReportingTab](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-windows-forms-ribbon/ribbonreportingtab/llms-full.txt) in the same ribbon.

#### Change the Selected Chart From Code

[Add Charts](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/charts-barcodes/#add-charts) in Document Processing creates a chart control, wraps it in a `ChartFrame` and inserts it with `textControl1.Charts.Add(frame, -1)`. In the editor, your code usually works with the chart the user has selected. The following method adds a data point to it:

```csharp
using System;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using TXTextControl;
using TXTextControl.DataVisualization;

public partial class Form1 : Form
{
    private void AddPointToSelectedChart()
    {
        // The chart the user has selected, or null
        ChartFrame frame = textControl1.Charts.GetItem();

        if (frame != null)
        {
            Chart chart = (Chart)frame.Chart;
            chart.Series[0].Points.AddXY("East", 95);

            // Show the change in the document
            frame.Refresh();
        }
    }
}
```

The code works as follows:

- [ChartCollection.GetItem](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-datavisualization/chartcollection/getitem-method/llms-full.txt) without a parameter returns the selected chart, or `null`, and [ChartFrame.Chart](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-datavisualization/chartframe/chart-property/llms-full.txt) its chart control. `Series` and `Points.AddXY` belong to the chart control, not to TX Text Control.
- Changes to the chart control aren't shown until [ChartFrame.Refresh](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-datavisualization/chartframe/refresh-method/llms-full.txt) is called. [AddUndoUnit](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-datavisualization/chartframe/addundounit-method/llms-full.txt) adds the state of the chart to the undo stack, which changes made in code don't reach otherwise.

[ChartLayoutDialog](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/chartlayoutdialog-method/llms-full.txt) opens the layout dialog box of the selected chart, with tabs for text wrapping and layout and for size and distances. Events such as [ChartDoubleClicked](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/chartdoubleclicked-event/llms-full.txt) report what users do with a chart. The blog article [Using Charts with TX Text Control .NET for Windows 31.0 in .NET 6](https://www.textcontrol.com/blog/2022/11/02/using-charts-with-tx-text-control-net-for-windows-3100-in-net-6/) binds a chart to XML data.

### Barcodes

The barcode classes are part of the package `TXTextControl.TextControl.WinForms.SDK`, in the assemblies *TXBarcode.dll* and *TXBarcode.Windows.Forms.dll*, and need no setup.

![Class diagram of the barcode classes: TextControl, WPF.TextControl and ServerTextControl with Barcodes, BarcodeLayoutDialog and the barcode events, BarcodeCollection, BarcodeFrame and the TXBarcodeControl and WPF.TXBarcodeControl with their properties.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/barcodes-class-diagram.df5a7e7c.webp)

#### Insert and Format Barcodes in the Ribbon

**Barcode** in the **Illustrations** group of the **Insert** tab lists the barcode types, from **QRCode** and **Code128** to **Maxicode**. A click inserts a barcode of that type:

![TX Text Control Words with the Barcode drop-down of the Insert tab open, listing the barcode types QRCode, Code128, EAN13, UPCA and more, above shipping labels with barcodes.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/insert-tab-barcode-menu.ea6bd4b9.webp)

The **Frame Formatting** tab shows a **Colors & Alignment** group for a selected barcode, with **Foreground Color**, **Background Color**, **Transparency**, **Horizontal Alignment**, **Vertical Alignment** and **Rotation**:

![TX Text Control Words with the Frame Formatting tab for a selected barcode and the Background Color gallery of the Colors & Alignment group open, above QR, Aztec, Data Matrix and MaxiCode barcodes in different colors.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/frame-tab-barcode-colors.1a14a062.webp)

The type and the encoded text are set in the dialog box that [BarcodeLayoutDialog](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/barcodelayoutdialog-method/llms-full.txt) opens: its tabs set text wrapping and layout, size and distances, and the colors, type and text of the barcode.

![The Barcode Layout dialog box on its Type and Color tab, with the background color and transparency, the bar color, horizontal and vertical alignment, rotation, and the Encoding list open with QRCode, Code128, EAN13, UPCA, EAN8 and Interleaved2of5.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/barcode-layout-dialog.771a6b03.webp)

#### Change the Selected Barcode From Code

[Insert a QR Code](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/charts-barcodes/#insert-a-qr-code) in Document Processing creates a `TXBarcodeControl`, sets its type and text, wraps it in a `BarcodeFrame` and inserts it with `textControl1.Barcodes.Add`. `Add` with only a [FrameInsertionMode](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/frameinsertionmode/enumeration/llms-full.txt) lets the user draw the barcode with the mouse instead. The following method changes the text of the barcode the user has selected:

```csharp
using System;
using System.Windows.Forms;
using TXTextControl;
using TXTextControl.Barcode;
using TXTextControl.DataVisualization;

public partial class Form1 : Form
{
    private void ChangeSelectedBarcode(string text)
    {
        // The barcode the user has selected, or null
        BarcodeFrame frame = textControl1.Barcodes.GetItem();

        if (frame == null)
        {
            return;
        }

        TXBarcodeControl barcode = (TXBarcodeControl)frame.Barcode;

        // Check text a user typed before it is assigned
        string errorMessage;

        if (!TXBarcodeControl.IsTextValid(barcode.BarcodeType, text, out errorMessage))
        {
            MessageBox.Show(errorMessage);
            return;
        }

        // The maximum length must allow the new text
        barcode.UpperTextLength = Math.Max(barcode.UpperTextLength, text.Length);
        barcode.Text = text;

        // Changes to the barcode are shown after Refresh
        frame.Refresh();
    }
}
```

The code works as follows:

- [BarcodeCollection.GetItem](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-datavisualization/barcodecollection/getitem-method/llms-full.txt) without a parameter returns the selected barcode, or `null`. [BarcodeFrame.Barcode](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-datavisualization/barcodeframe/barcode-property/llms-full.txt) is its control.
- The static [IsTextValid](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-barcode/txbarcodecontrol/istextvalid-method/llms-full.txt) checks whether a text is valid for the barcode type and returns an error message. [UpperTextLength](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-barcode/txbarcodecontrol/uppertextlength-property/llms-full.txt), the maximum number of characters, and [Text](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-barcode/txbarcodecontrol/text-property/llms-full.txt) would throw an `ArgumentException` for an invalid value.
- [Refresh](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-datavisualization/barcodeframe/refresh-method/llms-full.txt) shows the change in the document.

[BarcodeDoubleClicked](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/barcodedoubleclicked-event/llms-full.txt) and the other barcode events report the user's actions, for example to open `BarcodeLayoutDialog` on a double-click. The blog article [Enhancing Documents with QR Codes and Barcodes in .NET C#: A Comprehensive Guide](https://www.textcontrol.com/blog/2024/07/11/enhancing-documents-with-qr-codes-and-barcodes-in-net-csharp-a-comprehensive-guide/) merges barcodes into templates.

### Next Steps

- [Charts and Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/charts-barcodes/llms-full.txt) in Document Processing: insert, position and fill charts and barcodes in code.
- [Images and Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/mail-merge/images-barcodes/llms-full.txt): fill barcodes of a template with data.
- [Integrating Charts and Barcodes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/integration/charts-barcodes/llms-full.txt): set up the chart package and the barcode assemblies.
- [Images](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/images/llms-full.txt): images, the frames that charts and barcodes are placed like.

---

## Nearby

- **Previous:** [Text Frames and Shapes](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/text-frames-shapes/llms-full.txt)
- **Next:** [Headers and Footers](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/headers-footers/llms-full.txt)
