简体   繁体   English

创建Windows窗体控件(C ++)

[英]Creating a Windows Forms Control (C++)

trying to run this basic form control example on msdn. 尝试在msdn上运行此基本表单控件示例

At step 1 of the portion " To add a custom property to a control " we place the ClickAnywhere code in the public section of the class. 在“ 向控件添加自定义属性 ”部分的步骤1,我们将ClickAnywhere代码放置在类的公共部分中。

First error: " error C2144: syntax error : 'bool' should be preceded by ';' 第一个错误:“ 错误C2144:语法错误:'bool'应该以';'开头 "

Is this syntax correct in C++? 这种语法在C ++中正确吗? (see below) (见下文)
(removing the ClickAnywhere portion of code, it compiles fine...) (删除代码的ClickAnywhere部分,可以正常编译...)

#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();
             }

    };
}

Since you are using Visual Studio .Net 2003, you are using Managed C++, not C++/CLI. 由于使用的是Visual Studio .Net 2003,因此使用的是Managed C ++,而不是C ++ / CLI。 There is a significant difference in syntax. 语法上有很大的不同。 For a property, you must use the __property keyword, not the C++/CLI property keyword and its new style. 对于属性,必须使用__property关键字,而不是C ++ / CLI property关键字及其新样式。

It should therefore be: 因此应为:

__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;
}

It looks like you are being tripped up by following a guide written for C++/CLI (Visual Studio 2005 and later) while still using Visual Studio 2003. 看起来您在使用Visual Studio 2003的同时仍遵循为C ++ / CLI(Visual Studio 2005及更高版本)编写的指南而感到困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM