C ++ .NET VisibleChanged如果父项设置为可见,则调用,但如果父项设置不可见则调用

时间:2015-02-06 18:25:45

标签: winforms c++-cli

我有一个包含面板的表单,它本身包含一个标签。附加到标签的VisibleChanged。我在表单中添加了两个按钮。一个将Panel的Visible设置为true,另一个将相同属性设置为false。 单击可见按钮时,将抛出VisibleChanged事件。当我隐藏面板时,事件不会启动。我做错了什么。

这是我表格的代码。

#pragma once

namespace CLRConsoleApplication1 {

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 MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
    MyForm(void)
    {
        InitializeComponent();
        //
        //TODO: Add the constructor code here
        //
    }

protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~MyForm()
    {
        if (components)
        {
            delete components;
        }
    }
private: System::Windows::Forms::Panel^  panel1;
private: System::Windows::Forms::Label^  label1;
private: System::Windows::Forms::Button^  ShowButton;
private: System::Windows::Forms::Button^  HideButton;
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->panel1 = (gcnew System::Windows::Forms::Panel());
        this->label1 = (gcnew System::Windows::Forms::Label());
        this->ShowButton = (gcnew System::Windows::Forms::Button());
        this->HideButton = (gcnew System::Windows::Forms::Button());
        this->panel1->SuspendLayout();
        this->SuspendLayout();
        // 
        // panel1
        // 
        this->panel1->Controls->Add(this->label1);
        this->panel1->Location = System::Drawing::Point(32, 32);
        this->panel1->Name = L"panel1";
        this->panel1->Size = System::Drawing::Size(200, 108);
        this->panel1->TabIndex = 0;
        // 
        // label1
        // 
        this->label1->AutoSize = true;
        this->label1->Location = System::Drawing::Point(60, 38);
        this->label1->Name = L"label1";
        this->label1->Size = System::Drawing::Size(35, 13);
        this->label1->TabIndex = 0;
        this->label1->Text = L"label1";
        this->label1->VisibleChanged += gcnew System::EventHandler(this, &MyForm::label1_VisibleChanged);
        // 
        // ShowButton
        // 
        this->ShowButton->Location = System::Drawing::Point(32, 162);
        this->ShowButton->Name = L"ShowButton";
        this->ShowButton->Size = System::Drawing::Size(75, 23);
        this->ShowButton->TabIndex = 1;
        this->ShowButton->Text = L"Show";
        this->ShowButton->UseVisualStyleBackColor = true;
        this->ShowButton->Click += gcnew System::EventHandler(this, &MyForm::ShowButton_Click);
        // 
        // HideButton
        // 
        this->HideButton->Location = System::Drawing::Point(157, 162);
        this->HideButton->Name = L"HideButton";
        this->HideButton->Size = System::Drawing::Size(75, 23);
        this->HideButton->TabIndex = 2;
        this->HideButton->Text = L"Hide";
        this->HideButton->UseVisualStyleBackColor = true;
        this->HideButton->Click += gcnew System::EventHandler(this, &MyForm::HideButton_Click);
        // 
        // MyForm
        // 
        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(284, 262);
        this->Controls->Add(this->HideButton);
        this->Controls->Add(this->ShowButton);
        this->Controls->Add(this->panel1);
        this->Name = L"MyForm";
        this->Text = L"MyForm";
        this->panel1->ResumeLayout(false);
        this->panel1->PerformLayout();
        this->ResumeLayout(false);

    }
#pragma endregion
private: System::Void label1_VisibleChanged(System::Object^  sender, System::EventArgs^  e) {
             System::Windows::Forms::MessageBox::Show( "label1_VisibleChanged: " + label1->Visible );
         }
private: System::Void HideButton_Click(System::Object^  sender, System::EventArgs^  e) {
             panel1->Visible = false;
         }
private: System::Void ShowButton_Click(System::Object^  sender, System::EventArgs^  e) {
             panel1->Visible = true;
         }
};
}

PS。我正在使用Visual Studio 2012 Update 4。

编辑:这里有更多信息。我可以访问控件。另一个项目可以访问相同的控件,并放入表单内的面板内。然后根据用户请求隐藏或显示面板。 在我这边,我不知道小组,只知道控制。我想知道控件何时变得可见或隐藏,因为我需要相应地更改一些相关控件。

1 个答案:

答案 0 :(得分:0)

现在你有

label1->VisibleChanged += gcnew System::EventHandler(this, &MyForm::label1_VisibleChanged);

也添加

panel1->VisibleChanged += gcnew System::EventHandler(this, &MyForm::label1_VisibleChanged);

看看你是否得到了理想的结果。