Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

C# How do I interlock buttons in a SwitchArray

Hello everyone,
I wonder if anyone can help me?

I am working in C# 2005 writing Windows Forms, using MCC Measurement Studio version 8.0.1.334
 
I would like to add a SwitchArray, containing 5 buttons, to be used as a function selector.  Functions could be 'Frequency analysis','Statistics' etc.
 
A basic requirement is that the user can only select one button at a time.
 
My problem is how do I interlock the buttons, so that when one is selected, the previously selected one is deselected?
 
Everything that I have tried so far results in a 'Stack Overflow' error, presumably because the ValuesChanged event is being repeatedly triggered.
 
The only way I can see to make this work is to use a short duration timer and a set of flags to allow the setting of the switcharray the first time and inhibit any further changes.
 
A synopsis of my code is below:
 

private void switchArray1_ValuesChanged(object sender, EventArgs e)

{

timer1.Enabled =

true; // timer runs on 100ms interval

if( !InterlockFlag ) // Don't run InterlockButtons if flag is false

InterlockButtons();

//

}

private void InterlockButtons()

{

InterlockFlag =

true; // Set main flag to inhibit further calling of this function

InInterlockFlag =

true; // This flag stops the timer affecting things while the function is running

NowSwitch = switchArray1.GetValues();

// Get the current settings of the switcharray

for (int i = 0; i < NowSwitch.Length; i++) // Exclusively OR current status with last known settings

{

CompareArray[i] = NowSwitch[i] ^ LastSwitch[i];

}

switchArray1.SetValues(CompareArray);

// Set new state for switcharray

LastSwitch = CompareArray;

// Store current state for next time round

InInterlockFlag =

false; // Flag shows we are out of the function

}

private void timer1_Tick(object sender, EventArgs e)

{

// Stop the timer and reset the flag, ready for the next button change.

if(( InInterlockFlag == false ) & ( InterlockFlag == true))

{

timer1.Enabled = false;

InterlockFlag=

false;

}

}

This really seems like a seriously messy way of doing things, but at least it does work.
 
Is there a better way to do this?
 
Many thanks
John822179

Live long, prosper and understand Measurement Studio?
0 Kudos
Message 1 of 4
(4,034 Views)
Hi jw822179,

There is no built-in event or method that will get the behavior you are wanting and thus some coding is necessary. Shown below is some sample code that will give you the desired behavior you want. I wanted to point out that you don't need to do anything with a timer.  
 
private bool[] oldValues = new bool[3];
private bool isLocked = false;


private void switchArray1_ValuesChanged(object sender, EventArgs e)
{
     if (!isLocked)
     {
         bool[] newValues = switchArray1.GetValues();
         bool[] adjustedValues = new bool[oldValues.Length];

         for (int i = 0; i < oldValues.Length; ++i)
         {
             adjustedValues[i] = oldValues[i] ^ newValues[i];
         }

         isLocked = true;
         switchArray1.SetValues(adjustedValues);
         isLocked = false;
         oldValues = switchArray1.GetValues();
      }
}


With all that said, I can see this being a useful feature as well.

Hope this helps!

Best Regards,
Jonathan N.
National Instruments
0 Kudos
Message 2 of 4
(4,015 Views)
Hi jw822179,

I also wanted to let you know that a feature request has been filed for this behavior. The ID is 4E276T0F.

Best Regards,
Jonathan N.
National Instruments
0 Kudos
Message 3 of 4
(4,014 Views)
Hi Jonathan,
 
Thanks for your reply.
 
This is better!  Many thanks.
 
I am surprised that no one else has suggested an interlocking mechanism!  This is the first time that I have tried to use a switchArray and automatically assumed that they would interlock! 🙂
 
Thanks for suggesting that it be added!
 

Many thanks
John822179
Live long, prosper and understand Measurement Studio?
0 Kudos
Message 4 of 4
(4,000 Views)