First thing, you do know that
String ^ input = textBox1->Text;
is not C++ ; it's C++/CLI, which is language derived from C++, with it's own rules. And it's using the .NET Windows Forms library (WinForms), rather than the old Windows API.
If all you want to do is display some text in a TextBox (or Edit control) then it will prob be easier overall to share code between console and GUI apps if you use WinAPI -- once you sort out the basic framework (see theForger's lessons which slambert pointed you at.)
If you do stick with WinForms then you're going to have to learn how to convert C++ (and C) strings into .NET Strings, and the reverse, etc. Plus the bits of C++/CLI syntax that you need to work with the .NET controls.
Regarding your old code, what you should do is factor your console app's code so that the calculation is done by a helper function which does no UI work. Taking a very simple example, you alter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
using namespace std;
int main()
{
const double pi = 3.1415927;
double radius = 0.0;
cout << "radius? ";
cin >> radius;
double area = pi * radius * radius;
cout << endl
<< "area = " << area << endl;
return 0;
}
|
to
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
|
#include <iostream>
using namespace std;
double calcArea(double radius)
{
const double pi = 3.1415927;
double area = pi * radius * radius;
return area;
}
int main()
{
double radius = 0.0;
cout << "radius? ";
cin >> radius;
cout << endl;
double area = calcArea(radius);
cout << "area = " << area << endl
<< endl;
return 0;
}
|
As calcArea does not use cout or cin, then it can be used as-is in a program which uses some other UI library.
For example, in the WinAPI case you could something like this
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
|
// handle WM_COMMAND message
case WM_COMMAND:
{
// get command ID...
int id = LOWORD(wParam);
// if it's the ID of the "Calc" button
if(ID_BUTTON_CALC == id)
{
// get the text from the radius edit box
char buffer[64] = "";
GetWindowTextA(s_hWndEditRadius, buffer, sizeof(buffer)/sizeof(char));
// convert it to a double
double radius = 0.0;
std::istringstream iss(buffer);
iss >> radius;
// do the calculation using same function as console app
double area = calcArea(radius);
// convert back to a string
std::ostringstream oss;
oss << area;
std::string text = oss.str();
// set text in area edit box
SetWindowTextA(s_hWndEditArea, text.c_str());
// return 0 as we handled the message
return 0;
}
}
break;
|
Andy