【问题标题】:C++ .NET VisibleChanged Called if parent is set visible, but not if parent is set not visibleC++ .NET VisibleChanged 如果父级设置为可见则调用,但如果父级设置为不可见则不调用
【发布时间】:2015-02-06 18:25:44
【问题描述】:

我有一个包含面板的表单,它本身包含一个标签。附加到标签的 VisibleChanged。我在表单中添加了两个按钮。一个将面板的 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;
         }
};
}

附言。我正在使用 Visual Studio 2012 更新 4。

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

【问题讨论】:

  • 这不是 C++,而是使用 WinForms 的 C++/CLI。我已经更新了标签以反映这一点。
  • 您的代码没有更改标签的 Visible 属性。您只是通过隐藏其父标签来隐藏标签。如果您希望事件触发,那么您必须设置 label1->Visible = false;请注意,Visible 属性很古怪,将其设置为 true 并不能保证您在读取该属性时会返回 true。 setter 使 intent 可见,getter 返回它是否实际上 可见。
  • @HansPassant 是的,我只更改父级。在这种情况下,只有一个标签,我可以更改它,但这只是一个小程序,以显示我在更大的应用程序中遇到的问题,即我无法直接更改面板内的控件。
  • 这是此类问题的更大问题,它们没有向我们展示为什么 VisibleChanged 事件对您很重要。我从不使用它,所以也无法猜测。如果您不告诉我们,我们无法为您提供解决方法。
  • 查看SetVisibleCore 以及调用它的条件。您也可以为所有父控件订阅VisibleChanged 事件。

标签: winforms c++-cli


【解决方案1】:

现在你有

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

也添加

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

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

【讨论】:

    猜你喜欢
    • 2021-11-19
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    • 2019-04-04
    • 2014-03-23
    相关资源
    最近更新 更多