# Getting Started with CVB.Net

**URL:** <https://forum.commonvisionblox.com/t/getting-started-with-cvb-net/246>\
**Category:** .Net API\
**Tags:** csharp, tutorial\
**Created:** [September 9, 2019, 10:00pm UTC](https://forum.commonvisionblox.com/t/getting-started-with-cvb-net/246 "2019-09-09T22:00:00Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![parsd](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.commonvisionblox.com/parsd/32/7_2.png) [@parsd](https://forum.commonvisionblox.com/u/parsd)\
**Post date:** [September 11, 2019, 11:54am UTC](https://forum.commonvisionblox.com/t/getting-started-with-cvb-net/246/8 "2019-09-11T11:54:42Z")

</div>

# Display a Stream (Windows Forms)

For this post, let’s create a new _Windows Forms App_ (_Visual C#_) and call it _CvbViewer_:

 ![grafik](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/101b3faa429e998331e9783729d712e7f287f698.png)

After the wizard finishes its work you should see an open _Form1.cs [Design]_.

> Don’t forget to disable the _Prefer 32-bit_ check-box.

Before we can do anything useful we also need to add the _Stemmer.Cvb.dll_, _Stemmer.Cvb.Forms.dll_, _Stemmer.Cvb.Aux.dll_ and _Stemmer.Cvb.Extensions.dll_ to the references.

Open your _Toolbox_, right-click on the _General_ tab and click on _Choose items…_

 ![grafik](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/84d3f4360cbbdbeb2cff13c253960f11cffe6f27.png)

After loading finishes first try to enter `Stemmer` in the _Filter:_ text field. This should list:

- Display
- GenApiGrid
- StreamHandler

If that doesn’t show you anything you can click on _Browse…_ and enter

> %CVB%Lib\Net

and choose _Stemmer.Cvb.Forms_. Then confirm this and the _Choose Toolbox Items_ dialog with _Ok_. You now should see three new entries under _General_:

- Display
- GenApiGrid
- StreamHandler

1. We quickly add an “Open”-_Button_ (`openButton`) and a “Grab”-_CheckBox_ (`grabCheckBox`) and anchor them to the _Bottom_, _Left_.
2. We add the _StreamHandler_ as `streamHandler` to the `Form1`. It will be placed below the Designer window in the _Components_ section.
3. We add the _Display_ as `display`and anchor it _Top_, _Bottom_, _Left_, _Right_.

 ![grafik](https://canada1.discourse-cdn.com/flex027/uploads/commonvisionblox/original/1X/9aba7c4a387f1f612c9c57103c877cef6b9a31e7.png)

As we finished our UI-design, we can double-click on the _Open…_ button to create its _Click_ handler and enter the following code:

```auto
private void openButton_Click(object sender, EventArgs e)
{
  try
  {
    Device device = FileDialogs.LoadByDialog(path => DeviceFactory.Open(path), "CVB Devices|*.vin;*.emu;*.avi");
    if (device == null)
      return; // canceled

    Device = device;
  }
  catch (IOException ex)
  {
    MessageBox.Show(ex.Message, "Error loading device", MessageBoxButtons.OK, MessageBoxIcon.Error);
  }
}

```

and

```auto
private Device Device
{
  get { return _device; }
  set
  {
    if (!ReferenceEquals(value, _device))
    {
      display.Image = value?.DeviceImage;
      streamHandler.Stream = value?.Stream;

      _device?.Dispose(); // old one is not needed anymore
      _device = value;
    }
  }
}
private Device _device;

```

To make this work add the following namespaces:

```auto
using Stemmer.Cvb;
using Stemmer.Cvb.Forms;
using System.IO;

```

We use the helper `FileDialogs.LoadByDialog` to easily open a `FileDialog` that returns the opened `Device` or null if canceled. This dialog supports _avi_- and _emu_-files as well a preconfigured _vin_-drivers.

We also utilize a `Device Form1.Device` property to manage life-time of prior loaded devices and to assign the `DeviceImage` to the display and the `Device.Stream` to the `streamHandler`. (If `?.` looks unfamiliar: that is automatic `null` handling: `null` is assigned if `value == null`.)

When we run the app and load e.g. the _GenICam.vin_, we see a black image in the `Display`. This is normal as we don’t do “Auto Snap” and no data has been acquired, yet.

Finally let’s connect to the `grabCheckBox`’s _CheckedChanged_ event by double-clicking on the _CheckBox_ in the designer. Add the following code:

```auto
private void grabCheckBox_CheckedChanged(object sender, EventArgs e)
{
  if (grabCheckBox.Checked)
    streamHandler.Start();
  else
    streamHandler.Stop();

  openButton.Enabled = !grabCheckBox.Checked;
}

```

And that’s it. You have your life-display :cvb: app.

If you want to be correct and properly clean-up, move the `void Dispose(bool disposing)` (see the [Dispose pattern](https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/dispose-pattern) for more info about this) method from the _.Designer.cs_ into your implementation _cs_ file and change it to:

```auto
protected override void Dispose(bool disposing)
{
  if (disposing)
  {
    components?.Dispose();
    Device?.Dispose();
  }
  base.Dispose(disposing);
}

```

`components?.Dispose()` will dispose the `streamHandler`, which will abort any ongoing acquisition. And last but not least we clean-up our opened `Device`. This is especially useful if you use your window as a dialog in your application and must ensure that the `Device` is not open anymore after the dialog is closed.

---

_[View the full topic](https://forum.commonvisionblox.com/t/getting-started-with-cvb-net/246)._
