首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在TPageControl中TabSheet1Show中添加控件

在TPageControl中TabSheet1Show中添加控件
EN

Stack Overflow用户
提问于 2017-03-07 18:10:41
回答 1查看 227关注 0票数 0

当我试图在运行时将控件添加到已经存在的TTabSheet中时,这些控件在OnShow事件TTabSheet中添加时将保持不可见性。

复制步骤:

  1. TPageControl添加到设计器中的TForm
  2. 在设计器中向此TTabSheet添加3个TPageControl对象。
  3. 设置第一个TTabSheet活动(在设计时)。
  4. 运行下面的代码:

头文件:

代码语言:javascript
复制
#ifndef Unit1H
#define Unit1H

#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ComCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // Von der IDE verwaltete Komponenten
    TPageControl *PageControl1;
    TTabSheet *TabSheet1;
    TTabSheet *TabSheet2;
    TTabSheet *TabSheet3;
    TButton *Button1;
    void __fastcall TabSheet1Show(TObject *Sender);
private:    // Benutzer-Deklarationen
    TButton *ButtonConstructor;
    TButton *ButtonOnTabShow;
public:     // Benutzer-Deklarationen
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

源文件:

代码语言:javascript
复制
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    // Adding a TButton in the Form's constructor works

    TTabSheet *ts = this->TabSheet1;

    if (!this->ButtonConstructor)
    {
        ButtonConstructor = new TButton( ts );
        ButtonConstructor->Name = "ButtonConstructor";
        ButtonConstructor->Caption = "Construct";
        ButtonConstructor->Parent = ts;
    }


}
//---------------------------------------------------------------------------
void __fastcall TForm1::TabSheet1Show(TObject *Sender)
{
    // Adding a TButton in the OnShow Event of TTabSheet does NOT work:
    // The button stays invisible

    TTabSheet *ts = dynamic_cast< TTabSheet * >( Sender );
    // TTabSheet *ts = this->ButtonOnTabShow;   // does not make any difference

    if (!this->ButtonOnTabShow)
    {
        ButtonOnTabShow = new TButton( ts );
        ButtonOnTabShow->Name = "ButtonOnTabShow";
        ButtonOnTabShow->Caption = "Show";
        ButtonOnTabShow->Parent = ts;
        // Button should be placed below the other
        ButtonOnTabShow->Top = ButtonConstructor->Top + ButtonConstructor->Height + 2;
    }

    // The following 2 lines would make the Button visible
    // PageControl1->ActivePageIndex = 1;
    // PageControl1->ActivePageIndex = 0;
}

结果是:

  • ButtonConstructor是可见的
  • ButtonOnTabShow不可见

如果单击TabSheet2,然后返回到TabSheet1,则ButtonOnTabShow也将是可见的。

这是一个无法解决的错误,还是我遗漏了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-07 19:03:50

我不知道为什么会发生,但我可以复制。这可能与TPageControl管理其TTabSheet对象的可见性有关(因为Win32 API没有选项卡的概念。TTabSheet完全是VCL的发明,目的是简化对选项卡内容的管理。但是,使用以下代码很容易:

代码语言:javascript
复制
#define WM_ENSUREBUTTONSHOWN (WM_APP+1)

void __fastcall TForm1::TabSheet1Show(TObject *Sender)
{
    // ...

    if (!this->ButtonOnTabShow)
    {
        ButtonOnTabShow = new TButton( ts );
        // ...
        PostMessage(Handle, WM_ENSUREBUTTONSHOWN, 0, 0);
    }
}

void __fastcall TForm1::WndProc(TMessage &Message)
{
    TForm::WndProc(Message);
    if ((Message.Msg == WM_ENSUREBUTTONSHOWN) && (this->ButtonOnTabShow))
    {
        this->ButtonOnTabShow->Hide();
        this->ButtonOnTabShow->Show();
    }
}

另一种选择是:

代码语言:javascript
复制
#define WM_REFRESHTABSHEET (WM_APP+1)

void __fastcall TForm1::TabSheet1Show(TObject *Sender)
{
    // ...

    if (!this->ButtonOnTabShow)
    {
        ButtonOnTabShow = new TButton( ts );
        // ...
        PostMessage(Handle, WM_REFRESHTABSHEET, 0, 0);
    }
}

void __fastcall TForm1::WndProc(TMessage &Message)
{
    TForm::WndProc(Message);
    if ((Message.Msg == WM_REFRESHTABSHEET) && (this->TabSheet1->Visible))
    {
        this->TabSheet1->Hide();
        this->TabSheet1->Show();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42655347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档