动态生成的TabSheet上的DBGrid为什么显示不正常? 有谁遇到过?

nononono 2001-10-18 11:36:55
在Form的OnCreate事件中:

在一个PageControl上动态生成(后面的控件当然也是动态生成的)N个TabSheet, 每个TabSheet放置2个Panel, 其中一个Panel上放置1个DBGrid.

给每个DBGrid都设置了适当的数据源, 并保证已经打开(Query->Avtice = true).

如果TabSheet的数量N = 1, 正常.

问题:
如果TabSheet的数量N > 1, 默认显示的第一页(ActivePageIndex=0), 这时的DBGrid看起来就是数据源没有打开的样子.
如果用鼠标变化一下当前页, 一切都正常了.
如果用代码变化一下当前页(ActivePageIndex=1;ActivePageIndex=0), 一切也都正常了. 现在是用这种方法绕开这个麻烦, 但这样与我的设计有个冲突. 我希望能有正解.

可以肯定第一页的DBGrid的数据源是打开的, 应该是显示的问题. 记得在静态设计使用页框控件时也出现过类似的问题.

调用过Form以及各相关控件的Refresh/Repaint, 无效.

想知道原因以及正确的解决办法.

那位遇到这种情况并解决了?

...全文
243 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
nononono 2001-10-23
  • 打赏
  • 举报
回复
总结:

确实如Chxis(明月夜,古松冈.)所说,去掉"TabSheet->Visible = true; "即可。

想起来这个TTabSheet的确与其它控件有所不同,别的控件需要设定Parent,而它设定的是PageControl。另外,TTabSheet也不能通过改变Visible属性来达到隐藏页的目的。到现在我还不知道怎么能够隐藏页。

本帖结束。

nononono 2001-10-22
  • 打赏
  • 举报
回复
原代码来了。

这是我已经清理了与此内容无关的代码后的文件内容,可以运行并可以体现该问题。有心者可以试试。

*****************************************************************************
1. **** File Unit_COMM_VIEW.CPP
*****************************************************************************
//---------------------------------------------------------------------------
#include <vcl.h>
#include <dbgrids.hpp>
#pragma hdrstop

