08-10-2018 02:48 PM
In my VI I have two buttons that do almost the same thing, but using different numeric values. From a code efficiency point of view it would make a lot of sense to service them with the same event case – provided that I can use some portion of the event attributes to tell me which button was pressed. Illustration showing what I mean by attributes:
The one called "button" would seem to do the trick – but it always gives me value 1 no matter what button I press. Looking in the button's properties I can't see any way to assign it a value. Is there anything in these attributes that would accomplish what I want, reliably? (I consider the coordinates too clumsy, they might get moved around.)
08-10-2018 03:08 PM
Why not use the control reference and get the label text? (Or some other ID from the reference - the reference itself is also unique, so you could also use that).
08-10-2018 03:09 PM
"Button" is the mouse button that you are pressing. I think left-click is 1 but you can use this to capture a right-click or middle-click.
You have the control reference so you could use that to get the name of the control that was pressed.
08-10-2018 03:23 PM
Don't handle both Controls in the same Event. Have a Button 1:Mouse Up Event, and wire whatever you want as the Value, and do the same with Button 2:Mouse Up. Let LabVIEW handle this for you -- you can move the buttons all over the place, rename them as you will, etc., but the code will continue to work (and will be trivial to understand and debug).
Bob "Simpler is Better" Schor
08-10-2018 04:10 PM - edited 08-10-2018 04:31 PM
Attached is a very simple example how it could be done (the timeout case just resets the indicators after 400ms).
Still, if there are only two buttons, just use the value (since the event can only fire for one of the two, checking one is sufficient. And since the buttons are latch action, the pressed button will be TRUE, guaranteed!)
08-10-2018 04:53 PM
Here's another way to do it that's more complicated but scales to any amount of buttons:
08-10-2018 05:36 PM - edited 08-10-2018 05:37 PM
If there are several mutually exclusive buttons, the right way would be to use a radiobutton control. No other code needed and it can be wired directly to a case structure.
@Kyle97330 wrote:
Here's another way to do it that's more complicated but scales to any amount of buttons:
It does not really scale well if you have to create "any amount of control references". Starts getting tedious. 🙂
Full scalability can be achieved by using arrays of buttons, e.g. as follows. Now the code would not look any different with 20 buttons. 😄 Latch action can be achieved by resetting. (For more artistic freedom (buttons with different labels/colors etc.), a cluster of booleans could be used, the result of the "not requal" to be turned into an array for detection in a similar way).