Hey, sorry if this question is really really simple and silly to ask here but I can't find a suitable answer anywhere else and nobody out there so far is willing to help me out so any help would be greatly appreciated.

Simple thing I want to do:

I have a loop which creates a load of buttons. There are currently 3 buttons but at some point there will be 9. Each button will be numbered on the window (the first one will have "1" written on it, the second one "2", the third one "3" etc).

I am guessing the way of doing this in a loop would be to create a string and make sure that the number of the button created is what goes into the string. The string is then used on the button...

How do I do this? It sounds so simple but for some reason nothing has worked...

Thanks in advance.

Recommended Answers

All 3 Replies

Hey, sorry if this question is really really simple and silly to ask here but I can't find a suitable answer anywhere else and nobody out there so far is willing to help me out so any help would be greatly appreciated.

Simple thing I want to do:

I have a loop which creates a load of buttons. There are currently 3 buttons but at some point there will be 9. Each button will be numbered on the window (the first one will have "1" written on it, the second one "2", the third one "3" etc).

I am guessing the way of doing this in a loop would be to create a string and make sure that the number of the button created is what goes into the string. The string is then used on the button...

How do I do this? It sounds so simple but for some reason nothing has worked...

Thanks in advance.

Your loop counter will be an integer that rangers from 1 to 9. You can use the itoa function to convert that integer to a char* (assuming itoa exists, as it is not portable. If not, write your own or use sprintf), then convert the char* to a string. Then set the text of the button label to that string.

char buffer[20];
for (int i = 1; i <= 9; i++)
{
    itoa(i, buffer, 10);
    string buttonLabelText(buffer);
    // now set the button's label to buttonLabelText
}
commented: Didn't think it would be that easy for him. +5

Thank you, you have answered my question perfectly. You are a fantastic individual!

Thank you, you have answered my question perfectly. You are a fantastic individual!

You're welcome. Glad it helped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.