LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Detect which button in an Event case

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:

LabVIEWScreenSnapz001.png

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.)

0 Kudos
Message 1 of 7
(5,345 Views)

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).

0 Kudos
Message 2 of 7
(5,336 Views)

"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.

Matt J | National Instruments | CLA
0 Kudos
Message 3 of 7
(5,334 Views)

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

0 Kudos
Message 4 of 7
(5,326 Views)
  • Don't use mouse action on booleans, do a "value changed" event.
  • Latch action booleans belong inside their event case so they can properly reset.
  • Yes, check the control reference from the event data node.

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!)

 

TwoButtons.png

0 Kudos
Message 5 of 7
(5,315 Views)

Here's another way to do it that's more complicated but scales to any amount of buttons:

Register array of buttons.png

0 Kudos
Message 6 of 7
(5,300 Views)

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).

 

FourButtons.png

 

0 Kudos
Message 7 of 7
(5,296 Views)