Link to home
Start Free TrialLog in
Avatar of andyw27
andyw27

asked on

Create controls via a for loop.

I'm loking for suggestions as to how I can create static text controls by using a loop.

I'm having some success in what I tried up to now, however one problem I need to correct is that the name of the control is currently:

CStatic *text1 = new CStatic;

I need to change this so that it using the variable from the loop.  In this case the variable for the loop is an int called i  Otherwise when I run it it displays one control then just over writes the previous one and so on..

Thanks.
Avatar of jkr
jkr
Flag of Germany image

That could be like

CStatic* texts[10];

uint nIdBase = IDC_STATIC;
RECT rect = { 100, 100, 400, 200};

for (i = 0; i < 10; ++i) {

  CString str;

  str.Format("Text%d", i);

  texts[i] = new CStatic;

  texts[i]->Create((LPCTSTR)str,0,rect,this,unIdBase + 1);

  rect.top += 150; // next CStatic will be created 150 pixels below
}
ASKER CERTIFIED SOLUTION
Avatar of bijopuli
bijopuli
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial