Question

i'm using Visual C++ to create a simple compiler and a graphical interface for it(Windows Forms) for my school project.

I've used a Visual C++ Win32 Console Application project to develop the main classes as: Read, Compile, Assembly, etc. It works well until now as a Console App but the problem is that i don't know how to "integrate" my classes with the Windows Forms.

For example i have my reading class:

class Reading {
private:
   string TheFile;
   long TheFileLength;
   long TheFileMark;
   long CurrentLine;

public:
Reading() {
    TheFileMark=0;
    CurrentLine=1;
}
void OpenTheFile(basic_string<TCHAR> FileName);
enum WordTypes{EndOfLine, Identifier, Number, String, Symbol, None};
};

When i try to open the file in the OpenTheFile method:

void Reading::OpenTheFile(basic_string<TCHAR> FileName) {
   ifstream File(FileName.c_str(), ios::in);
   if (File.is_open() == false) {
      cout << "Error ! Could not open file: "<<FileName <<endl;
      exit(1);
   }
}

I actually want to replace the "cout" so that it will display the message in the Windows Forms control, for example in a TextBox.

I've included my "Reading.h" in the .cpp file of the project but i can't get to work a method for the above requirement...

Is there a way to do that ?

Was it helpful?

Solution

You can't make that code work using a winform application. While using a winform application you are compiling in a bit different language, C++/CLI. The syntax is quite the same of C++, but it can handle other features.
In a console app, every lines of code are executed one after another. This means, if you have 300 lines of code, that those will be executed but the compiler finds some kind of error.
In a Winform it is not necessarily true. If you want some code to be compiled, you have to fire some events.
Let's have this example. You have your app with a button that, if pressed, displays a popup with "Hello World". You will code just like that:

System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
   MessageBox::Show("Hello World");
}

This code won't be "seen" by the compiler until button1 won't be clicked.
So, if you want your class to be used in your app, you need to:

  • Convert in C++/CLI ( + find some tutorial about creation of winform apps :D )
  • Figure out when and where you want your function to be called and code, for instance, a click event of a button( but you can choose between a lot of choices ).

Summarize this up, you will have something like that:

System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
  /*Your converted Reading function */
  MessageBox::Show("Error ! Could not open file: "+ FileName);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top