12-08-2008 05:44 PM
I am trying to convert the label for a Control ID into the "Control ID int variable" so that I get sent an attribute in SetCtrlAttribute.
I have a number of controls checkboxes that have control id's CHBOX_1, CHBOX_2,....CHBOX_50. I wnat to create a string "CHBOX_" and append the appropriate # at the end and then be able to call SetCtrlAttribute (chnpnl, "CHBOX_#", ATTR_VISIBLE, 1); How can I convert my string label back to the Control ID name?
12-09-2008 02:34 AM
There is no way to get the control ID directly from a string that holds its constant name. What you can do, instead, is to iterate on all controls on a panel and get each control constant name, comparing it with your string, more or lass this way (it's pseudo-code, so it may need to be trimmed a little):
int id;
char name[256], yourName[256];
GetPanelAttribute (panelHandle, ATTR_PANEL_FIRST_CTRL, &id);
while (id) {
GetCtrlAttribute (panelHandle, id, ATTR_CONSTANT_NAME, name);
if (!strcmp (name, YourName)) {
// Your code here
}
GetCtrlAttribute (panelHandle, id, ATTR_NEXT_CTRL, &id);
}
Now, what to do in "your code" section is to be discussed, since calling this routine on all your 50 checkboxes each time you want to get their IDs may be a little cumbersome... You may want to fill an array of control IDs and then get the id from the array each time you want to access one or more controls. But if your controls are not dynamically created at runtime you may prefere to create a little snippet of code that fills the array and call itwhile starting the program:
int ctrlID[50];
ctrlID[0] = PANEL_CHECKBOX_1;
// and so on...
There is another method of accessing controls which has been discussed several times on this board: I mention it here even if I personally consider it more dangerous than beneficious. Control IDs are integer numbers in strict relation with the control z-plane order, which can be manually set on the UIR editor via Edit >> Tab Order... menu function (or Ctrl+T); supposing your checkboxes are ordered sequentially you could access them via a "first checkbox id + order" algorithm. Now, I consider it dangerous since the tab order could be altered easily if adding some control on the panel, changing its tab order or so; in this cases your algorithm will fail, possibly raising errors while trying to access a control different from the one expected. Moreover, maintenance of such an application over time could be difficoult since this action is to be *well* documented and transmitted to all programmers who will be working on it in the future. For this reason, even if I initially have used it, I very early moved to the other approach (array of IDs), more stable and easy to be maintained over time.
12-09-2008 08:57 AM
Roberto,
Thanks!
I was contemplating the tab order, but your method seems much better.