LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How does the .h file macro map to CVICALLBACK control parameter?

Solved!
Go to solution

I created 8 command buttons and created same callback for all the buttons. Also have a 2x4 array initialized to value of 1. When user clicks any button the 2x4 array value should change to 0 (as command button does not change value to 1 when selected). In order to fill the array I thought of using control from CVICALLBACK. 

 

int ctr_btns[2][4] = {{1,1,1,1},
                                        {1,1,1,1}};
                                        
int CVICALLBACK Button_cb (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)

  int row = -1;

    int val = 1;

switch (event)

    {

case EVENT_COMMIT:

                     GetCtrlVal (panelHandle, control, &val);

                      if(control >= 2 && control <= 5)

                                row = 0;

                      else if(control >= 6 && control <= 9)

                                   row = 1;

                      else if(control >= 10 && control <= 13)

                                    row = 2;

                       else

                                     row = 3;

                        ctr_btns[row][control] = val;

                   break;

    }

The issue is that even after I changed the #define value of the control in .h file, it does not uses the defined value. Where is the control parameter value coming from?

 

TIA.

 

*************************************************
CLD
*************************************************
0 Kudos
Message 1 of 5
(3,298 Views)

Hi, this is a common isue that has been discussed several times on the forums and does not have a fast response.

 

First of all, you should not modify the include file associated to the UIR file, as stated on top of it, since the system automatically regenerates it every time it saves the UIR. Additionally, the ID assigned to the controls depends on the tab order you define on the panel, so it is subject to change. FYI, this is a discussion on this subject that you may want to read: as you can see, it originates from a question very similar to yours.

Despite what I have said, control IDs actually can be used to discriminate between controls, so in your situation, and provided the buttons are assigned a progressive ID with the tab editor (Ctrl+T on the panel and assign the IDs clicking on the controls), your code could be rewritten this way:

 

 

    if(control >= PANEL_BUTTON1 && control <= PANEL_BUTTON4)
        row = 0;
    else if(control >= PANEL_BUTTON5 && control <= PANEL_BUTTON8)
        row = 1;
    else if(control >= PANEL_BUTTON9 && control <= PANEL_BUTTON12)
        row = 2;
    else
        row = 3;

 

For the same reason, you cannot use 'control' as an index on the array, as you have no possibility to foreseen the ID value. That is, even if rewritten the way I told you, that code is not likely to work... Smiley Sad

 

In my opinion the correct way of discriminating between controls is to add a switch inside the callback:

 

switch (control) {
   case PANEL_BUTTON1: ctr_btns[0][0] = 1; break;
   case PANEL_BUTTON2: ctr_btns[0][1] = 1; break;
...
   case PANEL_BUTTON5: ctr_btns[1][0] = 1; break;
...
}

( but you should create a 4x4 array of values).

 

 



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 2 of 5
(3,293 Views)

An alternative to all that, if you are using CVI2010, is the use of control arrays: here a brief explanation of how they work.



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 5
(3,290 Views)

Thanks Roberto,

 

As you mention that ID's can change if the tab order gets change. Are there any situations where CVI can change taborder automatically even if I assign a progressive ID (using tab ordering) to controls.

Could you also elaborate on the last line "you should use 4X4 array"? is this in reference with using switch(control)?

TIA.

*************************************************
CLD
*************************************************
0 Kudos
Message 4 of 5
(3,273 Views)
Solution
Accepted by topic author lvrat

The value corresponding to control ID may change if you add or delete controls or if you change the tab order; once I learned this, I stopped using it so I cannot be sure that these are the only conditions when this occurs. In my opinion they are enough to avoid using the numeric ID values at all.

 

The 4x4 matter derives from your code: you are calculating 4 rows each of them made by 4 elements. I tried to follow this approach in my switch statement, but you really must adapt it to your situation and UIR structure.



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?
Message 5 of 5
(3,267 Views)