I am an absolute novice in Visual C++ and hence I have to ask you, how would I create a managed class module (new class) with one or more functions inside of my managed C++ project (Visual Studio 2008)? How would I call the method of the class for example if a button was pressed. I was unable to understand the very complicated tutorials on it and most of the tutorials referring to unmanaged code or older versions of visual studio. My own attempt totally failed and produced only errors, since I found no right options on how I would add a new class file to my Visual C++ managed project. If I choose a new "CLR Component Class" I get a warning message telling me about components filling the right pane of my screen. If I choose a new "CLR Windows Form Class" it happens just nothing, no file with the extension ".class" would be added or I do not know the file which was newly added. I just need a very basic class file with one public function in it which I would be able to call from any location of my project.
I have a very big main() cpp-file already (main.cpp) with lots of functions in it. There are about thousand functions or more, so it becomes difficult to search or scroll. Now I would like to put some of these 1000 functions in a second cpp-file within my current project (in Visual Basic 6 it was simply called a 'new module' in CSharp it is called a 'new class file'). The problem is, that I can't call this functions in Visual C++, once I have moved them out of my main.cpp to module1.cpp for example.
That's what I don't want:
I know there is a way to make just a simple class and then create a new object of this class to use its methods. That's what I want. The examples in Google on this did not work for me, because they were for earlier versions of visual studio and not compatible with my version. From this examples I know what I want, but I haven't got the knowledge to implement it in Visual Studio 2008.
I'm going to try again to post step-by-step instructions on how to add a class file to a VS2008 WinForm project. I again did not realize until completing the list that numbered items don't always work cleanly here. I was able to fix all but one of the numbers, so this should be good to go - David W
The editor now displays the source of the DemoNewClass.h header file. This file includes an automatic declaration of the namespace from the main application, DemoWinFormApp, and also contains the two default constructors in the "public:" region, which we will not touch. We will add a new public static method to DemoNewClass immediately after the second constructor definition.
We will add a static method - one that does not require instantiation of the host class - that puts asterisks around a String. The method will be called "DecorateString"
Immediately following the second constructor, add the declaration as illustrated here and highlighted in blue:
With the declaration in place, we must now add the implementation. In Solution Explorer, double-click on "DemoNewClass.cpp" to edit the file:
The editor window opens, and the file has only two lines - two include directives that instruct the compiler to bring in the referenced files as part of the current source. The declaration provided in DemoNewClass.h is provided by default.
Add the implementation of the DecorateString method to the .cpp file as shown here:
This completes the definition of the new class, the DecorateString declaration, and the provision of the implementation for the DecorateString method. All that remains now is to allow the WinForms app to reference the class and method from the main form.
From the Solution Explorer, double-click the "Form1.h" file in the "Header Files" list of the DemoWinFormApp project, which brings up Form1 in the editor. (The button was already added, and the steps to add the button are left to the reader):
To demonstrate the new class method, we'll have the button click display a string modified by our DecorateString method via the MessageBox class. First, we must make the form aware of our new class by supplying an include directive to the new class' header at the top of the Form1.h header file. Right-click on the form in the designer, and click "View Code.." to bring up the source editor
To make the class and its methods available to the form, we must supply an include directive for the class' header, DemoNewClass.h, near the top of the Form1.cpp implementation source file:
From Solution Explorer, double-click on "Form1.h" to open the Form Designer with Form1. Alternatively, if the file is still open in the editor, you could open the Designer by clicking the editor tab labled "Form1.h [Design]"
We will call our new class method from the event handler tied to the "button1" button on the form. Visual Studio provides a default handler by double-clicking "button1" in the designer:
20. With a successful build, all that remains is to test the application. Press F5 to run the application and display the test form:
The passed parameter, "foo", was successfully passed to the new class' DecorateString method, and returned to the MessageBox method for display. The new class method declaration and form reference are now complete.
Good luck.