-
Hello All,
how do i put the data of an int to an edit control. i know that when i use
char *data = "Hello";
i can use
SetWindowText(editbox1, data);
but i'm having trouble putting the data of an int to the edit control. it gives me an error, cannot convert int to const char.
i have no idea how to do this, and i have looked around and gone through the help files or my borland compiler, and cannot figure this out. i'm pretty sure this is easy. but i don't know how. please help. thanx for your time.
AAG.
-
You need something like this:
Code:
#include <cstdlib>
char buffer[100];
// your other code
// blah blah blah
int data = 3;
SetWindowText(editbox1, itoa(data, buffer, 10));
You will get much better responses posting questions like this in the C++ forum.
-
Erm, well to be honest, I've never really thought about it. if it was me I would probably just make my own formatting function. You could quite easily do that. Say you have a time interval in seconds and want to display it like hh:mm:ss then you could use a function like this to return a string in that format:
Code:
char *formatTime(int seconds, char *buffer)
{
int secs = seconds % 60;
int mins = (seconds / 60) % 60;
int hours = mins / 60;
char buf[100];
int hourlen = strlen(itoa(hours, buf, 10));
int temp=hours;
buffer[hourlen]=itoa(hours%10, buf, 10);
for(int x=(hourlen-1); x>=0; x++)
{
buffer[x] = (temp/10)%10;
temp = (temp/10)%10;
}
buffer[hourlen+1] = ':';
buffer[hourlen+2] = mins/10;
buffer[hourlen+3] = mins%10;
buffer[hourlen+4] = ':';
buffer[hourlen+5] = secs/10;
buffer[hourlen+6] = secs%10;
buffer[hourlen+7] = '\0';
return(buffer);
}
I don't guarantee that will work as it is, it's just off the top of my head. You'd have to pass it a properly allocated buffer string, that's at least long enough for the whole string plus the ening '\0' character.
-
Hmm.
Thanx, i'll try it out and play around with it, and see what i can come up with. i know its really simple in vb6. i'm pretty fluent in vb6, and have only been programming in c for a month or so. i just skimmed through the c++ console programming book and when i felt i knew enough, i jumped into win32 programming. maybe my book might have something on the subject. thanx alot for your help, i really appreciate it.
thanx again,
AAG
-
No problem :) I just realised at least one mistake I made, here's the corrected code:
Code:
char *formatTime(int seconds, char *buffer)
{
int secs = seconds % 60;
int mins = (seconds / 60) % 60;
int hours = mins / 60;
char buf[100];
int hourlen = strlen(itoa(hours, buf, 10));
int temp=hours;
buffer[hourlen]=itoa(hours%10, buf, 10);
for(int x=(hourlen-1); x>=0; x++)
{
buffer[x] = itoa((temp/10)%10, buf, 10);
temp = (temp/10)%10;
}
buffer[hourlen+1] = ':';
buffer[hourlen+2] = itoa(mins/10, buf, 10);
buffer[hourlen+3] = itoa(mins%10, buf, 10);
buffer[hourlen+4] = ':';
buffer[hourlen+5] = itoa(secs/10, buf, 10);
buffer[hourlen+6] = itoa(secs%10, buf, 10);
buffer[hourlen+7] = '\0';
return(buffer);
}
The function is now painfully inefficient, but never mind, it should (might) work :)
-
HMM WIERD.
Ok Well I Tried Your Revised Code. I got a couple of errors that i fixed by changing ':' to ":", those were the only 2 errors. then i called the function as follows
SetWindowText(edithwnd, formatTime(data, secbuffer));
all it does is exit the program when the code is run. i also set the buffer to [40]. it compiled without errors or warnings. the program opens fine. then when i hit the button that runs the code. the program exits. no error is reported, no illegal operation. it just exits as if u were to
PostQuitMessage(0);
thanx anyways
AAG.