Winforms C++。NET VisibleChanged在父项设置为可见时调用,但在父项设置为不可见时不调用

Winforms C++。NET VisibleChanged在父项设置为可见时调用,但在父项设置为不可见时不调用,winforms,c++-cli,Winforms,C++ Cli,我得到了一个包含面板的表单,它本身包含一个标签。附加到标签的可见部分。我在表单中添加了两个按钮。一个将面板的Visible设置为true,另一个将相同属性设置为false。 单击visible按钮时,将引发VisibleChanged事件。隐藏面板时,事件不会启动。我做错了什么 这是我表格的代码 #pragma once namespace CLRConsoleApplication1 { using namespace System; using namespace System::Com

我得到了一个包含面板的表单,它本身包含一个标签。附加到标签的可见部分。我在表单中添加了两个按钮。一个将面板的Visible设置为true,另一个将相同属性设置为false。 单击visible按钮时,将引发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;
         }
};
}
#pragma一次
命名空间CLRConsoleApplication1{
使用名称空间系统;
使用名称空间System::ComponentModel;
使用名称空间系统::集合;
使用命名空间System::Windows::Forms;
使用名称空间System::Data;
使用名称空间系统::绘图;
/// 
///我的表格摘要
/// 
公共引用类MyForm:public System::Windows::Forms::Form
{
公众:
我的表格(作废)
{
初始化组件();
//
//TODO:在此处添加构造函数代码
//
}
受保护的:
/// 
///清理所有正在使用的资源。
/// 
~MyForm()
{
if(组件)
{
删除组件;
}
}
私有:系统::Windows::窗体::面板^panel1;
私有:系统::Windows::窗体::标签^label1;
私有:系统::Windows::窗体::按钮^ShowButton;
私有:系统::Windows::窗体::按钮^HideButton;
受保护的:
私人:
/// 
///必需的设计器变量。
/// 
系统::组件模型::容器^组件;
#pragma区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
void初始化组件(void)
{
这->panel1=(gcnewsystem::Windows::Forms::Panel());
这->标签1=(gcnewsystem::Windows::Forms::Label());
此->显示按钮=(gcnew System::Windows::Forms::Button());
this->HideButton=(gcnewsystem::Windows::Forms::Button());
此->面板1->SuspendLayout();
此->SuspendLayout();
// 
//小组1
// 
此->面板1->控件->添加(此->标签1);
此->面板1->位置=系统::绘图::点(32,32);
此->面板1->名称=L“面板1”;
此->面板1->尺寸=系统::图纸::尺寸(200108);
此->面板1->选项卡索引=0;
// 
//标签1
// 
此->标签1->自动调整大小=真;
该->标签1->位置=系统::图纸::点(60,38);
此->标签1->名称=L“标签1”;
该->标签1->尺寸=系统::图纸::尺寸(35,13);
此->标签1->选项卡索引=0;
此->标签1->文本=L“标签1”;
this->label1->VisibleChanged+=gcnewsystem::EventHandler(this,&MyForm::label1\u VisibleChanged);
// 
//显示按钮
// 
此->显示按钮->位置=系统::绘图::点(32262);
此->显示按钮->名称=L“显示按钮”;
这个->显示按钮->大小=系统::绘图::大小(75,23);
此->显示按钮->选项卡索引=1;
此->显示按钮->文本=L“显示”;
此->显示按钮->UseVisualStyleBackColor=true;
this->ShowButton->Click+=gcnewsystem::EventHandler(this,&MyForm::ShowButton\u Click);
// 
//隐藏按钮
// 
此->隐藏按钮->位置=系统::绘图::点(157162);
此->隐藏按钮->名称=L“隐藏按钮”;
此->隐藏按钮->大小=系统::绘图::大小(75,23);
此->隐藏按钮->选项卡索引=2;
此->隐藏按钮->文本=L“隐藏”;
此->隐藏按钮->UseVisualStyleBackColor=true;
this->HideButton->Click+=gcnewsystem::EventHandler(this,&MyForm::HideButton\u Click);
// 
//MyForm
// 
这->自动缩放尺寸=系统::图纸::尺寸(6,13);
这->AutoScaleMode=System::Windows::Forms::AutoScaleMode::Font;
这->ClientSize=System::Drawing::Size(284262);
此->控制->添加(此->隐藏按钮);
此->控制->添加(此->显示按钮);
此->控制->添加(此->面板1);
此->名称=L“MyForm”;
此->文本=L“MyForm”;
此->面板1->恢复布局(错误);
此->面板1->性能布局();
此->恢复布局(错误);
}
#布拉格端区
private:System::Void label1\u VisibleChanged(System::Object^sender,System::EventArgs^e){
System::Windows::Forms::MessageBox::Show(“label1\u VisibleChanged:”+label1->Visible);
}
private:System::Void HideButton_单击(系统::对象^sender,系统::事件参数^e){
panel1->Visible=false;
}
私有:系统::无效显示按钮\单击(系统::对象^sender,系统::事件参数^e){
panel1->Visible=true;
}
};
}
另外,我正在使用Visual Studio 2012更新4

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

现在您有了

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

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

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

这不是C++,而是使用WiFrm的C++/CLI。我已更新标记以反映这一点。您的代码未更改标签的Visible属性。您只是通过隐藏标签的父项来隐藏标签。如果希望触发事件,则必须设置label1->Visible=false;请注意,Visible属性很奇怪,将其设置为true并不保证在读取该属性时返回true。setter给出了可见的意图,getter返回它是否实际可见。@HansPassant是的,我只更改父级。在本例中,仅对于一个标签,我可以更改它,但它只是一个小程序,用于显示我遇到的问题