CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Aug 2007
    Posts
    448

    [RESOLVED] How to check on menu item in a dialog?

    Hi guys,

    I would like you to help me. I have figure out that I will get the errors of 'Form2' : undeclared identifier as if I included the "#include Form1.h" on the top of the Form2 class while I have the "#include Form2.h" on the top of the Form1 header.

    This is how I found the problem to get it solve, if I remove the "#include Form2.h" on the top of the Form1 header, the problem would solve but I cannot open the form2 dialog.

    What I am trying to do is to open the form2 dialog and check on the Form1 menu items that if any of the menu items checked is set to true or false then do something.

    Please can you help me to get this solution as I find it very difficult to deals with?

    Thanks,
    Mark
    Last edited by mark103; April 9th, 2011 at 11:17 AM.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: How to check on menu item in a dialog?

    If you really have a line reading

    Code:
    #include Form2.h
    in your code the error message is no surprise. Of course that would need to be:

    Code:
    #include "Form2.h"
    But maybe this was a transcription error while writing the post.

    If it's not that, it most likely is the "circular inclusion" problem. It's quite common and has been discussed around here in a lot of threads. You can find them using a forum search for "circular inclusion" or "mutual inclusion".

    Aside from that, your post suspiciously looks as if you're using C++/CLI which is discussed in a different section. In particular, there are some specifics about the circular inclusion problem in C++/CLI (related to how the IDE sets up the projects) so the (fewer) threads about that in the C++/CLI section may be particularly helpful.

    Checking menu items on another form seems to be a secondary issue as of now and I suggest to postpone this until you got the above sorted out.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Aug 2007
    Posts
    448

    Re: How to check on menu item in a dialog?

    Hello Eri523,

    Sorry for the delaying in my response as I was on vacation. However, yes I have included the #include "Form2.h" in my form1 header and #include "Form1.h" in the form2 header. I can't find a way to get solution, as I took my research and I doesn't find any answers.


    If you would need to see my form1 and my form2 code, so here it is:


    Form1:

    Code:
    #pragma once
    
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    
    #include "Form2.h"
    
    
    namespace Test {
    
    	/// <summary>
    	/// Summary for Form1
    	///
    	/// WARNING: If you change the name of this class, you will need to change the
    	///          'Resource File Name' property for the managed resource compiler tool
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public ref class Form1 : public System::Windows::Forms::Form
    	{
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    			//
    			//TODO: Add the constructor code here
    			//
    		}
    
    	protected:
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		~Form1()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    	private: System::Windows::Forms::Button^  button1;
    	protected: 
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container ^components;
    
    #pragma region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			this->button1 = (gcnew System::Windows::Forms::Button());
    			this->SuspendLayout();
    			// 
    			// button1
    			// 
    			this->button1->Location = System::Drawing::Point(73, 84);
    			this->button1->Name = L"button1";
    			this->button1->Size = System::Drawing::Size(133, 62);
    			this->button1->TabIndex = 0;
    			this->button1->Text = L"button1";
    			this->button1->UseVisualStyleBackColor = true;
    			// 
    			// Form1
    			// 
    			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->ClientSize = System::Drawing::Size(292, 266);
    			this->Controls->Add(this->button1);
    			this->Name = L"Form1";
    			this->Text = L"Form1";
    			this->ResumeLayout(false);
    
    		}
    #pragma endregion
    	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    
                                 Form2^ form2 = gcnew Form2();
                                 form2->showdialog(this);
    			 }
    	};
    }

    Form2:

    Code:
    #pragma once
    
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    
    
    #include "Form1.h"
    
    
    namespace Test {
    
    	/// <summary>
    	/// Summary for Form2
    	///
    	/// WARNING: If you change the name of this class, you will need to change the
    	///          'Resource File Name' property for the managed resource compiler tool
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public ref class Form2 : public System::Windows::Forms::Form
    	{
    	public:
    		Form2(void)
    		{
    			InitializeComponent();
    			//
    			//TODO: Add the constructor code here
    			//
    		}
    
    	protected:
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		~Form2()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    	private: System::Windows::Forms::Button^  button1;
    	protected: 
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container ^components;
    
    #pragma region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			this->button1 = (gcnew System::Windows::Forms::Button());
    			this->SuspendLayout();
    			// 
    			// button1
    			// 
    			this->button1->Location = System::Drawing::Point(73, 84);
    			this->button1->Name = L"button1";
    			this->button1->Size = System::Drawing::Size(133, 62);
    			this->button1->TabIndex = 0;
    			this->button1->Text = L"button1";
    			this->button1->UseVisualStyleBackColor = true;
    			// 
    			// Form2
    			// 
    			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->ClientSize = System::Drawing::Size(292, 266);
    			this->Controls->Add(this->button1);
    			this->Name = L"Form2";
    			this->Text = L"Form2";
    			this->ResumeLayout(false);
    
    		}
    #pragma endregion
    	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                               Form1^ form1 = gcnew Form1();
                               if (form1->menuitems->checked == true)
                               {
                                 'do something
                                }
    			 }
    	};
    }

    Hope you can help me with this.

    Thanks,
    Mark

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: How to check on menu item in a dialog?

    You may want to try a .net forum. This is for Visual C++ and MFC without .net

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: How to check on menu item in a dialog?

    The button click handler in Form2 is the only place in that form where you reference Form1. So, as a minimum solution, move the button click handler's implementation into the Form2.cpp file. I suppose the IDE has created one for you, at least VC++ 2010 does that. If not, simply create it yourself. You then can move the #include "Form1.h" directive to the .cpp file as well.

    If you have any more questions about C++/CLI, whether about doing the above or otherwise, please post them in the C++/CLI section.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: How to check on menu item in a dialog?

    [ Moved thread ]
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    Join Date
    Aug 2007
    Posts
    448

    Re: How to check on menu item in a dialog?

    Quote Originally Posted by Eri523 View Post
    The button click handler in Form2 is the only place in that form where you reference Form1. So, as a minimum solution, move the button click handler's implementation into the Form2.cpp file. I suppose the IDE has created one for you, at least VC++ 2010 does that. If not, simply create it yourself. You then can move the #include "Form1.h" directive to the .cpp file as well.

    If you have any more questions about C++/CLI, whether about doing the above or otherwise, please post them in the C++/CLI section.
    That doesn't really much helping, because I have tried it but still get the same error so no difference. I would appreciate if you could post the code so hopefully my problem would solve there.

  8. #8
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: How to check on menu item in a dialog?

    Ok, following are some concrete modifications to your test project. I won't attach a complete zipped project, though, because you probably wouldn't be able to open a VC++ 2010 project anyway. I also won't post complete .h files because .h files used by the Windows Forms Designer shouldn't simply be replaced in a project.

    At any rate, here we go...

    As already said, there are no modifications to Form1.h and the associated form design required except for the following:
    • Of course you need to call form2->ShowDialog(this) from your button click handler because C++ (whether /CLI or not) is case sensitive.
    • From Form2 in your code from post #3 you were accessing a Form1::menuitems property that actually isn't there at all. To demonstrate a reference from Form2 back to Form1 I added a label named label1 to Form1. The label needs to be public so it can be accessed from outside Form1. This can be achieved by appropriately setting the label's Modifiers property in its properties window in the Forms Designer.

    Of course, as I already said, the line

    Code:
    #include "Form1.h"
    needs to be removed from Form2.h. In addtition, the definition of the button click handler in Form2 needs to be reduced to a declaration:

    Code:
      // Event handlers
    
      private:
        System::Void button1_Click(System::Object^  sender, System::EventArgs^  e);
    Instead, the button click handler needs to be defined in Form2.cpp:

    Code:
    // Form2.cpp
    
    #include "StdAfx.h"
    
    #include "Form2.h"
    
    #include "Form1.h"
    
    using namespace Test17;  // Of course the namespace name would be Test in your project
    
    System::Void Form2::button1_Click(System::Object^  sender, System::EventArgs^  e)
    {
      Form1 ^form1 = dynamic_cast<Form1 ^>(Owner);
      form1->label1->Text = "A message from Form2! :)";
    }
    Moving the definition of the button click handler out of the .h file may seem irrelevant at first sight, but as this handler is the only place where Form1 is referenced, this allows to move the #include "Form1.h" directive, which was the actual cause for the primary problem, out of the .h file as well.

    Creating another Form1 instance using gcnew in your Form2 button click handler dosn't make sense. You obviously want to access the Form1 instance from which Form2 was invoked, but gcnew would create a new, distinct Form1 instance which certainly is not the one you want. Instead you can reference the original Form1 via the Form2::Owner property (inherited from Form) which contains what has been passed as the parameter to ShowDialog(). However, it needs to be cast in order to access (non-inherited) Form1 members because it is of type Form ^.

    HTH

    If any problems remain, feel free to ask.
    Last edited by Eri523; April 18th, 2011 at 06:05 AM. Reason: Minor code fix
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  9. #9
    Join Date
    Aug 2007
    Posts
    448

    Re: How to check on menu item in a dialog?

    Eri523, you doesn't really making any sense to me at all you are losing me there. So let me make this clear:

    Do you want me to remove this on Form2 header?

    Code:
    #include "Form1.h"


    What do you want me to do with this?

    Code:
      // Event handlers
    
      private:
        System::Void button1_Click(System::Object^  sender, System::EventArgs^  e);

    And you want me to add this code on Form2.cpp?

    Code:
    // Form2.cpp
    
    #include "StdAfx.h"
    
    #include "Form2.h"
    
    #include "Form1.h"
    
    using namespace Test17;  // Of course the namespace name would be Test in your project
    
    System::Void Form2::button1_Click(System::Object^  sender, System::EventArgs^  e)
    {
      Form1 ^form1 = dynamic_cast<Form1 ^>(Owner);
      form1->label1->Text = "A message from Form2! :)";
    }

    If so, you will need to consider your post again. There are few errors which YOU DIDN'T CHECKING PROPERLY. Could you stop discuss me too much saying in your own way which you are bored me and you doesn't make it sense at all. It would be much easier for me to get this if you post an example fixed code, so my problem would solve there especially post an fixed project.

  10. #10
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: How to check on menu item in a dialog?

    Quote Originally Posted by mark103 View Post
    Do you want me to remove this on Form2 header?

    Code:
    #include "Form1.h"
    Yes.

    What do you want me to do with this?

    Code:
      // Event handlers
    
      private:
        System::Void button1_Click(System::Object^  sender, System::EventArgs^  e);
    This is the proposed replacement for the following part of Form2.h:

    Code:
    	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                               Form1^ form1 = gcnew Form1();
                               if (form1->menuitems->checked == true)
                               {
                                 'do something
                                }
    			 }
    And you want me to add this code on Form2.cpp?

    Code:
    // Form2.cpp
    
    #include "StdAfx.h"
    
    #include "Form2.h"
    
    #include "Form1.h"
    
    using namespace Test17;  // Of course the namespace name would be Test in your project
    
    System::Void Form2::button1_Click(System::Object^  sender, System::EventArgs^  e)
    {
      Form1 ^form1 = dynamic_cast<Form1 ^>(Owner);
      form1->label1->Text = "A message from Form2! :)";
    }
    Actually, this is the entire Form2.cpp in the test project I created to model your scenario. If your IDE has automatically created a Form2.cpp for you, I assume it already contains the first two #includes.

    I have attached my test project to this post for your reference. You probably won't be able to open it in your IDE because it's from VC++ 2010, but you can pick out any individual file you want. I claim the project compiles and runs perfectly fine here.

    If all I posted here still doesn't make sense to you, hopefully someone more knowledgeable than me can help you out. (Maybe that person can also check whether my test project actually works.)
    Attached Files Attached Files
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  11. #11
    Join Date
    Aug 2007
    Posts
    448

    Re: How to check on menu item in a dialog?

    Thanks Eri523, I can see the problem is being fixed. However, I have problem with this:

    Form1.cpp

    Code:
    // Form1.cpp
    
    #include "StdAfx.h"
    #include "Form1.h"
    #include "Form2.h"
    
    System::Void Form1::ListView1_DoubleClick(System::Object^  sender, System::EventArgs^  e)
    {             
    	Form2 ^form2 = dynamic_cast<Form2 ^>(Owner);
    	//Address of URL
            .....code list
    	 }
    Error: C2084: function 'void Test17::Form1::ListView1_DoubleClick(System::Object ^,System::EventArgs ^)' already has a body


    I don't really understand what it does stand for already has a body and what it suppose to mean?

    Any idea how to correct the error?
    Last edited by mark103; April 20th, 2011 at 04:36 PM.

  12. #12
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: How to check on menu item in a dialog?

    Quote Originally Posted by mark103 View Post
    Thanks Eri523, I can see the problem is being fixed.
    You're welcome.

    However, I have problem with this:

    [...]
    You probably still have something like this (in whole or in part generated by the IDE) in your Form1.h:

    Code:
    	private: System::Void ListView1_DoubleClick(System::Object^  sender, System::EventArgs^  e) {
                               // Something more may or may not go here
     	 }
    Everything marked in red is the function body the compiler is complaining about and needs to be removed. Naturally, the same function can't be implemented twice. Instead, put a semicolon after the closing brace of the function header to make it a prototype.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  13. #13
    Join Date
    Aug 2007
    Posts
    448

    Re: How to check on menu item in a dialog?

    Thank you once again Eri523, I can see it is being fixed. However can you help me with this?

    Code:
    System::Void Form1::ListView1_DoubleClick(System::Object^  sender, System::EventArgs^  e)
    {             
    	Form2 ^form2 = dynamic_cast<Form2 ^>(Owner);
    	//Address of URL
    	String ^URL = "http://www.mysite.com/myscript.php?user=" + form2->ComboBox1->Text + "&pass=" + form2->TextBox1->Text;
    	HttpWebRequest ^request = safe_cast<HttpWebRequest^>(WebRequest::Create(URL));
    	HttpWebResponse ^response = safe_cast<HttpWebResponse^>(request->GetResponse());
    	StreamReader ^reader = gcnew StreamReader(response->GetResponseStream());
    	String ^str = reader->ReadToEnd();
    }

    Error: error C2039: 'Create' : is not a member of 'System::Net::WebRequest'

    Error: error C3861: 'Create': identifier not found

    Both errors I have got, are jumping on HttpWebRequest ^request statement. I have checked the name of the property called "Create" is a member of WebRequest, so I don't know how to correct the error as I have got?

    And also for this:

    Error: fatal error C1903: unable to recover from previous error(s); stopping compilation


    The error are jumping on this line:
    Code:
    HttpWebResponse ^response = safe_cast<HttpWebResponse^>(request->GetResponse());
    I don't know what to do with it and I can't find the solutions.

    Any idea?

  14. #14
    Join Date
    Aug 2007
    Posts
    448

    Re: How to check on menu item in a dialog?

    Do you know how to resolve the solutions from my post on above, Eri523?

  15. #15
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: How to check on menu item in a dialog?

    Quote Originally Posted by mark103 View Post
    However can you help me with this?
    Well, sorry, no(t yet).

    When I initially read your post #13 yesterday I saw nothing obviously wrong with your code and began to study the MSDN docs on WebRequest and its relatives in order to find something peculiar about them. Without result. In particular, they all reside in System.dll (so no extra references need to be added to the project) and they have been around since .NET 1.0 (so there's no chance your .NET is simply too old). These are the two things I have become used to look for first when I encounter errors like yours and can't explain them. (There actually is a third item on my "check first" list but that doesn't apply to static members.)

    So I tried to model your scenario in my Test17 project (using copy & paste in order not to miss a potential subtle syntax error).

    This is the code:

    Code:
    // Form1.cpp
    
    #include "stdafx.h"
    
    #include "Form1.h"
    
    using namespace Test17;
    
    using namespace System::Net;
    
    System::Void Form1::button2_Click(System::Object^  sender, System::EventArgs^  e)
    {
    	String ^URL = "http://localhost";
    	HttpWebRequest ^request = safe_cast<HttpWebRequest^>(WebRequest::Create(URL));
    }
    Initially I intentionally left out the using namespace Systen::Net; though I expected this to give me different error messages and in fact it did. (Actually it were 7 errors for the single second line of the function body before the compiler finally gave up with a fatal error.)

    After adding that line the code compiled flawlessly. So sorry, I can't reproduce your problem here. Can you set up a small test project that models the problem and attach it to a post? (Remember to clean the project before zipping it for upload.)

    And also for this:

    Error: fatal error C1903: unable to recover from previous error(s); stopping compilation


    The error are jumping on this line:
    Code:
    HttpWebResponse ^response = safe_cast<HttpWebResponse^>(request->GetResponse());
    I wouldn't worry too much about error messages like this one. They basically mean that the compiler became really annoyed by all those errors and finally got fed up. I'm used to see those messages right after one or more of the "usual" error messages pertaining to a certain line but that doesn't seem to be the case here. At any rate, that error message has practically nothing to do with the line you get it on.

    MS' own recommendation how to deal with this error:

    Quote Originally Posted by MSDN
    The compiler found too many errors to continue. Fix the errors and recompile.


    EDIT: BTW, something that made me curious about your code: Why does your Form1 have an owner of type Form2? Shouldn't that be the other way 'round?
    Last edited by Eri523; April 22nd, 2011 at 06:04 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured