LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to get the name of a duplicated control or created control and add a callback to it

Solved!
Go to solution

Hi All,

 

Depending on how many input points I need, I am duplicating a numeric or string input with the DuplicateCtrl function. My question is, how do I get the name and id of the control that was just created, and how can i assign a callback to it? Alternately, would it be better to create a fresh ctrl instead of duplicating?

 

Thanks in advance!

 

0 Kudos
Message 1 of 7
(4,322 Views)

Nevermind!

I created a fresh control with..


controlID = NewCtrl (panelHandle[MAIN], CTRL_STRING_LS, "", topCoord,leftCoord);
SetCtrlAttribute (panelHandle[MAIN], controlID, ATTR_MAX_ENTRY_LENGTH,-1);
SetCtrlAttribute (panelHandle[MAIN], controlID, ATTR_WIDTH, 50);

0 Kudos
Message 2 of 7
(4,318 Views)

Actually, there is one small thing that didn't work.

When getting the control name with "GetCtrlAttribute (panelHandle[MAIN], controlID, ATTR_CONSTANT_NAME,controlName);" It returns a blank string. Is this because the control was not created in the user interface? I noted that the control's name is restricted and can not be assigned.

0 Kudos
Message 3 of 7
(4,315 Views)
Solution
Accepted by topic author TurboMetrologist

Hi TurboMetrologist,

why are you trying to get the control constant name? Keep in mind that you cannot use that name to address the control; control names are actually simply macros in the include file associated to the UIR, that is, the compiler preprocesses the code and wherever it finds PANEL_CONTROL names substitutes the corresponding numeric value as parameters to the functions (and consequently 'control' parameter is an int and not a char*)

That is why NewCtrl () or DuplicateCtrl () functions return a control ID: it is the handle to the new object, our only way to manipulate it. You will need to store this handle into a non-volatile memory so that you can access it durng program life.

 

Additionally, there are other ways to assign some particular meaning to a control than swiching on the control constant name. You could for example use the callbackData parameter to differentiate between different copies of a control.

Let me explain with an example. You told that you need several inputs (dynamically created as you don't know in advance how many of them to use); a clean solution could be to:

  • Design a master copy of one control in the UIR editor, where it is easy to customize it; assign also a callback function, if it has to be common to all copies of the control
  • Programmatically assign a callbackData value to that control, e.g. SetCtrlAttribute (..., ..., ATTR_CALLBACK_DATA, (void *)1);
  • Duplicate the control and then assign a different callbackData
    for (i = 2; i < 5; i++) {
       handle = DuplicateCtrl (...);
       SetCtrlAttribute (..., handle, ATTR_CALLBACK_DATA, (void *)i);
    }

By operating this way, every time the control callback is fired by any control it will receive the assigned callbackData, and you will be able to differentiate your code by a simple switch:

switch ((int)callbackData) {

   case 1:    // The master control

      break;

   case 2:    // The first duplicated control

      break;

}



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 4 of 7
(4,298 Views)

Yes, controls that are created programmatically do not have constant names. A constant name is just a macro that resolves to a number. This number is the id of the control, which you use to identify the control when you need to pass it to some function.

 

If you create a control programmatically, whether you call NewCtrl or DuplicateCtrl, you have no need for this constant name macro. The id of the new control is returned by the function (the return value of NewCtrl or DuplicateCtrl). You use this id to refer to the new control.

 

(Darn... later than, and less thorough than, Roberto. Once again.)

 

Luis

0 Kudos
Message 5 of 7
(4,297 Views)

Thankyou for your help, my solution was to save the ID of each control into an array as soon as it is created to keep track of them and then do as Roberto said. Very simple.

0 Kudos
Message 6 of 7
(4,252 Views)

Buon Giorno Roberto,

 

(I've now exhausted my knowledge of Italian Smiley Very Happy). This post was detailed enough that I was able to recognize that it solved a problem I was facing. It is now a problem no longer. Molto grazie (if that is the correct thing to say).

 

Best regards,

Charles E. Gray

Charles E. Gray, Ph. D.
C. E. G. Consulting Services
647 Arboleda Drive
Los Altos, CA  94024-4115

650-814-9150 -- voice/voice-mail 
650-383-6657 -- Fax
cegcs@alum.mit.edu -- email
0 Kudos
Message 7 of 7
(4,023 Views)