求助贴

风语空音 2017-12-09 06:09:07
用C++builder写了个小程序,能运行但是功能没实现,功能是:当选择Combox1里某一项时,按查询按钮表会出现相应的数据,但是现在按查询按钮没出现数据,求解答。
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------



void __fastcall TForm1::FormCreate(TObject *Sender)
{ ComboBox1->Items->Add("所有借阅记录");
ComboBox1->Items->Add("今日借阅记录");
ComboBox1->Items->Add("今日到期记录");
ComboBox1->Items->Add("已经过期记录");

}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{ AnsiString a="资料编号",b="好好学习",c="天天向上",d="你好";
AnsiString e;
StringGrid1->Align=alTop;
StringGrid1->RowCount=5;
StringGrid1->ColCount=5;
//StringGrid1->FixedRows=1;
//StringGrid1->FixedCols=1;
StringGrid1->FixedColor=clGray;

for(int i=1;i<5;i++)
{
StringGrid1->Cells[i][0]=IntToStr(i);
StringGrid1->Cells[0][i]=IntToStr(i);
}
if(ComboBox1->SelText=="所有借阅记录")
{e=a;}
else if(ComboBox1->SelText=="今日借阅记录")
{e=b;}
else if(ComboBox1->SelText=="今日到期记录")
{e=c;}
else if (ComboBox1->SelText=="已经过期记录")
{e=d;}
for(int i=2;i<6;i++)
{
for(int j=2;j<6;j++)
{
StringGrid1->Cells[i][j]=e;
}
}
}
//---------------------------------------------------------------------------

...全文
396 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
风语空音 2017-12-11
  • 打赏
  • 举报
回复
好的,有空我上网找找
ooolinux 2017-12-10
  • 打赏
  • 举报
回复
这个属于进程间通信了,可以使用 内存映射文件,或者 管道。 具体的我也不会,你可以百度。 《Windows核心编程》可能有讲, 《C++ Builder经典范例50讲》里面也有例子。
风语空音 2017-12-10
  • 打赏
  • 举报
回复
大佬,请教一下如果我写了两个工程,两个工程里各有一个Form, 如何把Form1的一个值传给Form2呢?这个问题困扰我好长时间了,如果有具体例子就更感谢了!
ooolinux 2017-12-09
  • 打赏
  • 举报
回复
AnsiString a[]={"资料编号","好好学习","天天向上","你好"}; 最好改成: const AnsiString a[]={"资料编号","好好学习","天天向上","你好"}; 这一句可以改放到头文件里:
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Grids.hpp>

const AnsiString  a[]={"资料编号","好好学习","天天向上","你好"};

//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
    TComboBox *ComboBox1;
    TButton *Button1;
    TStringGrid *StringGrid1;
    void __fastcall Button1Click(TObject *Sender);
private:	// User declarations
public:		// User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
ooolinux 2017-12-09
  • 打赏
  • 举报
回复
改进版:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString  a[]={"资料编号","好好学习","天天向上","你好"};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    StringGrid1->RowCount=5;
    StringGrid1->ColCount=5;
    //StringGrid1->FixedRows=1;
    //StringGrid1->FixedCols=1;
    StringGrid1->FixedColor=clGray;
 
    for(int i=1;i<5;i++)
    {
        StringGrid1->Cells[i][0]=IntToStr(i);
        StringGrid1->Cells[0][i]=IntToStr(i);
    }

    ComboBox1->Items->Add("所有借阅记录");
    ComboBox1->Items->Add("今日借阅记录");
    ComboBox1->Items->Add("今日到期记录");
    ComboBox1->Items->Add("已经过期记录");
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    int i=ComboBox1->ItemIndex;
    Caption=IntToStr(i); //测试用
    if(i<0) //i==-1
    {
        ShowMessage("下拉列表请选择一项");
    }
    else
    {
        AnsiString s;
        if(i<sizeof(a)/sizeof(AnsiString)) //确保下拉列表选项不超过数组a的项目
            s=a[i];                        //避免数组越界
        else
            s="未定义";

        for(int x=1;x<5;x++)
            for(int y=1;y<5;y++)
		//Cells[列][行],因为列为x,行为y,与二维数组不同
		//比如MouseToCell(X, Y, Column, Row)
                StringGrid1->Cells[x][y]=s;
    }
}
//---------------------------------------------------------------------------
ooolinux 2017-12-09
  • 打赏
  • 举报
回复
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString  a[]={"资料编号","好好学习","天天向上","你好"};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    StringGrid1->RowCount=5;
    StringGrid1->ColCount=5;
    //StringGrid1->FixedRows=1;
    //StringGrid1->FixedCols=1;
    StringGrid1->FixedColor=clGray;
 
    for(int i=1;i<5;i++)
    {
        StringGrid1->Cells[i][0]=IntToStr(i);
        StringGrid1->Cells[0][i]=IntToStr(i);
    }

    ComboBox1->Items->Add("所有借阅记录");
    ComboBox1->Items->Add("今日借阅记录");
    ComboBox1->Items->Add("今日到期记录");
    ComboBox1->Items->Add("已经过期记录");
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    int i=ComboBox1->ItemIndex;
    Caption=IntToStr(i); //测试用
    if(i<0) //i==-1
    {
        ShowMessage("下拉列表请选择一项");
    }
    else
    {
        AnsiString s=a[i];
        for(int x=1;x<5;x++)
            for(int y=1;y<5;y++)
		//Cells[列][行],因为列为x,行为y,与二维数组不同
		//比如MouseToCell(X, Y, Column, Row)
                StringGrid1->Cells[x][y]=s;
    }
}
//---------------------------------------------------------------------------
初始化(只执行一次)的代码一般都可以放在构造函数里,有的可以在OnCreate里。 for(int i=2;i<6;i++) 是错的,数组 a[n] 完整下标都是从0 ~ n-1,而不是1~n

13,824

社区成员

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

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