Sign in to follow this  

Array of Controls

This topic is 3314 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I want to create several controls dynamically in Win32 C++. Because typing out 9*9 controls and edit their position is quite difficult, I would like to create them using a for loop:
  unsigned short int x, y;

  for(x=0; x<9; x++) {
    for(y=0; y<9; y++) {
      
      // Code to create controls
      STATIC_xy = CreateWindow(
           "STATIC",
           "-",
           WS_VISIBLE | WS_CHILD,
           10 + x, 10 + y, 30, 30,
           hwnd,
           (HMENU) ID_STATIC_xy,
           NULL,
           NULL);
    }
  }
However, I'm stuck right there, how can I move on. All articles on this on internet are either C#, Visual C or MFC. Could anyone give me a push here? :=)

Share this post


Link to post
Share on other sites
Congratulations, you chose one of the few programming languages that don't allow dynamic variable naming. :) As I understand it, for the most part you can only do that with interpreted languages, so you're going to have to be more clever about it.

I'm not familiar with Win32 programming, but since CreateWindow just returns a window handle, it seems to me that a simple array will do the trick, especially since you know exactly how many you want to create.

Share this post


Link to post
Share on other sites
Quote:
Original post by brechtjah
Quote:
Original post by Sneftel
What is "STATIC_xy"?


This is what I meant by STATIC_xy, it should be a dynamic name:
STATIC_00
STATIC_01
STATIC_02
...
STATIC_99


C++ doesn't work that way; the variable names don't "exist" in the compiled code.

The natural way to accomplish this sort of thing is with a container. You can use one from the standard library, such as std::vector, or an array, which is built-in.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
Quote:
Original post by brechtjah
Quote:
Original post by Sneftel
What is "STATIC_xy"?


This is what I meant by STATIC_xy, it should be a dynamic name:
STATIC_00
STATIC_01
STATIC_02
...
STATIC_99


C++ doesn't work that way; the variable names don't "exist" in the compiled code.

The natural way to accomplish this sort of thing is with a container. You can use one from the standard library, such as std::vector, or an array, which is built-in.


Would it then still be better (here better not in the meaning of efficient) to create all 81 controls?

Share this post


Link to post
Share on other sites
Quote:
Original post by godsenddeath
what you would have to do is this


// for example's sake, x = 5

HWND *controlList = new HWND[x]; //dynamicly allocate(create) enough room
//for your list of controls

for(i = 0;i<x;i++)
{
controlList[i] = CreateWindow(fill this with stuff);
}

You forgot to delete [] the data you newed.

I would strongly consider the use of a standard container instead of raw memory management. In this case, std::vector<> with some clever indexing or boost::multi_array could work.

Share this post


Link to post
Share on other sites
Thanks for all the help, however, I've bumped into a problem I don't understand. This is my code:

unsigned short int row, column, i;
row = 9;
column = 9;
i = 0;
HWND *controlList = new HWND[row][column];

for(row=0;row<9;row++) {
for(column=0;column<9;column++) {
controlList[row][column] = CreateWindow("EDIT",
NULL,
ES_CENTER | WS_VISIBLE | WS_CHILD,
10+row*30, 10+column*30, 20, 20,
hwnd,
(HMENU) 400+i,
NULL,
NULL);
i++;
}
}


It gives me the following error (Dev Cpp):
'column' cannot appear in a constant expression
I've never seen this error, some articles on Google speak of doing something tat is not allowed by the standards but why wouldn't you be able to do the above code?

EDIT: I solved it like this, but why won't it accept a two dimensional array?
unsigned short int row, column, i;
row = 9;
column = 9;
i = 0;
HWND *controlList = new HWND[81];

for(row=0;row<9;row++) {
for(column=0;column<9;column++) {
controlList[i] = CreateWindow("EDIT", // Window Type
NULL, // Text
ES_CENTER | WS_VISIBLE | WS_CHILD,
10+row*30, 10+column*30, 20, 20, // x, y, width, height
hwnd, // Parent Handle
(HMENU) 400+i, // Window ID
NULL,
NULL);

i++;
}
}


[Edited by - brechtjah on November 15, 2008 5:48:59 AM]

Share this post


Link to post
Share on other sites

This topic is 3314 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this