-
sprintf in C#?
hi,
Actually in VC++, for example,when I was loading mutiple bitmaps whose filenames are bitmap0,bitamp1;bitamp2,respectively:
char filename[20];
FILE * file;
for(int i = 0; i< 3;i++)
sprintf(filename, "bitmap%d",i);
//////load bitmap function
Load_Bitamp(*file,filename);
so my question is that, is there any function like sprintf()
like my previous question, if I want to create some number of labels, so I use such way :
for(int i = 0; i< number;i++)
{
sprintf(filename, "button%d",i);
///load and create buttons using filename
}
thanx! :)
-
You can use the static Format method of class String:
string s = String.Format( "Bitmap{0}.bmp", i );
{0} is the placeholder for the first variable. No type formatting is required for easy output, as each object features a ToString method. Next placeholder would be {1} and so on.
-
thank you so much,
if I want to create 10 labels,
string label_str;
for(int i = 1; i<11;i++)
{
label_str = String.Format( "label{0}", i );
System.Windows.Forms.Label label_str;
label_str = new System.Windows.Forms.Label();
...................
}
but label_str is a string type ,can't be convert to System.Windows.Forms.Label.
so is that possible to change mylabel name ?
System.Windows.Forms.Label mylabel;
mylabel =????("label{0}",i);
-
This is the wrong way to do it. You need an array of labels. You can dynamically create that array and then dynamically create each label in that array like you did. You cannot create variable names this way, variable names have to be fix when you compile the program.