473,396 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C++/CLI VS2005, WFC andinstantiate, and use a class without messing up the "designer"

40
Intro:
I have recently started programming in C++/CLI. I have done a lot of research on the net trying to locate an answer to this problem, but there seems to be a lack of Tutorials and books on C++/CLI.
I have read 4 of the published books as best I could. The one book that has a great deal of info for my interest was Pro Visual C++/CLI and the .NET 2.0 platform by Fraser.

Problem:
Using VS2005 C++ and writing C++/CLI code, and using the "Designer", I created a basic Windows form, populated it with a menustrip, and an Open File menu (short snippet of C++/CLI from "Form1.h":

Expand|Select|Wrap|Line Numbers
  1. public: System::Void openToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e)
  2.              {
  3.                 // System::Windows::Forms::ToolStripMenuItem^  openToolStripMenuItem;
  4.                 static int ii=1;
  5.                 openFileDialog1->ShowDialog();  // *** has memory leak must be disposed.
  6.                 String^ filePath = openFileDialog1->FileName;
  7.                 label1->Text = filePath;
  8.                 label2->Text = "Counting";
  9.                 Dyn_XML_Obj.File_List->Add(filePath); // put path into File_List (keeps track of all open XML docs.)
  10.  
This all works as expected and places the filepath correctly as it loads properly in XmlDocument.

However, inorder to get these working, I had to place the class definitions at the top of the Form like this:

Expand|Select|Wrap|Line Numbers
  1. #pragma once
  2. #include "stdafx.h"
  3. #include <stdio.h>
  4. #include <stdlib.h> // allows use of abort()
  5. #include <vector>    // include the STL vector (Standard Template Library for vector which is a type of list.
  6. #include <iostream> // To use iostream commands such as cout, cin
  7. #include <process.h> // so I can clear the screen.
  8. #using <System.xml.dll> // so we can access XmlDocument
  9. using namespace System::Xml;
  10. using namespace std; // so we can shorten from std::vector to vector 
  11. using namespace System::IO; // se we can use Directory methods
  12. using namespace System;
  13. using namespace System::ComponentModel;
  14. using namespace System::Collections;
  15. using namespace System::Windows::Forms;
  16. using namespace System::Data;
  17. using namespace System::Drawing;
  18.  
  19. namespace WFC_3a {
  20.     int ii; // Put your variables here
  21. public ref class Dyn_XML_Class
  22. {
  23. public:
  24.  
  25.     ArrayList^ File_List; // Will hold the filenames of XML files.
  26.     ArrayList^ XML_List; // Will hold the XmlDocument objects for each VALID item in File_List.
  27.  
  28.     Dyn_XML_Class() // CONSTRUCTOR
  29.     {
  30.     File_List = gcnew ArrayList(16); // Initialize File_List with 16 empty items. 
  31.     XML_List = gcnew ArrayList(16); // Initialize XML_List with 16 empty items. 
  32.     Console::WriteLine("File_List Constructor Completed.");
  33.     }
  34.     void ADD()
  35.     {Console::WriteLine("You have called ADD.");}
  36.  
  37. };
  38. public ref class XML_Class
  39. {
  40. public:
  41.     XmlDocument^ XML_Doc;
  42.     static int Doc_Total;
  43.     int My_Num;
  44.     XML_Class()
  45.     {
  46.     XML_Doc = gcnew XmlDocument();
  47.     My_Num=Doc_Total;
  48.     Doc_Total++;
  49.     }
  50.     void show()
  51.     {
  52.         // Console::WriteLine("hello, My_Num={0},{1}",My_Num,Doc_Total);
  53.     }
  54. };
  55.  
  56.  
  57.  
Problems:
1. Once I placed the classes there I "Broke" the designer... and it would no longer work to let me see the visual layout of my form.
2. Attempting to Move the class definitions to WFC_3a.cpp resulted in 82 errors that everything in those classes was missing and syntax errors.
3. Attempting to move the classes to the same file Form1.h, but at the bottom also creates 82 errors:
Form1.h(185) : error C2227: left of '->Add' must point to class/struct/union/generic type
Form1.h(192) : error C2059: syntax error : '>'
and many more "Missing ;" and Syntax errors.

So, questions are:

1. Where should I move these classes and what code should I use so that Form1 will recognize the classes... without damaging the "designer" functions of VS2005.

Thank you.
Mar 6 '08 #1
6 2571
Banfa
9,065 Expert Mod 8TB
I have been using C++/CLI .NET 2 in VS 2005 and have had similar problems. I think the basic problem is that VS 2005 tries to put all the form1 code in Form1.h

I have now move much of it out into cpp files including the form initialisation function (the really long one that sets all the properties of the items on your form).

Once Form1.h contains mainly declarations and few definitions things seemed to get better for me.

The other thing is to undo you work step by step (or step backto a working copy and slowly reapply your changes). You have most likely put something in the wrong place (inside a section reserved for editing by VS 2005) or something like that. I spent a couple of hours doing that to sort my project out a few weeks ago.
Mar 7 '08 #2
MACKTEK
40
Thanks Banfa.
I am sure the Designer has some mechanism for letting the programmer add classes into the program without causing errors.

A guess to simplify my question:

How do I add classes into Form1.h so that I can use the designer and ALSO use those classes/instances of classes within the delegates and events of the Form?

Any example of a class that allows me to change button1->Text would be helpful. ( I already made the class, but it only works when the class is declared at the top of the form!)

These simple things, I cannot find on the net.
Should I put everything in a different file and include it? should I make it under the same namespace?
Mar 7 '08 #3
Banfa
9,065 Expert Mod 8TB
The correct way to add a class is to select Project -> Add Class ... from here you can add a new form, or component or other CLR class or you can add a C++ class. This will create a header(h) and code(cpp) file for the new class, although optionally you can create an inline class and not have the cpp file.

If you create the class with a cpp file then you can check it compiles before you include you header into form1.h (towards the top).

I have to admit that I do create classes by hand at times but generally only simple ones or ones that are similar to an existing class so a create with a copy/paste. With very few exceptions I stick to 1 class per header file. For me the exception is EventArg classes which are often extremely simple and really just data holders rather than fully fledged classes with there own methods.

Any example of a class that allows me to change button1->Text would be helpful. ( I already made the class, but it only works when the class is declared at the top of the form!)
Strictly speaking since button1 is create by the designer as private you should be trying to alter it via an outside class. If you are you are probably breaking the rule of keeping implementation and user interface separate. It can seem like a lot of hassle to do this but in the long run you end up with a more structure more easily maintained program. And it isn't to hard to do using events and delegates, have you class raise an event and your form create a delegate on that event then put your code to handle the event in that delegate which is part of the Form1 class and you have access to the provate data.

If as in my project the class happens to be a component that runs in it's own thread then you will not be able to access the buttons (or other gui widgets) in that delegate because you will be executing in the wrong thread. You have to have a second delegate (again part of the Form1 class) and Invoke a call to it with the data, this causes the second delegate to be called in the context of the Form1 class handling thread.

These simple things, I cannot find on the net.
Should I put everything in a different file and include it? should I make it under the same namespace?
I have said elsewhere on this forum that it is often the simple things that are hardest to find the answer to. If you have read this post you should be able to answer the 1st question (basically yes).

The namespace question is a big moveable feast, personally I would recommend having at least 2 namespaces, 1 for the user interface and 1 for the implementation. And make sure you have a well defined the interface between them.

In my project I have 4 namespaces
  1. User interface - handles all windows and dialogs
  2. Communication - handles the various differing communication methods with which the application and receive and send data
  3. Message Protocol - definition of the message protocol carried across the communication mediums
  4. Code Download - implementation of a state-event machine to control a specific and logically separate piece of the functionality that is self-contained with a well defined interface.
Mar 7 '08 #4
MACKTEK
40
Fantastic help. Thanks.
I will try to implement as much as I can.
Mar 7 '08 #5
MACKTEK
40
Using the Project Menu, and choosing Add Class, I made a class called Dyn_XML_Class. This is contained in Dyn_XML_Class.h

In Form1 all I had to do was:
#pragma once // exists as top of Form1.h
#include "Dyn_XML_Class.h" // add this line.

I then instantiated the class inside an event handler, add some extra lines of code and it worked. (At least so far.)

One thing I noticed is that the designer creates a completely new include for each class that I add.
I guess, I will experiment with whether or not I can combine more than 1 class into the same include without messing up the Designer.

Thanks again.
Mar 8 '08 #6
Banfa
9,065 Expert Mod 8TB
One thing I noticed is that the designer creates a completely new include for each class that I add.
I guess, I will experiment with whether or not I can combine more than 1 class into the same include without messing up the Designer.
You could do but this is normally considered desirable, it is normally considered better practice to have lots of small files than a few very large ones.

Particularly in a large project with multiple programmers this leads to less file conflicts.
Mar 8 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
0
by: Bad_Kid | last post by:
I would try to do something like that (nothing complicate). Just -> where to start? any good link? ;-))
0
by: Chris Snyder | last post by:
OK. Here's the short form: I have a non-abstract base page class that other forms inherit from. I keep getting the infamous "file could not be loaded into the designer, file name <x> could not...
1
by: Robin Tucker | last post by:
Hi, I have a problem with my code (somewhere in the InitialiseComponent) preventing any of my controls being visible in form design mode, although everything is ok at runtime. No errors are...
3
by: sklett | last post by:
This BUG that is so ridiculous I can't believe they shipped 05 is making my life hell. I have tried the various solutions found on the web and none of them have worked thus far. What's even more...
3
by: Tin Gherdanarra | last post by:
A colleague gave me a project where there was a /designer/ window for database connectivity (drag&drop from the toolbox), but I can't find out how to get a /designer/ window in my own project....
1
by: TheSteph | last post by:
Hi ! I would like to create a UserControl that act as a « Collapsible Panel ». So I have a UserControl with two panels : a "Header panel" at the top, and a "Container Area Panel"...
3
by: segecko | last post by:
Hi I have a created a custom usercontrol which inherites an Excel like usercontrol. In this usercontrol I have a custom property called SpreadTemplate, which is an enum with (at the moment) two...
1
by: =?Utf-8?B?QnJldHRWUA==?= | last post by:
I have a child-class that inherits from a base class that implements an Interface. The child class overrides a SUB from the base class that implements a sub from the interface. (The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.