开发者

Use numerical IDs of text boxes to initialize DoDataExchange?

So I have close to 300 variables (just right now), and I have numerically ordered their IDs in Resource.h so it's:

开发者_运维知识库
#define IDC_BOX1     1
#define IDC_BOX2     2
#define IDC_BOX3     3
#define IDC_BOX4     4

etc. My question involves the DoDataExchange that I'm performing for each different dialog that I have that contains all of these variables. I REALLY don't want to go through doing the following for each variable:

DDX_CText(pDX, IDC_BOX1, m_nBox1);
DDX_CText(pDX, IDC_BOX2, m_nBox2);
DDX_CText(pDX, IDC_BOX3, m_nBox3);
DDX_CText(pDX, IDC_BOX4, m_nBox4);

because that's just ridiculous.

How can I do something along the same lines as this:

for(int i = 0; i < **totalVariables**; i++)
     DDX_CText(pDX, **nameByIdInResourceFile(i)**, **indexOfVariableNameInArray**;

I'm sure this is possible, I just don't know what the function might be that pulls the IDC_... variable names by their ID number. Any thoughts?


Since you've gone to the trouble of creating your resource IDs consecutively and in order, it's easy to go through all of them in the loop:

for(int i = 0; i <= (IDC_BOX300-IDC_BOX1); i++)
     DDX_CText(pDX, IDC_BOX1+i, ...

Naturally this will fail if someone comes along and adds IDC_BOX301 and doesn't put it in the sequence properly, so be careful!

The simplest solution for the variable names is to replace the individual variables with an array.

for(int i = 0; i <= (IDC_BOX300-IDC_BOX1); i++)
     DDX_CText(pDX, IDC_BOX1+i, m_nBoxes[i]);


@Mark Ransom's answer is great. I do exactly what he suggests, but I have one other thing I do as well. During my app's startup (guarded by an #ifdef DEBUG), I have some code that verifies that all of my IDs are in consecutive numerical order. That way, I can be sure that someone (likely me in the future) doesn't come along and add an out-of-numerical-sequence ID.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