0

We are trying to modify a program to control a usb relay device. Essentially what the program originally does is give you a series of check boxes that you check to make the relay fire. We tried to modify the code so that the box is checked after 5 seconds using a counter and an if statement. It works, but only partially, what happens is the check box shows a check, but the even that is normally triggered does not happen. I'm hoping someone can help as my knowledge of VB stops here. I've highlighted the part we added in red. Thanks!

Public Class Form1
    Dim WithEvents phidgetIFK As Phidgets.InterfaceKit

    Private digiInArray As CheckBox()
    Private digiOutArray As CheckBox()
    Private sensorInArray As TextBox()

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
    End Sub

    'initialize the device
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        makeDigiInArray()
        makeDigiOutArray()
        makeSensorInArray()

        inputTrk.Value = 0
        inputTrk.Enabled = False
        sensitivityTxt.Text = ""

        ratioChk.Enabled = False
        ratioChk.Checked = False

        'To reduce code complexity we assume that there is one PhidgetInterfacekit
        'attached to the PC before the program is run.
        Try
            phidgetIFK = New Phidgets.InterfaceKit
            phidgetIFK.open()
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try


    End Sub

    'attach event handler... here we'll display the interface kit details as well as determine how many output and input
    'fields to display as well as determine the range of values for the output simulator slider
    Private Sub phidgetIFK_Attach(ByVal sender As Object, ByVal e As Phidgets.Events.AttachEventArgs) Handles phidgetIFK.Attach

        attachedTxt.Text = phidgetIFK.Attached.ToString()
        nameTxt.Text = phidgetIFK.Name
        serialTxt.Text = sender.SerialNumber.ToString()
        versionTxt.Text = sender.Version.ToString()
        digiInNumTxt.Text = phidgetIFK.inputs.Count.ToString()
        digiOutNumTxt.Text = sender.outputs.Count.ToString()
        sensorInNumTxt.Text = sender.sensors.Count.ToString()

        Dim i As Integer
        For i = 0 To phidgetIFK.inputs.Count - 1
            digiInArray(i).Visible = True
        Next i

        For i = 0 To phidgetIFK.outputs.Count - 1
            digiOutArray(i).Visible = True
            digiOutArray(i).Enabled = True
        Next i

        For i = 0 To phidgetIFK.sensors.Count - 1
            sensorInArray(i).Visible = True
        Next i

        If phidgetIFK.sensors.Count > 0 Then
            inputTrk.Enabled = True
            inputTrk.SetRange(0, 1000)
            inputTrk.Value = phidgetIFK.sensors(0).Sensitivity
            sensitivityTxt.Text = inputTrk.Value.ToString()

            phidgetIFK.ratiometric = True

            ratioChk.Enabled = True
            ratioChk.Checked = phidgetIFK.ratiometric
        End If
    End Sub

    ''ifkit detach event handler... here we display the statu, which will be false as the device is not attached.  We
    ''will also clear the display fields and hide the inputs and outputs.
    Private Sub phidgetIFK_Detach(ByVal sender As Object, ByVal e As Phidgets.Events.DetachEventArgs) Handles phidgetIFK.Detach

        attachedTxt.Text = phidgetIFK.Attached.ToString()
        nameTxt.Text = ""
        serialTxt.Text = ""
        versionTxt.Text = ""
        digiInNumTxt.Text = ""
        digiOutNumTxt.Text = ""
        sensorInNumTxt.Text = ""

        Dim i As Integer

        For i = 0 To 15
            digiInArray(i).Visible = False
            digiInArray(i).Checked = False
        Next

        For i = 0 To 15
            digiOutArray(i).Visible = False
            digiOutArray(i).Checked = False
            digiOutArray(i).Enabled = False
        Next

        For i = 0 To 7
            sensorInArray(i).Visible = False
            sensorInArray(i).Text = ""
        Next

        inputTrk.Value = 0
        inputTrk.Enabled = False
        sensitivityTxt.Text = ""

        ratioChk.Enabled = False
        ratioChk.Checked = False
    End Sub

    Private Sub phidgetIFK_Error(ByVal sender As Object, ByVal e As Phidgets.Events.ErrorEventArgs) Handles phidgetIFK.Error
        MessageBox.Show(e.Description)
    End Sub

    'digital input change event handler... here we check or uncheck the corresponding input checkbox based on the index of
    'the digital input that generated the event
    Private Sub phidgetIFK_InputChange(ByVal sender As Object, ByVal e As Phidgets.Events.InputChangeEventArgs) Handles phidgetIFK.InputChange
        digiInArray(e.Index).Checked = e.Value
    End Sub

    ''digital output change event handler... here we check or uncheck the corresponding output checkbox based on the index of
    ''the output that generated the event
    Private Sub phidgetIFK_OutputChange(ByVal sender As Object, ByVal e As Phidgets.Events.OutputChangeEventArgs) Handles phidgetIFK.OutputChange
        digiOutArray(e.Index).Checked = e.Value
    End Sub

    ''sensor input change event handler...Set the textbox content based on the input index that is communicating with the
    ''interface kit
    Private Sub phidgetIFK_SensorChange(ByVal sender As Object, ByVal e As Phidgets.Events.SensorChangeEventArgs) Handles phidgetIFK.SensorChange
        sensorInArray(e.Index).Text = e.Value.ToString()
    End Sub

    'Modify the digital ouputs...Please observe the properties window in the ofrm designer for the digital output checkboxes.
    'Each of the output checkboxes CheckedChanged events point to this one event handler, I use the sender object (the checkbox
    'triggering the event) to get the checkbox that is changing.  Also note that there is a "tag" property that is basically
    'user defined data associated with the control.  I used this to store the output index that the checkbox is supposed to
    'represent for use in the following code to easily index the output we wanted to change.  Hopefully this clarifies what
    'was done in this method.
    Private Sub ClickOutputs(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim outputIndex As Integer
        Dim outputState As Boolean
        outputIndex = DirectCast(sender, CheckBox).Tag
        outputState = DirectCast(sender, CheckBox).CheckState
        'MessageBox.Show(outputIndex.ToString() + " and " + outputState.ToString())

        phidgetIFK.outputs(outputIndex) = outputState

    End Sub

    Private Sub inputTrk_Scroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles inputTrk.Scroll
        Try
            Dim i As Integer
            For i = 0 To phidgetIFK.sensors.Count - 1
                phidgetIFK.sensors(i).Sensitivity = inputTrk.Value
            Next
            sensitivityTxt.Text = inputTrk.Value.ToString()
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString())
        End Try
    End Sub

    Private Sub ratioChk_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ratioChk.CheckedChanged
        If phidgetIFK.Attached Then
            phidgetIFK.ratiometric = ratioChk.Checked
        End If
    End Sub

    'When the application is terminating, close the Phidget.
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        RemoveHandler phidgetIFK.Attach, AddressOf phidgetIFK_Attach
        RemoveHandler phidgetIFK.Detach, AddressOf phidgetIFK_Detach
        RemoveHandler phidgetIFK.Error, AddressOf phidgetIFK_Error
        RemoveHandler phidgetIFK.InputChange, AddressOf phidgetIFK_InputChange
        RemoveHandler phidgetIFK.OutputChange, AddressOf phidgetIFK_OutputChange
        RemoveHandler phidgetIFK.SensorChange, AddressOf phidgetIFK_SensorChange
        Application.DoEvents()

        phidgetIFK.close()
    End Sub

    'this method creates the digital output array that corresponds to the group of checkboxes
    'we are using to represent the state of the digital outputs on the interface kit
    Sub makeDigiOutArray()
        ReDim digiOutArray(15)

        digiOutArray(0) = checkBox16
        digiOutArray(1) = checkBox17
        digiOutArray(2) = checkBox18
        digiOutArray(3) = checkBox19
        digiOutArray(4) = checkBox20
        digiOutArray(5) = checkBox21
        digiOutArray(6) = checkBox22
        digiOutArray(7) = checkBox23
        digiOutArray(8) = checkBox24
        digiOutArray(9) = checkBox25
        digiOutArray(10) = checkBox26
        digiOutArray(11) = checkBox27
        digiOutArray(12) = checkBox28
        digiOutArray(13) = checkBox29
        digiOutArray(14) = checkBox30
        digiOutArray(15) = checkBox31

        Dim i As Integer
        For i = 0 To 15
            digiOutArray(i).Visible = False
            AddHandler digiOutArray(i).Click, AddressOf ClickOutputs
        Next i

    End Sub

    Private Sub checkBox16_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkBox16.CheckedChanged

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ListBox1.Items.Add(ListBox1.Items.Count)

        If ListBox1.Items.Count > 5 Then
            checkBox16.CheckState = CheckState.Checked
        Else
            checkBox16.CheckState = CheckState.Unchecked
        End If
    End Sub
End Class
2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by Oxiegen
1

Instead of CheckState, try using the property Checked.
This will trigger the checkBox16_CheckedChanged event.

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ListBox1.Items.Add(ListBox1.Items.Count)

        If ListBox1.Items.Count > 5 Then
            checkBox16.Checked = True
        Else
            checkBox16.Checked = False
        End If
    End Sub
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.