C++ Builder之StringGrid表格简单示例(TStringGrid控件实例例子)


程序运行截图如下:

每次点击可以生成不同的成绩表。

主要源代码如下:

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

#include 
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
AnsiString columns[]={"姓名","语文","数学","英语","计算机"};
AnsiString names[]={"王小明","李小刚","陈大海","林小芳","张小静","杨光"};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    this->Caption="StringGrid表格简单示例";

    StringGrid1->ColCount=sizeof(columns)/sizeof(AnsiString);
    StringGrid1->RowCount=1+sizeof(names)/sizeof(AnsiString);

    for(int x=0;xColCount;++x)
    {
        StringGrid1->Cells[x][0]=columns[x];
    }
    for(int y=1;yRowCount;++y)
    {
        StringGrid1->Cells[0][y]=names[y-1];
    }
    
    srand((unsigned)time(NULL));
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    for(int x=1;xColCount;++x)
    {
        for(int y=1;yRowCount;++y)
        {
            int score=60+rand()%41;
            StringGrid1->Cells[x][y]=IntToStr(score);
        }
    }
}
//---------------------------------------------------------------------------


相关