String - Int- String Conversion

I am using Windows Forms on Visual C++ 2010. I am creating a very simple program. I will take in the input from TextBox1, make some calculations and print out in Label3. For this, I need to convert the string in TextBox1 into an integer and, after the calculations, convert the integer back into a string. This part of the code is in bold.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#pragma once
#include <iostream>
#include <sstream>



namespace TemperatureConverter {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}

	protected: 
	private: System::Windows::Forms::Button^  button1;
	private: System::Windows::Forms::ListBox^  listBox1;
	private: System::Windows::Forms::ListBox^  listBox2;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::TextBox^  textBox1;

	private: 
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::Label^  label1;
	public: 


	private: 




	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->listBox1 = (gcnew System::Windows::Forms::ListBox());
			this->listBox2 = (gcnew System::Windows::Forms::ListBox());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->BackColor = System::Drawing::Color::Red;
			this->button1->BackgroundImageLayout = System::Windows::Forms::ImageLayout::Center;
			this->button1->Cursor = System::Windows::Forms::Cursors::Hand;
			this->button1->FlatStyle = System::Windows::Forms::FlatStyle::System;
			this->button1->Font = (gcnew System::Drawing::Font(L"Impact", 24, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->button1->Location = System::Drawing::Point(209, 434);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(136, 47);
			this->button1->TabIndex = 1;
			this->button1->Text = L"Convert";
			this->button1->UseVisualStyleBackColor = false;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// listBox1
			// 
			this->listBox1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->listBox1->FormattingEnabled = true;
			String^ str = "Select Unit";
			String^ str2 = "Celsius";
			String^ str3 = "Farhenheit";
			String^ str4 = "Kelvin";
			this->listBox1->SelectionMode = System::Windows::Forms::SelectionMode::MultiExtended;
			this->listBox1->Items->AddRange(gcnew cli::array<System::String^ >(4) {str, str2, str3, str4});
			this->listBox1->ItemHeight = 24;
			this->listBox1->Location = System::Drawing::Point(11, 129);
			this->listBox1->Name = L"listBox1";
			this->listBox1->Size = System::Drawing::Size(170, 28);
			this->listBox1->TabIndex = 2;
			// 
			// listBox2
			// 
			this->listBox2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->listBox2->FormattingEnabled = true;
			this->listBox2->ItemHeight = 24;
			this->listBox2->SelectionMode = System::Windows::Forms::SelectionMode::MultiExtended;
			this->listBox2->Items->AddRange(gcnew cli::array<System::String^ >(4) {str, str2, str3, str4});
			this->listBox2->Location = System::Drawing::Point(377, 129);
			this->listBox2->Name = L"listBox2";
			this->listBox2->Size = System::Drawing::Size(170, 28);
			this->listBox2->TabIndex = 3;
			this->listBox2->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::listBox2_SelectedIndexChanged);
			// 
			// 
			// textBox1
			// 
			this->textBox1->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
			this->textBox1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->textBox1->Location = System::Drawing::Point(11, 202);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(169, 29);
			this->textBox1->TabIndex = 5;
			// 
			// label3
			// 
			this->label3->BackColor = System::Drawing::SystemColors::HighlightText;
			this->label3->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
			this->label3->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label3->Location = System::Drawing::Point(375, 202);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(170, 26);
			this->label3->TabIndex = 6;
			this->label3->Click += gcnew System::EventHandler(this, &Form1::label3_Click);
			// 
			// label4
			// 
			this->label4->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15.75F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label4->ForeColor = System::Drawing::Color::Red;
			this->label4->Location = System::Drawing::Point(155, 320);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(262, 37);
			this->label4->TabIndex = 7;
			// 
			// label1
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->BackColor = System::Drawing::SystemColors::InactiveCaption;
			this->ClientSize = System::Drawing::Size(561, 514);
			this->Controls->Add(this->label4);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->listBox2);
			this->Controls->Add(this->listBox1);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->label1);
			this->Cursor = System::Windows::Forms::Cursors::Default;
			this->Name = L"Form1";
			this->Text = L"Temperature Converter";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

public: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			std::string text = textBox1->Text;
			int Result;
			std::stringstream convert(text);
			if ( !(convert >> Result) )
				Result = 0;
			int x = Result + 10;
			String^ valueString = Convert::ToString(x);
			label3->Text = valueString;
		 }

};
}


When I run the code, I get this error:

error C2440: 'initializing' : cannot convert from 'System::String ^' to 'std::basic_string<_Elem,_Traits,_Ax>'

Can somebody please help me? I am new to this, so please try to keep your explanations as simple as possible.

Thanks
Tejas
Can somebody please help me ?
It's the wrong forum for this. This is a C++ forum, however the language you seem to be using is called C++/CLI.
closed account (1vRz3TCk)
tejas1995,
If you are going to use .net [via C++/CLI] then I suggest you learn it and not try mixing managed an unmanaged code, life will be much easier.
1
2
3
4
5
6
7
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
             {
                 int result = 0;
                 Int32::TryParse(textBox1->Text, result);
                 result +=10;
                 label3->Text = result.ToString();
             }
Topic archived. No new replies allowed.