2009-10-19 8 views
0

en essayant d'exécuter this basic form control example sur msdn.Création d'un contrôle Windows Forms (C++)

À l'étape 1 de la partie «Pour ajouter une propriété personnalisée à un contrôle», nous plaçons le code ClickAnywhere dans la section publique de la classe.

Première erreur: "erreur C2144: erreur de syntaxe: 'bool' doit être précédé de ';' "

Cette syntaxe est-elle correcte en C++? (Voir ci-dessous)
(enlever la partie ClickAnywhere du code, il compile bien ...)

#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 clickcounter 
{ 
    /// <summary> 
    /// Summary for clickcounterControl 
    /// </summary> 
    /// 
    /// 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. 
    public __gc class clickcounterControl : public System::Windows::Forms::UserControl 
    { 
    public: 



//Problem code***** 


property bool ClickAnywhere { //Is this syntax right in C++? 
    bool get() { 
     return (label1->Dock == DockStyle::Fill); 
    } 
    void set(bool val) { 
     if (val) 
      label1->Dock = DockStyle::Fill; 
     else 
      label1->Dock = DockStyle::None; 
    } 
} 
//End Problem code***** 


     clickcounterControl(void) 
     { 
      InitializeComponent(); 
     } 



    protected: 
     void Dispose(Boolean disposing) 
     { 
      if (disposing && components) 
      { 
       components->Dispose(); 
      } 
      __super::Dispose(disposing); 
     } 
    private: System::Windows::Forms::Label * label1; 

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

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     void InitializeComponent(void) 
     { 
      this->label1 = new System::Windows::Forms::Label(); 
      this->SuspendLayout(); 
      // 
      // label1 
      // 
      this->label1->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle; 
      this->label1->Location = System::Drawing::Point(32, 40); 
      this->label1->Name = S"label1"; 
      this->label1->Size = System::Drawing::Size(30, 20); 
      this->label1->TabIndex = 0; 
      this->label1->Text = S"0"; 
      this->label1->TextAlign = System::Drawing::ContentAlignment::MiddleCenter; 
      this->label1->Click += new System::EventHandler(this, label1_Click); 
      // 
      // clickcounterControl 
      // 
      this->Controls->Add(this->label1); 
      this->Name = S"clickcounterControl"; 
      this->Size = System::Drawing::Size(100, 100); 
      this->ResumeLayout(false); 

     } 
    private: System::Void label1_Click(System::Object * sender, System::EventArgs * e) 
      { 
       int temp = System::Int32::Parse(label1->Text); 
       temp++; 
       label1->Text = temp.ToString(); 
      } 

    }; 
} 
+2

Pour l'anecdote, il s'agit de C++/CLR, pas simplement de C++. – GManNickG

+0

Ceci est vrai. Ai-je besoin d'un paramètre configuré différemment pour compiler ceci dans Visual C++ .net 2003? –

+0

Peut-être. Démarrez un nouveau projet sous CLR et choisissez Windows Form Application. Voyez si cela corrige quelque chose. – GManNickG

Répondre

2

Puisque vous utilisez Visual Studio .NET 2003, vous en utilisant C++ managée, pas C++/CLI. Il y a une différence significative dans la syntaxe. Pour une propriété, vous devez utiliser le mot clé __property, pas le mot clé C++/CLI property et son nouveau style.

Il devrait donc être:

__property bool get_ClickAnywhere() { 
    return (label1->Dock == DockStyle::Fill); 
} 
__property void set_ClickAnywhere(bool value) { 
    if (value) 
     label1->Dock = DockStyle::Fill; 
    else 
     label1->Dock = DockStyle::None; 
} 

On dirait que vous êtes trébuché en suivant un guide écrit pour C++/CLI (Visual Studio 2005 et versions ultérieures) tout en utilisant Visual Studio 2003.

Questions connexes