2013-07-25 63 views
0

我不知道如何在窗體和另一個頭文件之間共享數據。 變量輸入存在問題。 我不知道如何聲明它可以從窗體訪問。主要計算在other.h中。 我想在表單中設置一個變量並將其發送到other.h,計算並返回表單。 如何聲明int輸入?在Visual C中的窗體和頭文件之間共享變量

注: 問題是btnWrong

// Form1.h 

#pragma once 
    #include "other.h" 


    namespace VariablesandDebugging { 


     using namespace System; 
     using namespace System::ComponentModel; 
     using namespace System::Collections; 
     using namespace System::Windows::Forms; 
     using namespace System::Data; 
     using namespace System::Drawing; 

     /// <summary> 
     /// Summary for Form1 
     /// </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::PictureBox^ pictureBox1; 
     private: System::Windows::Forms::Label^ lblHeader1; 
     private: System::Windows::Forms::Button^ btnRight; 
     private: System::Windows::Forms::Button^ btnWrong; 

     private: 
      /// <summary> 
      /// Required designer variable. 
      /// </summary> 
      System::ComponentModel::Container ^components; 

      //My variables 
     private: 
      int iInches; 
      int iFeet; 
      bool bConvertingToInches;//??? 
      int iConvertedInches; 
      int iRozpeti; 


    #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->btnRight = (gcnew System::Windows::Forms::Button()); 
       this->btnWrong = (gcnew System::Windows::Forms::Button()); 
       // 
       // btnConvert 
       // 
       this->btnRight->Location = System::Drawing::Point(60, 60); 
       this->btnRight->Text = L"In Form1"; 
       this->btnRight->Click += gcnew System::EventHandler(this, &Form1::btnRight_Click); 
       this->Controls->Add(this->btnRight); 
       // 
       // btnExit 
       // 
       this->btnWrong->Location = System::Drawing::Point(60, 150); 
       this->btnWrong->Text = L"In other.h"; 
       this->btnWrong->Click += gcnew System::EventHandler(this, &Form1::btnWrong_Click); 
       this->Controls->Add(this->btnWrong); 


      } 
    #pragma endregion 
    public: System::Void btnWrong_Click(System::Object^ sender, System::EventArgs^ e) { 
       getfavoritenumber2(a, b); 
       MessageBox::Show(Convert::ToString(a)); 
       MessageBox::Show(Convert::ToString(b)); 
       } 

    public: System::Void btnRight_Click(System::Object^ sender, System::EventArgs^ e) { 
       input = 10; 
       getfavoritenumber3(a, b, output); 
       MessageBox::Show(Convert::ToString(a)); 
       MessageBox::Show(Convert::ToString(b)); 
       MessageBox::Show(Convert::ToString(output)); 
      } 
     }; 
    } 



//other.h 

#ifndef _OTHER_H_ 
#define _OTHER_H_ 

int a; 
int b; 
int input; 

void getfavoritenumber2(int &a, int &b) 
{ 
    a = 3; 
    b = 6; 
} 

void getfavoritenumber3(int &a, int &b, int &output) 
{ 
    a = 3; 
    b = 6; 
    output = input*input; 
} 

#endif 

非常感謝您

+0

....'C'標籤??? –

+1

@Dayalrai它必須是'C with Classes';) – jpw

回答

0

您可以使用文件範圍的靜態變量。

但這很舊,壞主意,所以考慮全球/ EXTERN