在一个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, 无效.
想知道原因以及正确的解决办法.
那位遇到这种情况并解决了?
26 个解决方案
#1
go top
#2
其实问题已经绕开, 但想知道最直接的办法.
没人知道原因和正确的解决办法吗?
加到300分.
没人知道原因和正确的解决办法吗?
加到300分.
#3
我有过类似的遭遇,就是我用程序增加了几条纪录,但dbgrid并不立即显示,恭喜你,你还绕开了,我现在都没有解决。
#4
go top
#5
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)
没有这个现象啊,
{
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)
没有这个现象啊,
#6
哦
等我去办公室的时候, 我把我的代码整理一下, 也贴上来, 大家讨论.
等我去办公室的时候, 我把我的代码整理一下, 也贴上来, 大家讨论.
#7
我试过没发现问题。
#8
Query1->Close();
Query1->Open();
放在合适的地方!
Query1->Open();
放在合适的地方!
#9
类似Query1->Open();的语句我已经在Form的OnCreate事件中执行, 不应该再次"关闭-打开", 因为这样会影响程序的运行效率, 我这个模块的SQL语句可能会查到极多的数据, 轻易不能重新打开.
#10
nononono
你说的问题我前不久遇到过,当时也没想清楚怎么回事,我采用了绕过问题方法自欺欺人了,和你一样,置了两次焦点就好了。现在也没细想原因。关注。
你说的问题我前不久遇到过,当时也没想清楚怎么回事,我采用了绕过问题方法自欺欺人了,和你一样,置了两次焦点就好了。现在也没细想原因。关注。
#11
内存不足吧
#12
内存不足? 怎么会?!
#13
原代码来了。
这是我已经清理了与此内容无关的代码后的文件内容,可以运行并可以体现该问题。有心者可以试试。
*****************************************************************************
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);
}
//---------------------------------------------------------------------------
这是我已经清理了与此内容无关的代码后的文件内容,可以运行并可以体现该问题。有心者可以试试。
*****************************************************************************
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);
}
//---------------------------------------------------------------------------
#14
go top
没人试试么?
没人试试么?
#15
to nononono
问题解决了别忘记通知我一声
lluunn_sina.com.cn
谢了。
问题解决了别忘记通知我一声
lluunn_sina.com.cn
谢了。
#16
你把所有控件全部动态生成结束后再打开Query
#17
我试了,
当程序运行到
if (i==0)
Query->Active = true;<<---
时就死了
当程序运行到
if (i==0)
Query->Active = true;<<---
时就死了
#18
if (i==0)
Query->Active = true;
又想想,你在query0才打开query,那其他的呢?
Query->Active = true;
又想想,你在query0才打开query,那其他的呢?
#19
测试通过
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;
}
}
//---------------------------------------------------------------------------
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;
}
}
//---------------------------------------------------------------------------
#20
我知道原因了,
原来是当动态创建两个TabSheet时,
就算你 this->PageControl1->ActivePageIndex = 0;
但是显示的还是最后一个Tabsheet,
你只要试试把
if (i==0) ///把这个去掉,
Query->Open();
就可以知道了.
原来是当动态创建两个TabSheet时,
就算你 this->PageControl1->ActivePageIndex = 0;
但是显示的还是最后一个Tabsheet,
你只要试试把
if (i==0) ///把这个去掉,
Query->Open();
就可以知道了.
#21
所以当你点击几次tabsheet时就显示回应该显示的tabsheet的内容
#22
随之,相应DBGRID的内容也出来了
#23
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天内结贴.
多谢参与.
***************************************************************************
多谢 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天内结贴.
多谢参与.
#24
可以
只要将
TabSheet->Visible = true;
这句删去,
那么
//----------------------------------------
/* 注意:如果取消下面的 2行,将看不到第一页的数据!!! */
if (NumberOfQuerys>1)
this->PageControl1->ActivePageIndex = 1;
//----------------------------------------
this->PageControl1->ActivePageIndex = 0;
这些都不用要了,
奇怪啊...~~
只要将
TabSheet->Visible = true;
这句删去,
那么
//----------------------------------------
/* 注意:如果取消下面的 2行,将看不到第一页的数据!!! */
if (NumberOfQuerys>1)
this->PageControl1->ActivePageIndex = 1;
//----------------------------------------
this->PageControl1->ActivePageIndex = 0;
这些都不用要了,
奇怪啊...~~
#25
"只要将 TabSheet->Visible = true; 这句删去"?
一想是有道理! 因为TabSheet->Visible的属性本来就是没用的(或者说我没觉得它有用)!
明天试过结贴.
一想是有道理! 因为TabSheet->Visible的属性本来就是没用的(或者说我没觉得它有用)!
明天试过结贴.
#26
总结:
确实如Chxis(明月夜,古松冈.)所说,去掉"TabSheet->Visible = true; "即可。
想起来这个TTabSheet的确与其它控件有所不同,别的控件需要设定Parent,而它设定的是PageControl。另外,TTabSheet也不能通过改变Visible属性来达到隐藏页的目的。到现在我还不知道怎么能够隐藏页。
本帖结束。
确实如Chxis(明月夜,古松冈.)所说,去掉"TabSheet->Visible = true; "即可。
想起来这个TTabSheet的确与其它控件有所不同,别的控件需要设定Parent,而它设定的是PageControl。另外,TTabSheet也不能通过改变Visible属性来达到隐藏页的目的。到现在我还不知道怎么能够隐藏页。
本帖结束。
#1
go top
#2
其实问题已经绕开, 但想知道最直接的办法.
没人知道原因和正确的解决办法吗?
加到300分.
没人知道原因和正确的解决办法吗?
加到300分.
#3
我有过类似的遭遇,就是我用程序增加了几条纪录,但dbgrid并不立即显示,恭喜你,你还绕开了,我现在都没有解决。
#4
go top
#5
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)
没有这个现象啊,
{
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)
没有这个现象啊,
#6
哦
等我去办公室的时候, 我把我的代码整理一下, 也贴上来, 大家讨论.
等我去办公室的时候, 我把我的代码整理一下, 也贴上来, 大家讨论.
#7
我试过没发现问题。
#8
Query1->Close();
Query1->Open();
放在合适的地方!
Query1->Open();
放在合适的地方!
#9
类似Query1->Open();的语句我已经在Form的OnCreate事件中执行, 不应该再次"关闭-打开", 因为这样会影响程序的运行效率, 我这个模块的SQL语句可能会查到极多的数据, 轻易不能重新打开.
#10
nononono
你说的问题我前不久遇到过,当时也没想清楚怎么回事,我采用了绕过问题方法自欺欺人了,和你一样,置了两次焦点就好了。现在也没细想原因。关注。
你说的问题我前不久遇到过,当时也没想清楚怎么回事,我采用了绕过问题方法自欺欺人了,和你一样,置了两次焦点就好了。现在也没细想原因。关注。
#11
内存不足吧
#12
内存不足? 怎么会?!
#13
原代码来了。
这是我已经清理了与此内容无关的代码后的文件内容,可以运行并可以体现该问题。有心者可以试试。
*****************************************************************************
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);
}
//---------------------------------------------------------------------------
这是我已经清理了与此内容无关的代码后的文件内容,可以运行并可以体现该问题。有心者可以试试。
*****************************************************************************
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);
}
//---------------------------------------------------------------------------
#14
go top
没人试试么?
没人试试么?
#15
to nononono
问题解决了别忘记通知我一声
lluunn_sina.com.cn
谢了。
问题解决了别忘记通知我一声
lluunn_sina.com.cn
谢了。
#16
你把所有控件全部动态生成结束后再打开Query
#17
我试了,
当程序运行到
if (i==0)
Query->Active = true;<<---
时就死了
当程序运行到
if (i==0)
Query->Active = true;<<---
时就死了
#18
if (i==0)
Query->Active = true;
又想想,你在query0才打开query,那其他的呢?
Query->Active = true;
又想想,你在query0才打开query,那其他的呢?
#19
测试通过
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;
}
}
//---------------------------------------------------------------------------
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;
}
}
//---------------------------------------------------------------------------
#20
我知道原因了,
原来是当动态创建两个TabSheet时,
就算你 this->PageControl1->ActivePageIndex = 0;
但是显示的还是最后一个Tabsheet,
你只要试试把
if (i==0) ///把这个去掉,
Query->Open();
就可以知道了.
原来是当动态创建两个TabSheet时,
就算你 this->PageControl1->ActivePageIndex = 0;
但是显示的还是最后一个Tabsheet,
你只要试试把
if (i==0) ///把这个去掉,
Query->Open();
就可以知道了.
#21
所以当你点击几次tabsheet时就显示回应该显示的tabsheet的内容
#22
随之,相应DBGRID的内容也出来了
#23
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天内结贴.
多谢参与.
***************************************************************************
多谢 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天内结贴.
多谢参与.
#24
可以
只要将
TabSheet->Visible = true;
这句删去,
那么
//----------------------------------------
/* 注意:如果取消下面的 2行,将看不到第一页的数据!!! */
if (NumberOfQuerys>1)
this->PageControl1->ActivePageIndex = 1;
//----------------------------------------
this->PageControl1->ActivePageIndex = 0;
这些都不用要了,
奇怪啊...~~
只要将
TabSheet->Visible = true;
这句删去,
那么
//----------------------------------------
/* 注意:如果取消下面的 2行,将看不到第一页的数据!!! */
if (NumberOfQuerys>1)
this->PageControl1->ActivePageIndex = 1;
//----------------------------------------
this->PageControl1->ActivePageIndex = 0;
这些都不用要了,
奇怪啊...~~
#25
"只要将 TabSheet->Visible = true; 这句删去"?
一想是有道理! 因为TabSheet->Visible的属性本来就是没用的(或者说我没觉得它有用)!
明天试过结贴.
一想是有道理! 因为TabSheet->Visible的属性本来就是没用的(或者说我没觉得它有用)!
明天试过结贴.
#26
总结:
确实如Chxis(明月夜,古松冈.)所说,去掉"TabSheet->Visible = true; "即可。
想起来这个TTabSheet的确与其它控件有所不同,别的控件需要设定Parent,而它设定的是PageControl。另外,TTabSheet也不能通过改变Visible属性来达到隐藏页的目的。到现在我还不知道怎么能够隐藏页。
本帖结束。
确实如Chxis(明月夜,古松冈.)所说,去掉"TabSheet->Visible = true; "即可。
想起来这个TTabSheet的确与其它控件有所不同,别的控件需要设定Parent,而它设定的是PageControl。另外,TTabSheet也不能通过改变Visible属性来达到隐藏页的目的。到现在我还不知道怎么能够隐藏页。
本帖结束。