Users Guide

Using Changed Events

In many applications, you may want to perform an action in response to a change in the value of an object's property. For example, if the contrast or brightness of an acquisition FIFO changes, you may want to acquire a new image. Or you may want to change a vision tool's parameters in response to a shape change. Most VisionPro properties fire a Changed event when their value changes. You can write a changed event handler to perform an action when the value of a property changes. Some VisionPro objects fire other events in addition to changed events. Interactive graphics, for example, fire Dragging and events when a graphic object is dragged.

To learn more about events, see the About Events in VisionPro theory topic.

This example shows you how to write an event handler for an ICogAcqFifo that acquires a new image whenever the brightness or contrast properties change. The brightness and contrast values can be changed two ways: with a slider, and by resetting the values with a button. Although you could insert the reacquisition code in each of the controls, in this example the code will be in the FIFO's changed event handler. This approach avoids the duplication of code, improves maintainability, and ensures that the reacquisition takes place regardless of how the brightness or contrast values are changed.

Note: If you are using QuickBuild and using your own changed event handler, bear in mind that QuickBuild may change properties (such as triggering) that may invoke your changed even handler. Your changed event handler must be able to handle this case.

Before You Begin

This example assumes that you have installed VisionPro and that you are familiar with acquiring images using an acquisition FIFO and with displaying images .

The examples in this walkthrough are given in both C# and Visual Basic.

Initialize the FIFO and Controls

The InitialzieFifo function initializes the acquisition FIFO, the two sliders, and the reset button. sldBrightness and sldContrast are the sliders. btnReset is the reset button. The highlighted line shows how to associate the event handler, mAcqFifo_Changed with the acquisition FIFO's changed event.

C# Code

private ICogAcqFifo mAcqFifo = null;
private ICogFrameGrabber mFrameGrabber = null;
private int numAcqs = 0;
private ICogAcqBrightness mBrightness;
private ICogAcqContrast mContrast;
private double _defaultBrightness, _defaultContrast;

private void InitializeFifo()
{
    CogFrameGrabbers mFrameGrabbers = new CogFrameGrabbers();
    String videoFormat = "Sony XC75 640x480";

    mFrameGrabber = mFrameGrabbers[0];
    mAcqFifo = mFrameGrabber.CreateAcqFifo(videoFormat, CogAcqFifoPixelFormatConstants.Format8Grey,0,true);

    mBrightness = mAcqFifo.OwnedBrightnessParams;
    mContrast = mAcqFifo.OwnedContrastParams;
    _defaultBrightness = mBrightness.Brightness;
    _defaultContrast = mContrast.Contrast;

    sldBrightness.Value = (int) (mBrightness.Brightness * 100);
    sldContrast.Value = (int) (mContrast.Contrast * 100);

    mAcqFifo.Changed += new CogChangedEventHandler(mAcqFifo_Changed);
}

VB.NET Code

Private mAcqFifo As Cognex.VisionPro.ICogAcqFifo = Nothing
Private mFrameGrabber As Cognex.VisionPro.ICogFrameGrabber = Nothing
Private numAcqs As Integer = 0
Private mBrightness As ICogAcqBrightness
Private mContrast As ICogAcqContrast
Private _defaultBrightness As Double
Private _defaultContrast As Double

Private Sub InitializeFifo()
    Dim videoFormat As String = "Sony XC75 640x480"    
    mAcqFifo = mFrameGrabber.CreateAcqFifo(videoFormat, CogAcqFifoPixelFormatConstants.Format8Grey, 0, True)

    mBrightness = mAcqFifo.OwnedBrightnessParams
    mContrast = mAcqFifo.OwnedContrastParams
    _defaultBrightness = mBrightness.Brightness
    _defaultContrast = mContrast.Contrast

    sldBrightness.Value = mBrightness.Brightness * 100
    sldContrast.Value = mContrast.Contrast * 100
            
    AddHandler mAcqFifo.Changed, AddressOf mAcqFifo_Changed
End Sub

Write the Acquisition FIFO Event Handler

All changed events for the acquisition FIFO are handled by mAcqFifo_Changed.

C# Code

private void mAcqFifo_Changed(object sender, CogChangedEventArgs e) {
    if (  ((e.StateFlags & CogAcqFifoStateFlags.SfBrightness) != 0) || ((e.StateFlags & CogAcqFifoStateFlags.SfContrast) != 0 )) {
        int trignum;
        cogDisplay1.Image = ((ICogAcqFifo) sender).Acquire(out trignum);
        numAcqs++;
        if (numAcqs > 4) {
            GC.Collect();
            numAcqs = 0;
        }
    }
}

VB.NET Code

