Console App to Windows Forms

Hey, I have a fully functioning console app, I would like to turn it into an app with a UI. Using VC++ I have created a simple UI and done a bit of research to see that rather than your standard cin you can just create a textbox and use

String ^ input = textBox1->Text;

My question is how do I make the variable input global, as all of the buttons are private, and second of all how do I execute code from my main function that I already wrote in console? do I just make a call to the main function from the button click or do I need to dump the code from my main into the button. If that's the case, and seeing how a string is made in Windows Forms, will I need to change the syntax of my existing code?
Up, any help would be greatly appreciated!
Are you looking for like an edit control? It seems like you might want to look at theForger's WINAPI lessons. http://www.winprog.org/tutorial/start.html

As for how to make an edit control: http://msdn.microsoft.com/en-us/library/windows/desktop/bb775458(v=vs.85).aspx
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
Last edited on
Topic archived. No new replies allowed.