DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: only static data members can be initialized inside a ref class or value type

  1. #1
    Join Date
    Apr 2011
    Posts
    20

    only static data members can be initialized inside a ref class or value type

    Hi guys,

    Do any of you know how to correct the error when using bool OkToClose = false;??

    The error I have got is:

    Error: error C3845: 'MyApplication::Form1::OkToClose': only static data members can be initialized inside a ref class or value type


    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;
    
    namespace Form1 {
    
    	/// <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;
    			}
    		}
    	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->SuspendLayout();
    			// 
    			// 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->comboBox1);
    			this->Controls->Add(this->button1);
    			this->Name = L"Form1";
    			this->Text = L"Form1";
    			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
    			this->ResumeLayout(false);
    
    		}
    #pragma endregion
    
    bool OkToClose = false;
    };
    }
    Any advice would be much appreciate.

    Thanks,
    Mark
    Last edited by mark108; 04-29-2011 at 07:59 PM.

  2. #2
    Join Date
    Dec 2007
    Posts
    401
    Just declare the member variable in the class and initialize it in the constructor.
    Code:
    public ref class Form1: public System::Windows::Forms::Form
    {
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    			//
    			//TODO: Add the constructor code here
    			//
                            OkToClose = false ;
    		}
    
            // ...
    
            bool OkToClose ;
    };

  3. #3
    Join Date
    Apr 2011
    Posts
    20
    Thanks for your help vijayan, I can see myself that it has been solve, but I am having a bit of trouble of closing the form. I am using RightToLeft::Yes and RightToLeftLayout properties to set the form from left to the right and I am also using the code to block the form from being execute while using RightToLeft::Yes and RightToLeftLayout properties, without using the block code it will be execute every time when I am trying to opening the form.


    I tried to use this:

    Code:
    Code:
    private: System::Void Form2_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e) {
    if(!OkToClose)
    {
     e->Cancel = true;
    }
    }
    The form will be execute when I opening the form every time, so I am now using the current code which it block the form from being execute while using RightToLeft::Yes and RightToLeftLayout properties:

    Code:
    Code:
    private: System::Void Form2_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e) {
    if(OkToClose)
    {
     e->Cancel = true;
    }
    }
    Now, I am trying to close the form, but I can't be able to do because I am using the block code to avoid the form from being execute when I opening the form. I can't find a way to close the form. Do you know how to close the form after I have block them from being execute when I am still using RightToLeft::Yes and RightToLeftLayout properties?



    Thanks,
    Mark
    Last edited by mark108; 05-03-2011 at 03:52 PM.

  4. #4
    Join Date
    Dec 2007
    Posts
    401
    Don't know much about this C++/CLI stuff, so I'm punting.

    Perhaps, something like:
    Code:
    private: System::Void Form2_FormClosing( System::Object^  sender, 
                                  System::Windows::Forms::FormClosingEventArgs^  e ) 
    {
            e->Cancel = ! OkToClose ;
            // same as 
            // if( OkToClose ) e->Cancel = false ;
            // else e->Cancel = true ;
    }

  5. #5
    Join Date
    Apr 2011
    Posts
    20
    Thanks for your help. When I click on the X button on the top left corner or when I select on the titlebar and click "Close", the form will not close it. Do you have any idea how to close it? something is like override or something using a different way?

  6. #6
    Join Date
    Dec 2007
    Posts
    401
    I'm sorry, I'm completely out of my depth in C++/CLI, WinForms etc.

    Hopefully, someone else would be able to assist you.

  7. #7
    Join Date
    Dec 2003
    Posts
    3,366
    A form is closed in visual c++ by sending a message. The x in the corner menu or alt+f4 and escape all do the same thing: send the message.

    So if you go into the dialog and add a message handler for on close message, you can over-ride the behavior (for example, make the dialog only closable by a button you click rather than alt+f4 or escape).

    For you, since you want to close it, you should be able to add a command to close the window by sending the message when the system menu command to close is invoked.

    I realize that is vague: I am not at work today. However take a look at that and if you cannot find it I will dig out the actual code and process from my notes if I remember to do that monday.

  8. #8
    Join Date
    Dec 2003
    Posts
    3,366
    I found it.

    One thing you can do is define the "OnCommand" routine for your dialog. Whatever else you choose to do inside the routine, if you simply pass along

    CDialog::OnCommand(wParam, lParam);

    it will exit normally when the exit commands are given.

    Normal dialogs exit when wParam is < 3 and lParam = 0 --- this covers the escape key, enter key, alt f4, x system button, etc.


    Later, if you want to exit the program another way, such as with a button or file-exit menu, you can simply invoke the same routine with
    CDialog::OnCommand(1,0); -- this will send the message that you require to exit the dialog cleanly. There is a way to do it directly with the message pump commands but I do not have that handy.
    ----------------
    I do not see why you would need to add this into your code to restore the default behavior, but it may work. At the very least, calling the (1,0) directly may let you add back in what you require.

Similar Threads

  1. Structures and Data Referance Methods
    By rrjii2000 in forum .NET
    Replies: 60
    Last Post: 05-04-2010, 04:17 PM
  2. please help me Answer these questions
    By salman1986 in forum .NET
    Replies: 8
    Last Post: 04-07-2010, 07:56 AM
  3. Singleton
    By ami in forum C++
    Replies: 10
    Last Post: 01-22-2007, 09:13 AM
  4. TicTacToe help...
    By MP35719 in forum Java
    Replies: 0
    Last Post: 12-16-2006, 07:06 PM
  5. Data Type Mismatch
    By maria in forum VB Classic
    Replies: 0
    Last Post: 05-01-2001, 06:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center