LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Radio buttons but using command buttons (with example source)

The LabWindows/CVI 2017 application I maintain uses Command Buttons as radio button style controls (only one can be clicked at a time). This is all being done manually with a bunch of code, so today I worked up a much simpler/smaller soltution.

 

But I wanted to ask: Is something like this already provided in LabWindows? I've seen how you can install an Instrument File (radioGroup.fp) and make them work. (Toolslib -> Radio Group).

 

Is there a way to use command buttons in a group like that?

 

For my test, I created my Command Buttons and assigned them all to the same callback routine (right side of this panel):

AllenInIowa_0-1770752475657.png

Then I have the routine see if the button that called it got released and, if so, press it back, else if was pressed then scan through an array of those buttons and release any that don't match. I use the index for the loop to know which button was selected. This code ignores buttons that are clicked that were already pressed, but remove a few lines and it can report over and over if the same button keeps getting clicked.

 

static int toggle_buttons[4] =
{
    PANEL_TOGGLEBUTTON_1,
    PANEL_TOGGLEBUTTON_2,
    PANEL_TOGGLEBUTTON_3,
    PANEL_TOGGLEBUTTON_4
};


int CVICALLBACK toggle_button_cb (int panel, int control, int event,
                                  void *callbackData, int eventData1, int eventData2)
{
    switch (event)
    {
        case EVENT_COMMIT:
            int selected = -1;
            
            // Get the state of the button that was clicked.
            int value = 0;
            
            GetCtrlVal (panel, control, &value);
            
            if (0 == value) // Click un-pressed it.
            {
                SetCtrlVal (panel, control, 1); // Press it back.
            }
            else // Click pressed it.
            {
                // Un-press the others and identify which one was cicked.
                for (int idx=0; idx<sizeof(toggle_buttons)/sizeof(*toggle_buttons); idx++)
                {
                    int temp_control = toggle_buttons[idx];

                    if (control != temp_control)
                    {
                        SetCtrlVal (panel, temp_control, 0); // Unpress it.
                    }
                    else
                    {
                        selected = idx; // Button that was clicked.
                    }
                }

                //handle_button_press (int selected);
            
                DebugPrintf ("Button %d selected.\n", selected);
            }
        break;
    }
    return 0;
}

 

Since we do this in multiple places in our user interface, I was going to write up a simple routine to handle this but thought I'd ask here first in case it is already provided somewhere.

 

Thanks...

0 Kudos
Message 1 of 3
(82 Views)

Here is a rough concept how how it might work as a function:

 

static int toggle_buttons[4] =
{
    PANEL_TOGGLEBUTTON_1,
    PANEL_TOGGLEBUTTON_2,
    PANEL_TOGGLEBUTTON_3,
    PANEL_TOGGLEBUTTON_4
};

// Used to set the state.
int button_radio_group_set (int panel, int *toggle_buttons, int num_toggle_buttons, int selected);
// Used to update to a new state.
int button_radio_group_update (int panel, int control, int *toggle_buttons, int num_toggle_buttons);

// Functions:
int button_radio_group_set (int panel, int *toggle_buttons, int num_toggle_buttons, int selected)
{
    int control = toggle_buttons[selected];
    
    return button_radio_group_update (panel, control, toggle_buttons, num_toggle_buttons);
}


int button_radio_group_update (int panel, int control, int *toggle_buttons, int num_toggle_buttons)
{                                           
    int selected = -1;
    
    // Get the state of the button that was clicked.
    int value = 0;
    
    GetCtrlVal (panel, control, &value);
    
    if (0 == value) // Click un-pressed it.
    {
        SetCtrlVal (panel, control, 1); // Press it back.
    }
    else // Click pressed it.
    {
        // Un-press the others and identify which one was cicked.
        for (int idx=0; idx<num_toggle_buttons; idx++)
        {
            int temp_control = toggle_buttons[idx];

            if (control != temp_control)
            {
                SetCtrlVal (panel, temp_control, 0); // Unpress it.
            }
            else
            {
                selected = idx; // Button that was clicked.
            }
        }
    }
    
    return selected;
}


// Callback using the function:
int CVICALLBACK toggle_button_cb (int panel, int control, int event,
                                  void *callbackData, int eventData1, int eventData2)
{
    switch (event)
    {
        case EVENT_COMMIT:
            int selected = -1;
            
            selected = button_radio_group_update (panel, control,
                &toggle_buttons[0], sizeof(toggle_buttons)/sizeof(*toggle_buttons)); 

            if (selected >= 0)
            {
                // handle_button (selected);
                DebugPrintf ("Button %d selected.\n", selected);
            }
        break;
    }
    return 0;
}

 

No error checking at all, so don't try to use this code as-is. 😉

 

I would probably also write a "get" function to retrieve the current state.

 

If LabWindows does not have something like this, I will forge ahead with my concept code and start using it in my application.

0 Kudos
Message 2 of 3
(78 Views)

You could take a look to radioBtnGrp instrument and associated example radioBtnGrpCtrlArray: it still uses radio buttons but since it is not based on the tree control it may possibly adapted to use toggle buttons.

 

The instrument is located in <CVIfolder>\toolslib\custctrl folder, while the example can be found in <CVISamplesFolder>\userint\custctrl\radioBtnGrp folder



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 3 of 3
(34 Views)