Making Device Settings

This chapter shows you how to create a custom device settings dialog. The code is provided in reusable form, thus it can be easily integrated into your own applications.

IC Imaging Control also offers a built-in dialog, which can be used to select and set up a device. If, however, the dialog provided by IC Imaging Control does not fit your needs, this chapter shows you how to create a custom dialog. It will first show you how to integrate the sample code into your own projects and then how to actually build the dialog.

Integrating the sample code into your project

The source code for the C# version of this sample program can be found under samples\C# *\Making Device Settings in your My Documents/IC Imaging Control 3.5 directory. To integrate the code into your project, you just have to add the file frmDeviceSettings.cs. Select Project from the menu and then select Add Existing Item.... Now browse to the sample folder and open the file frmDeviceSettings.cs.

To call the dialog from your application, you will need an instance of the dialog first. After the instance is created, assign the ICImagingControl variable (here: IcImagingControl1, or icImagingControl1 if you are using C#) to the dialog and then show the dialog. After this, Dispose should be called for the dialog:

[C#]
using( frmDeviceSettings DeviceDialog = new frmDeviceSettings( icImagingControl1 ) ) { DeviceDialog.ShowDialog(); }

Creating the Dialog

In this chapter, we will focus on the device settings dialog. In addition to the dialog, you will of course need an application that calls it. The sample code of this chapter contains a ready-to-run project with a small application that calls the dialog.

Assuming you have already set up a main project, you now have to add a new form to the project. This form will contain the elements of the custom dialog. To add a new form, select Project from the menu and then choose Add Form. Choose the Dialog type of form. Name the form frmDeviceSettings.

Now add 5 combo boxes to the form. Name them cboDevice, cboVideoNorm, cboVideoFormat, cboFrameRate and cboInputChannel. Also add a text box, called txtSerial and two check boxes named chkFlipV and chkFlipH.

In addition to the controls mentioned above, the sample dialog also contains a label named lblErrorMessage. The Visible property of this label is set to false, so that it will not be displayed when the dialog is called. The reason for this is explained when the Form_Load event is discussed.

image

Adding Functionality to the Dialog

First add some global variables:

[C#]
private TIS.Imaging.ICImagingControl imagingControl; private string deviceState; private const string not_available_text = "n\\a";

ImagingControl must be public since it is assigned from the main program before the dialog is called.

Now add a Form_Load event and insert the following code:

[C#]
private void frmDeviceSettings_Load( object sender, EventArgs e ) { if( imagingControl.DeviceValid ) { if( imagingControl.LiveVideoRunning ) { lblErrorMessage.Text = "The device settings dialog is not available while the live video is running.\n\nStop the live video first."; lblErrorMessage.AutoSize = false; lblErrorMessage.Padding = new Padding( 8 ); lblErrorMessage.SetBounds( 0, 0, 100, cmdOK.Top ); lblErrorMessage.Dock = DockStyle.Top; lblErrorMessage.Visible = true; return; } else { lblErrorMessage.Visible = false; } } SaveDeviceSettings(); UpdateDevices(); }

The Form_Load event checks, whether the live video is running. If it is, it reformats the dialog box to display a message to tell you that the dialog will not be displayed, while the live video is running. This behavior was implemented to point out that changing the device will cause side effects. The ring buffer collection ICImagingControl.ImageBuffers is reset when the device is changed. Therefore, image data in the collection is lost.

Now add the click events of the OK and the Cancel button and insert the following code accordingly:

[C#]
private void cmdOK_Click( object sender, System.EventArgs e ) { this.Close(); }
[C#]
private void cmdCancel_Click( object sender, System.EventArgs e ) { RestoreDeviceSettings(); this.Close(); }

As can be seen above, the Form_Load event calls a procedure SaveDeviceSettings and the CancelButton_Click event calls RestoreDeviceSettings.

The UpdateDevices procedure that is called on the Form_Load event updates the controls of the dialog, but this will be discussed later.

Now add the SelectedIndexChanged events for all 5 combo boxes and the click event for the 2 check boxes. On the SelectedIndexChanged event of the device combo box, the currently selected device must be changed and all other combo boxes, the check boxes and the output of the serial number text field must be updated. This is done with the following code:

[C#]
private void cboDevice_SelectedIndexChanged( object sender, System.EventArgs e ) { try { // Open the device if( cboDevice.Enabled ) { imagingControl.Device = cboDevice.Text; string serial; if( imagingControl.DeviceCurrent.GetSerialNumber(out serial) ) { txtSerial.Text = serial; } else { txtSerial.Text = not_available_text; } } // Get supported video norms, formats and inputs UpdateVideoNorms(); UpdateInputChannels(); UpdateFlip(); } catch( Exception ex ) { MessageBox.Show( ex.Message ); } }

After the new device is selected, its serial number is retrieved. If a serial number is not available, n/a is displayed. The procedures UpdateVideoNorms, UpdateInputChannels and UpdateFlip update the controls. They will be discussed later. Please note that the video format and the frame rate are updated implicitly by UpdateVideoNorms and UpdateVideoFormat.

The SelectedIndexChanged events for the other combo boxes are quite similar. The SelectedIndexChanged event for the cboVideoNorm combo box includes a procedure UpdateVideoFormats, which is used to update the video format combo box according to the selected video norm. The SelectedIndexChanged event for the cboVideoFormat combo box includes a procedure UpdateFrameRates that updates the frame rate combo box after the video format has changed. The other combo boxes just set the currently selected value. Here is the code for the video norm combo box:

[C#]
private void cboVideoNorm_SelectedIndexChanged( object sender, System.EventArgs e ) { try { if( cboVideoNorm.Enabled ) { imagingControl.VideoNorm = cboVideoNorm.Text; } UpdateVideoFormats(); } catch( Exception ex ) { MessageBox.Show( ex.Message ); } }

The SelectedIndexChanged events for the other combo boxes are implemented accordingly, except a minor detail regarding the frame rate combo box. If you assign the new value for the frame rate, make sure you do a typecast, since cboFrameRate.Text returns a string, but ICImagingControl.DeviceFrameRate needs a floating point value.

Finally, add click events for the check boxes. They just toggle the horizontal and vertical flip:

[C#]
private void chkFlipV_CheckedChanged( object sender, System.EventArgs e ) { if( imagingControl.DeviceFlipVerticalAvailable ) { imagingControl.DeviceFlipVertical = ( chkFlipV.Checked == true ); } }

The event for chkFlipH is implemented the same way.

The Update Procedures

As mentioned before, there is an Update procedure for every combo box and one for the two check boxes. The Update procedures for the combo boxes just fill the appropriate combo box with currently valid values. If the value that was selected before the update is still contained in the combo box, it is selected automatically. The code for the UpdateDevice procedure looks as follows:

[C#]
private void UpdateDevices() { cboDevice.Items.Clear(); if( imagingControl.Devices.Length > 0 ) { foreach( object Item in imagingControl.Devices ) { cboDevice.Items.Add( Item.ToString() ); } if( imagingControl.DeviceValid ) { cboDevice.SelectedItem = imagingControl.Device; } else { cboDevice.SelectedIndex = 0; } cboDevice.Enabled = true; } else { cboDevice.Items.Add( not_available_text ); cboDevice.Enabled = false; cboDevice.SelectedIndex = 0; } }

The Update procedures for the other combo boxes are implemented accordingly, but note that ImagingControl.DeviceFrameRates returns a collection that contains floating point values. Therefore, in the UpdateFrameRates procedure, you must typecast them into a string before you can add them to the combo box.

The UpdateFlip procedure checks whether there is a vertical or horizontal flip available with the current device and sets their current state if possible:

[C#]
private void UpdateFlip() { if( imagingControl.DeviceFlipHorizontalAvailable ) { chkFlipH.Enabled = true; if( imagingControl.DeviceFlipHorizontal ) { chkFlipH.Checked = true; } else { chkFlipH.Checked = false; } } else { chkFlipH.Enabled = false; chkFlipH.Checked = false; } if( imagingControl.DeviceFlipVerticalAvailable ) { chkFlipV.Enabled = true; if( imagingControl.DeviceFlipVertical ) { chkFlipV.Checked = true; } else { chkFlipV.Checked = false; } } else { chkFlipV.Enabled = false; chkFlipV.Checked = false; } } //> // ------------------------------------------------------------------------------ // UI events // ------------------------------------------------------------------------------ // // cboDevice_SelectedIndexChanged // // Get available inputs and video formats for the selected // device and enter the information in the respective combo // boxes. // //<<cboDevice private void cboDevice_SelectedIndexChanged( object sender, System.EventArgs e ) { try { // Open the device if( cboDevice.Enabled ) { imagingControl.Device = cboDevice.Text; string serial; if( imagingControl.DeviceCurrent.GetSerialNumber(out serial) ) { txtSerial.Text = serial; } else { txtSerial.Text = not_available_text; } } // Get supported video norms, formats and inputs UpdateVideoNorms(); UpdateInputChannels(); UpdateFlip(); } catch( Exception ex ) { MessageBox.Show( ex.Message ); } }

<< Programmer's Guide