Private Sub mAcqFifo_Changed(ByVal sender As System.Object, ByVal e As Cognex.VisionPro.CogChangedEventArgs)
    If (e.StateFlags And CogAcqFifoStateFlags.SfBrightness <> 0) Or _
       (e.StateFlags And CogAcqFifoStateFlags.SfContrast <> 0) Then
        Dim trignum As Integer
        cogDisplay1.Image = CType(sender, ICogAcqFifo).Acquire(trignum)
        numAcqs += 1
        If numAcqs > 4 Then
            GC.Collect()
            numAcqs = 0
    End If
End Sub

The sender parameter is a reference to the object that raised the event. In this case it is an ICogAcqFifo.

The e parameter is CogChangedEventArgs object whose StateFlags property is a set of bit flags that describes which aspects of the acquisition FIFO have changed.

If you want to use the changed event to limit the value of a property, you must change the property itself, not the value parameter.

Write the Slider Code

In this example, the form contains two sliders named sldBrightness and sldContrast. The Maximum property of both sliders to 100. The brightness and contrast values range from 0.0 to 1.0. The values of the sliders range from 0 to 100. You need to multiply the contrast and brightness values to set the sliders.

The code to change the contrast and brightness values with the slider is simple. This example implements it in the sliders' Scroll event handler so that the values change as you slide the slider.

C# Code

private void sldBrightness_Scroll(object sender, System.EventArgs e) {
    mBrightness.Brightness = ((System.Windows.Forms.TrackBar) sender).Value / 100.0;
}

private void sldContrast_Scroll(object sender, System.EventArgs e) {
    mContrast.Contrast = ((System.Windows.Forms.TrackBar) sender).Value / 100.0;
}
	

VB.NET Code

Private Sub sldBrightness_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sldBrightness.Scroll
    mBrightness.Brightness = CType(sender, System.Windows.Forms.TrackBar).Value / 100.0
End Sub

Private Sub sldContrast_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sldContrast.Scroll
    mContrast.Contrast = CType(sender, System.Windows.Forms.TrackBar).Value / 100.0
End Sub
	

If you slide the sliders now, the image will change because the acquisition FIFO's changed event fires when the brightness and contrast values change.

Write the Reset Button Code

A reset button on the form sets the contrast and brightness back to their initial values. The Click handler for the button looks like this:

C# Code

private void btnReset_Click(object sender, System.EventArgs e) {
    mBrightness.Brightness = _defaultBrightness;
    mContrast.Contrast = _defaultContrast;
}
	

VB.NET Code

Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
    mBrightness.Brightness = _defaultBrightness
    mContrast.Contrast = _defaultContrast
End Sub

The only problem now is to make sure that the sliders get updated as well. You could rewrite your acquisition FIFO changed event handler like this:

C# Code

private void mAcqFifo_Changed(object sender, CogChangedEventArgs e) {
     bool _needNewImage = false;

    if ( (e.StateFlags & CogAcqFifoStateFlags.SfBrightness) != 0) {
        _needNewImage = true;
        sldBrightness.Value = (int) (((ICogAcqFifo) sender).OwnedBrightnessParams.Brightness * 100);
    }
    if ( (e.StateFlags &' CogAcqFifoStateFlags.SfContrast) != 0) {
        _needNewImage = true;
        sldContrast.Value = (int) (((ICogAcqFifo) sender).OwnedContrastParams.Contrast * 100);
    }

    if (_needNewImage) {
        int trignum;
        cogDisplay1.Image = ((ICogAcqFifo) sender).Acquire(out trignum);
        numAcqs++;
        if (numAcqs > 4) {
            GC.Collect();
            numAcqs = 0;
    }
}

VB.NET Code

Private Sub mAcqFifo_Changed(ByVal sender As System.Object, ByVal e As Cognex.VisionPro.CogChangedEventArgs)
    Dim _needNewImage As Boolean = False

    If e.StateFlags And CogAcqFifoStateFlags.SfBrightness <> 0 Then
        _needNewImage = True
        sldBrightness.Value = CType(sender, ICogAcqFifo).OwnedBrightnessParams.Brightness * 100
    End If

    If e.StateFlags And CogAcqFifoStateFlags.SfContrast <> 0 Then
        _needNewImage = True
        sldContrast.Value = CType(sender, ICogAcqFifo).OwnedContrastParams.Contrast * 100
    End If

    If _needNewImage Then
        Dim trignum As Integer
        cogDisplay1.Image = mAcqFifo.Acquire(trignum)
        numAcqs += 1
        If numAcqs > 4 Then
            GC.Collect()
            numAcqs = 0
        End If
    End If
End Sub