Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does printf not work for a label Caption?

Status
Not open for further replies.

Johncccc

Programmer
Nov 28, 2003
3
FR
Sorry if this has been asked before but :(

The String.printf() function does not seem to work for Label->Caption.

I know there are ways around this (assign to new String first etc.) but why does it not work?

Thanks for any help.

 
because printf is DOS style function, for console applications only. If you sant to set the text of a label use SetWindowText or the property Caption

Ion Filipski
1c.bmp
 
woops soory i meant sprintf.

sprintf is an AnsiString (String) function. It will work when u declare a String ie

String str;
str.sprintf("test %d", 10);

It does not work (for me anyway) when u try to use it like this:

Label1->Caption.sprintf("test %d", 10);

Why is this? surely Caption is a String.


 
because you print in a variable returned by the Caption. To change caption you should use
...->Caption = ....

I don't know exactly what is the syntax for BorlandC++, maybe ->Caption(something) or SetCaption(something) or ->Caption.set(something...

Ion Filipski
1c.bmp
 
The syntax is correct for Builder 6. It compiles and runs. It just does not produce the string correctly. ie the string is empty. (The code completion feature of Builder even suggest that it is ok to use sprintf on a Labels Caption property)
 
[LOL]
Let's see what are you doing:
type:
Label1->
you get code completion for a label window

type
Caption.
you get completion for a borland string

.sprintf("test %d", 10);
you print in that string, but not in the caption.

So you change a reference to a copy of a variable returned by caption.

Ion Filipski
1c.bmp
 
To make it work try this:
char Buffer[100]; // Enough to store the new caption

sprintf(Buffer,"This: %u and that: %u",Some_Value, Another_Value); // Print the value/desired string in the buffer

Form1->Caption = Buffer; // Set the caption where it's needed

You could use new & delete for the buffer but that's up to you.

Totte
 
I guess the last post remarks the way to do it around. Now I understand, too, why sometimes I had the problem of a string not really returned changed.

So I now always do the following, to be sure:

AnyString = AnyString.anystring_function;
NewString = AnyString;
And NewString is ok.

I had it like this before:

AnyString.anystring_function; //the new form of AnyString is returned by the function, but does not modify the string itself.

NewString = AnyString;
and NewString was the UNMODIFIED AnyString.

Ok, what works, too is

NewString = AnyString.anystring_function;
But, still, I need a new variable.

So that is, I think, now clear with the "reference to a COPY" explanation of You, Ion.

Thanx!
Oh, btw., the SetLength() function works like this:
AnyString.SetLength(2);
and AnyString.Length() will be = 2;

Michael
is that explainable, too?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top