简单的GUI不起作用。 [C ++]

时间:2014-02-02 10:37:54

标签: winforms c++-cli

我只需输入所有内容,例如我书中的示例。你能告诉我为什么它不起作用吗?

感谢您的关注。


这是布局。

enter image description here

这是Example_1.cpp。

// Example_1.cpp : main project file.

#include "stdafx.h"
#include "Form1.h"

using namespace Example_1;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
    // Enabling Windows XP visual effects before any controls are created
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false); 

    // Create the main window and run it
    Application::Run(gcnew Form1());
    return 0;
}

以下是代码。

#pragma once

namespace Example_1 {

    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::Button^  button1;
    private: System::Windows::Forms::Label^  label1;
    private: System::Windows::Forms::TextBox^  textBox1;
    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->label1 = (gcnew System::Windows::Forms::Label());
            this->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->SuspendLayout();
            // 
            // button1
            // 
            this->button1->Location = System::Drawing::Point(154, 74);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(104, 23);
            this->button1->TabIndex = 0;
            this->button1->Text = L"OK";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Location = System::Drawing::Point(12, 9);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(118, 13);
            this->label1->TabIndex = 1;
            this->label1->Text = L"Please enter you name.";
            // 
            // textBox1
            // 
            this->textBox1->Location = System::Drawing::Point(15, 35);
            this->textBox1->Name = L"textBox1";
            this->textBox1->Size = System::Drawing::Size(243, 20);
            this->textBox1->TabIndex = 2;
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(273, 110);
            this->Controls->Add(this->textBox1);
            this->Controls->Add(this->label1);
            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);
            this->PerformLayout();

        }
#pragma endregion
    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
             }
    private: System::Void button1_Click(System::Object^  sender, 
    System::EventArgs^  e) {
             String^ message = "Hello";
             String^ title = "Welcome";
             MessageBox::Show( message, title, MessageBoxButtons::OK );
             }
    };
}

以下是输出。

1>------ Build started: Project: Example_1, Configuration: Debug Win32 ------
1>Build started 02-Feb-14 6:28:11 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\Example_1.unsuccessfulbuild".
1>GenerateTargetFrameworkMonikerAttribute:
1>Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
1>ClCompile:
1>  All outputs are up-to-date.
1>  Example_1.cpp
1>c:\users\user\desktop\example_1\example_1\Form1.h(108): error C2653: 'MessageBoxButton' : is not a class or namespace name
1>c:\users\user\desktop\example_1\example_1\Form1.h(108): error C2065: 'OK' : undeclared identifier
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.88
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

2 个答案:

答案 0 :(得分:1)

您忘记了MessageBox类的命名空间:

 using namespace System::Windows;

答案 1 :(得分:1)

也许试试这个: 系统:视窗:形式:的MessageBox 还有这个: 系统:视窗:形式:MessageBoxButtons (你忘了s)

链接: http://msdn.microsoft.com/en-us/library/system.windows.forms%28v=vs.110%29.aspxhttp://msdn.microsoft.com/en-us/library/system.windows.forms.messageboxbuttons%28v=vs.110%29.aspx