Hello Everyone.
I am creating this Windows Forms Application using Visual Studio.
Basically, in order to do what I want to do, I need to use an integer, and that integer needs to be converted into a string in order to change the label text, when pushing the button. However, using static_cast, like I did in the code below gives me " error C2061: syntax error : identifier 'string' ". :( I have included <string> on top of the file.
What shall I do in order to fix this problem and make this run? Any suggestions?

Thanks in advance. :)

#pragma once
#include <string>


namespace testingforms {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
///////////////////////////////////////
// Everything that is created by default
// when creating a Windows Forms Application
// I just deleted it, I don't think it is that much of a concern.
//////////////////////////////////////


	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
			 {
			
				 int a;
				 a=5;
				 label2->Text = static_cast<string>(a); //right here
			 }


}

Dani AI

Generated

You’re hitting two separate issues here, :

  • string is the C++ standard type (std::string), not the .NET type the WinForms Label expects. label2->Text is a managed System::String^, so a native std::string (or an int) won’t assign directly. Also, static_cast can’t manufacture a std::string from an int like that.
  • Even if you produced a std::string, you’d still need to convert it to System::String^ for WinForms.

Since you’re already writing C++/CLI, the simplest fix is to stay in the managed world. Either call ToString() on the integer, or format the value into a managed string:

int a = 5;
label2->Text = a.ToString();              // managed Int32 -> managed String^
label2->Text = String::Format("{0}", a);  // composite formatting

Both produce a System::String^ and work cleanly with Windows Forms. See the docs for Int32.ToString and String.Format for details and formatting options. (learn.microsoft.com)

’s ostringstream tip is fine for native code, but as you found, it doesn’t solve the managed Label->Text assignment in a Forms app. If you really must start with a std::string, marshal it to a managed String^ like this:

#include <msclr/marshal_cppstd.h>
using msclr::interop::marshal_as;

std::string native = "5";
label2->Text = marshal_as<String^>(native);

That uses the msclr marshaling library, which is designed for moving between std::string and System::String^. (learn.microsoft.com)

Finally, for clarity: WinForms Label exposes a Text property of type System::String, so stick with managed strings in UI code whenever possible. (learn.microsoft.com)

Recommended Answers

All 6 Replies

Not sure if in Visual_Basic works like this, but in Borland C++ Builder worked just fine

void __fastcall TForm1::Button3Click(TObject *Sender)
{
int k;
k=5   ;
Label1->Caption = k;
}

Looks like ye' need to perform a good ol' int-to-string conversion. Today we'll be creating an 'ostringstream' object derived from the <sstream> library in order to facilitate our int-to-string needs:

#include<sstream>
#include<string>
#include<iostream>

using namespace std;

int main()
{
     ostringstream int_to_str;
     string temp;
     int a = 0;

     cout << "Enter a number fool: ";
     cin >> a;

     int_to_str << a;
     temp = int_to_str.str();

     cout << "String " << temp << " == int " << a;

     return 0;
}

vidmaa, this doesn't work with Visual.


Clinton, it does not work in my situation. I am sure it is going to work if I do it as console application. However it does not work with the Forms :(

I looked around a bit, what I found out is that we actually need to convert an int to System::String. Who can help me with that?

Mixing C++/CLI and standard C++ can be troublesome. Try sticking to one or the other unless you have good reason not to:

label2->Text = Convert::ToString ( a );

Narue, thanks a lot! It worked finally :)

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.