#include "Unit_COMM_VIEW.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
__fastcall TForm_COMM_VIEW::TForm_COMM_VIEW(TComponent* Owner,
AnsiString *pSQLArr,
int NumOfQuerys)
: TForm(Owner)
{
this->NumberOfQuerys = NumOfQuerys;
for (int i=0; i<this->NumberOfQuerys; i++)
this->SQLArr[i] = pSQLArr[i];
}
//---------------------------------------------------------------------------
void COMM_Query_VIEW(AnsiString *pSQLArr, int NumOfQuerys)
{
TForm_COMM_VIEW *Form_COMM_VIEW;
Form_COMM_VIEW = new TForm_COMM_VIEW(Application,pSQLArr,NumOfQuerys);
Form_COMM_VIEW->Show();
}
//---------------------------------------------------------------------------
void __fastcall TForm_COMM_VIEW::FormCreate(TObject *Sender)
{
//----------------------------------------
PageControl1->MultiLine = true;
for (int i=0; i<this->NumberOfQuerys; i++)
{
TQuery *Query;
Query = new TQuery(this);
Query->Name = "Query"+IntToStr(i);
Query->DatabaseName = "databasename"; // TDatabase控件的databasename或BDE的别名
Query->SQL->Clear();
Query->SQL->Add(SQLArr[i]);
if (i==0)
Query->Active = true;

TDataSource *DataSource;
DataSource = new TDataSource(this);
DataSource->Name = "DataSource"+IntToStr(i);
DataSource->DataSet = Query;

TTabSheet *TabSheet;
TabSheet = new TTabSheet(this);
TabSheet->Name = "TabSheet"+IntToStr(i);
TabSheet->PageControl = PageControl1;
TabSheet->PageIndex = i;
TabSheet->Caption =TabSheet->Name;
TabSheet->Visible = true;

TDBGrid *DBGrid;
DBGrid = new TDBGrid(this);
DBGrid->Name = "DBGrid"+IntToStr(i);
DBGrid->Parent = TabSheet;
DBGrid->Align = alClient;
DBGrid->DataSource = DataSource;

DBGrid->Visible = true;
}
//----------------------------------------
/* 注意:如果取消下面的 2行,将看不到第一页的数据!!! */
if (NumberOfQuerys>1)
this->PageControl1->ActivePageIndex = 1;
//----------------------------------------
this->PageControl1->ActivePageIndex = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm_COMM_VIEW::Button1Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm_COMM_VIEW::FormClose(TObject *Sender,
TCloseAction &Action)
{
Action = caFree;
}
//---------------------------------------------------------------------------
*****************************************************************************
2. **** File Unit_COMM_VIEW.h
*****************************************************************************
//---------------------------------------------------------------------------

#ifndef Unit_COMM_VIEWH
#define Unit_COMM_VIEWH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
#define MaxPageNum 10
//---------------------------------------------------------------------------
class TForm_COMM_VIEW : public TForm
{
__published: // IDE-managed Components
TPageControl *PageControl1;
TPanel *Panel1;
TButton *Button1;
void __fastcall FormCreate(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
private: // User declarations
int NumberOfQuerys;
AnsiString SQLArr[MaxPageNum];

public: // User declarations
__fastcall TForm_COMM_VIEW(TComponent* Owner,
AnsiString *SQLArr,
int NumOfQuerys);

};
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
#endif
*****************************************************************************
3. **** File Unit_COMM_VIEW.dfm
*****************************************************************************
object Form_COMM_VIEW: TForm_COMM_VIEW
Left = 201
Top = 120
Width = 687
Height = 430
Caption = '浏览统计'
Color = clBtnFace
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -12
Font.Name = 'Arial'
Font.Style = []
OldCreateOrder = False
Position = poDesktopCenter
OnClose = FormClose
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 12
object PageControl1: TPageControl
Left = 0
Top = 0
Width = 679
Height = 372
Align = alClient
HotTrack = True
TabOrder = 0
end
object Panel1: TPanel
Left = 0
Top = 372
Width = 679
Height = 31
Align = alBottom
TabOrder = 1
object Button1: TButton
Left = 587
Top = 3
Width = 89
Height = 25
Cancel = True
Caption = '退出'
TabOrder = 0
OnClick = Button1Click
end
end
end
*****************************************************************************
4. **** File Unit1.cpp
*****************************************************************************
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit_COMM_VIEW.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

void COMM_Query_VIEW(AnsiString *pSQLArr, int NumOfQuerys);

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString SQLs[2];

SQLs[0] = "select * from table1";
SQLs[1] = "select * from table2";

COMM_Query_VIEW(SQLs, 2);
}
//---------------------------------------------------------------------------


nononono 2001-10-22
  • 打赏
  • 举报
回复
内存不足? 怎么会?!

greentrees 2001-10-22
  • 打赏
  • 举报
回复
内存不足吧
书生 2001-10-22
  • 打赏
  • 举报
回复
nononono
你说的问题我前不久遇到过,当时也没想清楚怎么回事,我采用了绕过问题方法自欺欺人了,和你一样,置了两次焦点就好了。现在也没细想原因。关注。
nononono 2001-10-22
  • 打赏
  • 举报
回复
"只要将 TabSheet->Visible = true; 这句删去"?

一想是有道理! 因为TabSheet->Visible的属性本来就是没用的(或者说我没觉得它有用)!

明天试过结贴.
Chxis 2001-10-22
  • 打赏
  • 举报
回复
可以
只要将
TabSheet->Visible = true;
这句删去,

那么
//----------------------------------------
/* 注意:如果取消下面的 2行,将看不到第一页的数据!!! */
if (NumberOfQuerys>1)
this->PageControl1->ActivePageIndex = 1;
//----------------------------------------
this->PageControl1->ActivePageIndex = 0;

这些都不用要了,

奇怪啊...~~
nononono 2001-10-22
  • 打赏
  • 举报
回复
lluunn007(玉笛书生【再现江湖】), 结贴时会通知你.

***************************************************************************
多谢 Dala(大拉)!
"你把所有控件全部动态生成结束后再打开Query", 有道理. 不过我原来好像是这样做的, 这里的代码是简化的. 我再试试.

"DBGrid->Align = alClient;" 这条不能去掉, 不过, 去掉了就没问题了吗? 我再试试.

***************************************************************************

多谢 Chxis(明月夜,古松冈.)!

1. 点击变换PageControl1的活动页或用如下代码变换的活动页都能达到目的.
this->PageControl1->ActivePageIndex = 1;
this->PageControl1->ActivePageIndex = 0;

* 是不是这个方法就是正解?! 呵呵

2. 我不想在一开始就打开全部的查询, 因为这样会使得模块的进入时间太长, 所以只打开了第一个.
if (i==0) Query->Open();

3. "当动态创建两个TabSheet时, 就算你 this->PageControl1->ActivePageIndex = 0; 但是显示的还是最后一个Tabsheet", 不用1的办法能更利索地解决吗?

***************************************************************************

我2天内结贴.

多谢参与.
Chxis 2001-10-22
  • 打赏
  • 举报
回复
随之,相应DBGRID的内容也出来了
Chxis 2001-10-22
  • 打赏
  • 举报
回复
所以当你点击几次tabsheet时就显示回应该显示的tabsheet的内容
Chxis 2001-10-22
  • 打赏
  • 举报
回复
我知道原因了,
原来是当动态创建两个TabSheet时,
就算你 this->PageControl1->ActivePageIndex = 0;
但是显示的还是最后一个Tabsheet,
你只要试试把
if (i==0) ///把这个去掉,
Query->Open();
就可以知道了.
Dala 2001-10-22
  • 打赏
  • 举报
回复
测试通过
void __fastcall TForm1::FormCreate(TObject *Sender)
{
PageControl1->MultiLine = true;
for (int i=0; i<3; i++)
{
TQuery *Query = new TQuery(this);
Query->Name = "Q" + AnsiString(i);
Query->DatabaseName = "BCDEMOS";
Query->SQL->Clear();
Query->SQL->Add("select * from country");
Query->Prepare();
Query->Open();

TDataSource *DS = new TDataSource(this);
DS->Name = "DS" + AnsiString(i);
DS->DataSet = Query;

TTabSheet *Tab = new TTabSheet(PageControl1);
Tab->Name = "Tab" + AnsiString(i);
Tab->PageControl = PageControl1;
Tab->PageIndex = i;
Tab->Caption =Tab->Name;

TDBGrid *DBGrid;
DBGrid = new TDBGrid(Tab);
DBGrid->Name = "DBGrid"+IntToStr(i);
DBGrid->Parent = Tab;
// DBGrid->Align = alClient;
DBGrid->DataSource = DS;
DBGrid->Left = 20*i;
DBGrid->Visible = true;
}
}
//---------------------------------------------------------------------------
Chxis 2001-10-22
  • 打赏
  • 举报
回复
if (i==0)
Query->Active = true;
又想想,你在query0才打开query,那其他的呢?
Chxis 2001-10-22
  • 打赏
  • 举报
回复
我试了,
当程序运行到
if (i==0)
Query->Active = true;<<---
时就死了
Dala 2001-10-22
  • 打赏
  • 举报
回复
你把所有控件全部动态生成结束后再打开Query
书生 2001-10-22
  • 打赏
  • 举报
回复
to nononono
问题解决了别忘记通知我一声
lluunn_sina.com.cn

谢了。
nononono 2001-10-22
  • 打赏
  • 举报
回复
go top

没人试试么?

nononono 2001-10-21
  • 打赏
  • 举报
回复


等我去办公室的时候, 我把我的代码整理一下, 也贴上来, 大家讨论.
Chxis 2001-10-21
  • 打赏
  • 举报
回复
void __fastcall TForm1::FormCreate(TObject *Sender)
{
for(int i=0;i<2;i++)
{

TTabSheet *tab=new TTabSheet(PageControl1);
tab->PageControl=PageControl1;
tab->Parent=PageControl1;
TDBGrid *grid=new TDBGrid(tab);
grid->Parent=tab;
grid->Align=alClient;
if(i==1)grid->DataSource=DataSource1;
grid->DataSource=DataSource2;
}

}
(DataSource1指向table,DataSource2指向一个Query)


没有这个现象啊,
nononono 2001-10-21
  • 打赏
  • 举报
回复
类似Query1->Open();的语句我已经在Form的OnCreate事件中执行, 不应该再次"关闭-打开", 因为这样会影响程序的运行效率, 我这个模块的SQL语句可能会查到极多的数据, 轻易不能重新打开.

加载更多回复(6)

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