Printing integers and control array

General help with the Ecere Cross Platform GUI toolkit: Window, common controls, events, etc.
Help with the 2D Graphics library: Surface, Display, Bitmap, Font and others.
Post Reply
D.Bane
Posts: 78
Joined: Thu Jul 29, 2010 2:51 pm

Printing integers and control array

Post by D.Bane »

I guess a lot of you may find this stupid to ask, but since I have a problem with it, I really need some help..

I want to type a number on the window, or MessageBox and it allways gives me an error..
This is how I did it..

Code: Select all

surface.WriteText(x1,y1,(char *)i,1) // change 1 with len when it doesn't error..
I want to recreate some of my programs in VB in eC, and I need to put some boxes with continuing numbers.. In VB I created Label[15] and depending on page quickly changed the numbers..

So my questions are, is there a way I can create Label lblName [15], and how can I make "i" (integer) write it self on surface, or function return code on MessageBox..

PS: If I pass someWindowClass.text, to pointer I got empty string..Is that normal, or do I have many errors in my codes for now?
No army is to big, if the heart is beating strong.
There's no bad luck, it's just easier to blame someone else.
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Printing integers and control array

Post by jerome »

There are no stupid questions, only stupid answers.

Don't be shy to ask, that's how we fill up these forums with easy to find answers!

Turning numbers into a string of digits characters is a process that is often greatly simplified by programming languages. C belongs to the category of those in which it is not.

Because C supports direct memory access, and because character strings are typically stored into pointers used to reference an address, casting a number to a char * in fact makes up a pointer pointing at the memory location represented by that number. That will be an invalid address unless your number is indeed a valid address in memory your program allocated. So you don't want to be doing that.

The typical way to turn a number into a string in C is using the standard C library sprintf function.
There are a few additional ways to do it in eC. You can take a look at this post on the eC programming Google Groups. Here's the excerpt:
Hi Sam,

Code: Select all

// The usual sprintf method from the standard C library works: 
int number = 1234; 
char string[100]; 
sprintf(number, "%d", 1234); 
 
// Alternatively, eC proposes some new ways: 
PrintBuf(string, sizeof(string), number); // This outputs to the string buffer, just like the printf call above 
 
// PrintString allocates a new string ( which must be deleted ) 
char * string; 
string = PrintString(number); 
// Use the string 
delete string; 
 
// If putting to a file, the File class also has Print methods: 
File f = FileOpen("test.txt", write); 
f.Print(number); 
delete f;
The PrintLn equivalent for Print also exists, printing a new line at the
end. A variable number of additional parameters to be printed (which can be
of various different types) can also be added.
See the latest release notes from 0.43 for a short description of the
Print/PrintLn functions.
It seems the Surface object is lacking a Print like function that can conveniently accept any type of parameters. (New Mantis ticket here) DialogBoxes and other Window objects also seem to be lacking this type of functionality. For now this code should do it:

Code: Select all

char s[100];
PrintBuf(s, sizeof(s), i);
surface.WriteText(x1, y1, s, strlen(s));
jerome
Site Admin
Posts: 608
Joined: Sat Jan 16, 2010 11:16 pm

Re: Printing integers and control array

Post by jerome »

About using arrays of controls: it is possible in eC as well.
You can use either a C array (Label labels[15]) or an eC dynamic array (Array<Label> labels { })

The former however does not allow for instantiation within the declaration, so you will need to instantiate all of them in your constructor:

Code: Select all

class MyForm : Window
{
   Label labels[15];
   MyForm()
   {
      int i;
      for(i = 0; i < 15; i++)
      {
         char s[100];
         PrintBuf(s, sizeof(s), "Label #", i);
         labels[i] = { this, position = { 10, i * 20 + 10 }, text = s };
      }
   }
}
To use the dynamic arrays you can either declare all in the declaration:

Code: Select all

Array<Label> labels { [ 
   { this, position = { 10, 10 }, text = "Label 1" },
   { this, position = { 10, 30 }, text = "Label 2" },
   { this, position = { 10, 30 }, text = "Label 3" }
] };
Or in the constructor use labels.Add({ this, position = { 10, i * 20 + 10 }, text = s });
D.Bane
Posts: 78
Joined: Thu Jul 29, 2010 2:51 pm

Re: Printing integers and control array

Post by D.Bane »

Thanks, that helped. :) (I prefer here C version..for what I want now).

Just to add if someone also want's the label to be different color change opacity from 0 to 1 (also background) :)

Thanks again, for both answers :)
No army is to big, if the heart is beating strong.
There's no bad luck, it's just easier to blame someone else.
Post Reply