Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Index of switcharray

Private Sub SwitchArray1_ValuesChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SwitchArray1.ValuesChanged

Dim SwitchState() As Boolean

SwitchState = SwitchArray1.GetValues

LedArray1.SetValues(SwitchState)

'textbox1.Text= WHAT CODE DO I USE TO SHOW WHICH SWITCH WAS CHANGED??

End Sub

In Visual basic 2005 with Measurement Studio 8.1, when a switch in a switch array is changed, how do I know which switch was changed?
Lets say I had a switcharray with 5 switches.  I want textbox1 to show a number from 0 to 4 indicating which switch was changed.
 
This may be a simple, stupid question, but in Visual Basic 6 it was just the index of the array.
I have no Idea on how to get the number in VB.net 2005
0 Kudos
Message 1 of 5
(3,542 Views)

Hi lburberry,

You just need to compare the previous set of Boolean values to the most recent set that is returned by GetValues in your ValuesChanged event. 

Dim oldValues() As Boolean = Nothing
Dim firstTime As Boolean = False

Private Sub ValuesChanged(......
  If (firstTime) Then
    Dim newValues() As Boolean = Nothing
    newValues = SwitchArray1.GetValues()
    Dim changedValue As String = String.Empty

    For index As Integer = 0 To oldValues.Length - 1
        If (oldValues(index) <> newValues(index)) Then
            changedValue = (index + 1).ToString()  ' Adding one in case you don't won't the index offset
            Exit For
        End If
    Next

    TextBox1.Text = changedValue
End If
End Sub

' I decided to set the oldValues to the default state when the Form Load event occurs; You can do whatever you want
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    oldValues = SwitchArray1.GetValues() 
    firstTime = True
End Sub

Hope this helps!

Best Regards,



Message Edited by Jonathan N on 04-07-2008 01:13 PM
Jonathan N.
National Instruments
0 Kudos
Message 2 of 5
(3,539 Views)
Yes, that is the brute force method of finding the changed value.  Is there no property such as childindex or any direct way of getting the index from the sender?
0 Kudos
Message 3 of 5
(3,536 Views)
Hi Iburberry,

It would be nice if the ValuesChanged event for the SwitchArray returned the control that changed along with its index in the collection but unfortunately it currently does not. When we created the SwitchArray, this just wasn't a property we thought customers would want access to without do a little programming.  However, I don't see a reason why we can't add this in so I'll add this suggestion to our list of features to implement.

Another option as well that I forgot about is that you can hook up to the ItemPropertyChanged event. This event is firect when a property of an item in the array changes.  The great benefit of this event is that it actually sends in the Control that changed along with the property that changed in the EventArgs parameter.  You still have to do a little programming but I like this approach better than my initial post.  So you can do something like:

Private Sub SwitchArray1_ItemPropertyChanged(ByVal sender As Object, ByVal ........) Handles SwitchArray1.ItemPropertyChanged

    Dim propertyName As String = e.PropertyName

    If (String.Compare(e.PropertyName, "Value") = 0) Then
      Dim index As Integer

      index = SwitchArray1.Controls(0).Controls.IndexOf(e.Control)
      TextBox1.Text = (index + 1).ToString()
    End If

End Sub


One thing to to mention is that the ItemPropertyChanged event has generic event args. If an event has generic event args, it is not displayed in the property grid in the VS 2005 Windows Forms designer. This is a bug with VS 2005. 

Best Regards,


Message Edited by Jonathan N on 04-09-2008 09:17 AM
Jonathan N.
National Instruments
Message 4 of 5
(3,512 Views)

Thank you.  It works!

That is the syntax I was looking for. 

 

Lee

 

0 Kudos
Message 5 of 5
(3,509 Views)