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

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    40

    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

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Do you use header guards?
    Code:
    #ifndef SOME_NAME
    #define SOME_NAME
    ...
    #endif
    or for VC++
    Code:
    #pragma once
    Maybe that's your problem.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    40
    Quote Originally Posted by Sipher View Post
    Do you use header guards?
    Code:
    #ifndef SOME_NAME
    #define SOME_NAME
    ...
    #endif
    or for VC++
    Code:
    #pragma once
    Maybe that's your problem.
    I am using vc++ which i use pragma once, so do you have any idea how to solve it?

    Here it is my form1 code:

    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
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I do not think your approach will work.

    You need to access the same instance of Form1, not just any instance (or a 'new' one you just created).

    IMO it would be better to pass the menu states as part of the form2's constructor OR
    use a relationship between the forms (ie use the parent -> child relationship to find the correct instance of the parent [Form1] from the child [Form2] using something like GetParent())

    Personally I would create a struct / class to hold the menu settings (assuming there are more than 2-4) and pass a copy of this struct into Form2 (in Form1::button1_Click).

    Code:
    create a Form2 object with new, 
    call a method to set menu states/options in this instance of Form2
    showDialog Form2
    Either use Form2's constructor to pass the struct or create a member in Form2 (ie a 'set' method of the 'Menu setting' property).


    BTW I do not trust the pragma to work correctly in all cases.
    I use the 'guards', or as they used to be called 'conditional compilation'.

    You could need a forward declaration (empty class stub) but that would be if your approach was actually going to work, which I do not think it will.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you get a submenu to come out from a menu item?
    By Swarvy in forum Windows Programming
    Replies: 2
    Last Post: 09-26-2008, 09:51 PM
  2. going about opening a .chm file from a menu item
    By DarkViper in forum Windows Programming
    Replies: 4
    Last Post: 02-20-2004, 04:16 PM
  3. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  5. enable disabled menu item.
    By Apache in forum Windows Programming
    Replies: 7
    Last Post: 02-12-2002, 08:29 PM