Delphi StringGrid常用属性和常用操作

StringGrid组件用于建立显示字符串的网格,与电子表格相似。它可使表格中的字符串和相关对象操作简单化。StringGrid组件提供了许多可控制网格外观念的属性,以及利用表格的结构响应用户操作的事件和方法。StringGrid具有关联对象与网格中的每个字符串的作用,这些对象为用户封装了字符串表示的信息或行为。

一、Delphi StringGrid控件的属性及使用说明

1、固定行及固定列: (没有固定行列的时候,不能在运行时调节各个单元格的宽度和高度)

StringGrid.FixedCols := 固定行数;

StringGrid.FixedRows := 固定列数;

StringGrid.FixedColor := 固定行列的颜色;

StringGrid.Color := 未固定行列的颜色(资料区);

2、资料区行列的宽度和高度:

StringGrid.DefaultColWidth := 內定全部的宽度;

StringGrid.DefaultRowHeight := 內定全部的高度;

StringGrid.ColWidths[Index:Longint]:=某一行整行的宽度;

StringGrid.RowHeights[Index:Longint]:=某一列整列之高度;

3、资料区(CELL)指定(将某一行列停在资料区最左上角)

StringGrid.LeftCol:=某一行號;

StringGrid.TopRow:=某一列號;

4、焦点移动到某一单元格内:

StringGrid.Row:=?;

StringGrid.Col:=?;

5、设定资料区行数和列数:(包含固定行、列亦算在內)

StringGrid.RowCount:=?;

StringGrid.ColCount:=?;

6、给某一个单元格赋值(从0开始):

StringGrid.Cells[Col值 , Row值]:=字串;

7、判断鼠标处于哪一个单元格中

在StringGrid的Mouse事件中(UP,DOWN或MOVE)下:

VAR C , R : Longint;

StringGrid.MouseToCell(X,Y,C,R); {X,Y由MOUSE事件传入}

{取回 C , R 即为目前之Col , Row值 }

8、StringGrid之Options属性:

若要在执行中开启合租关闭Options某一功能如 ‘goTABS’,则可以如下:

开启: StringGrid.Options:= StringGrid.Options + [goTABS];

关闭: StringGrid.Options:= StringGrid.Options - [goTABS];

goFixedHorzLine 固定列之间的水平分割线

goFixedVertLine 固定行之间的垂直分割线

goHorzLine 可滚动列之间的水平分割线

goVertLine 可滚动行之间的垂直分割线

goRangeSelect 可多重选择单元,如果包含goEditing,则忽略goRangeSelect。

goDrawFocusSelected 用鼠标多重选择时,第一个选择的单元反白

goRowSizing 用鼠标可改变列高

goColSizing 用鼠标可改变行寬

goRowMoving 用鼠标可移动可滚动行

goColMoving 用鼠标可移动可滚动列

goEditing 可编辑单元的内容

goAlwaysShowEditor 表格总是编辑模式,不需要F2或ENTER即有等待输入的游标。如果Options不包含goEditing或包含goRowSelect,则goAlwaysShowEditor无效。

goTabs 用TAB及Shift+TAB可切换表格单元

goRowSelect 用滑鼠點一下可選取整列(亦與滑鼠可多重選擇互斥)

goThumbTracking 捲軸動時GRID跟著動,否則捲軸動完放開,GRID才動

一、Delphi StringGrid控件的常用操作代码

// 初始化StirngGrid的首行和首列

procedure TReferContentForm.SetSGridTitle(SGrid: TSuiStringGrid);

var

ColIndex, RowIndex: integer;

begin

//画第一行(标题栏)

for colIndex := 1 to SGrid.ColCount do

begin

SGrid.Cells[colIndex, 0] := '列名' + Chr(ord('A') - 1 + colIndex);

end;

//画第一列(数字栏)

SGrid.ColWidths[0] := 30;

SGrid.Cells[0, 0] := '序列';

for RowIndex := 1 to SGrid.RowCount - 1 do

begin

SGrid.Cells[0, RowIndex] := IntToStr(RowIndex);

end;

end;

// 清楚StirngGrid,指定行数和列数

procedure TReferContentForm.ClearSGrid(SGrid: TSuiStringGrid; aRow, aCol: integer);

var

i: integer;

begin

SGrid.RowCount := aRow;

SGrid.ColCount := aCol;

for i := 0 to SGrid.RowCount - 1 do //如果不清表头则从1开始

SGrid.Rows[i].Clear;

setSGridTitle(SGrid);

end;

// 添加一行

procedure TReferContentForm.mmiN1Click(Sender: TObject);

var

SGrid: TsuiStringGrid;

begin

SGrid := TsuiStringGrid(suiPMSGrid.PopupComponent);

if SGrid <> nil then

begin

SGrid.RowCount := SGrid.RowCount + 1;

SetSGridTitle(SGrid);

end;

end;

// 插入一行

procedure TReferContentForm.mmiN2Click(Sender: TObject);

var

i, curRow: integer;

SGrid: TsuiStringGrid;

begin

SGrid := TsuiStringGrid(suiPMSGrid.PopupComponent);

if SGrid <> nil then

begin

curRow := SGrid.Row; //记录当前选定行的位置

SGrid.rowcount := SGrid.rowcount + 1;

for i := SGrid.rowcount - 1 downto curRow + 1 do

SGrid.Rows[i] := SGrid.Rows[i - 1];

SGrid.Rows[curRow].Clear;

SetSGridTitle(SGrid);

end;

end;

// 删除当前一行

procedure TReferContentForm.mmiN3Click(Sender: TObject);

var

i: integer;

SGrid: TsuiStringGrid;

begin

SGrid := TsuiStringGrid(suiPMSGrid.PopupComponent);

if SGrid <> nil then

begin

for i := SGrid.row to SGrid.RowCount - 1 do

SGrid.Rows[i] := SGrid.Rows[i + 1];

SGrid.RowCount := SGrid.RowCount - 1; //删除

SetSGridTitle(SGrid);

end;

end;

// 添加一列

procedure TReferContentForm.mmiN5Click(Sender: TObject);

var

SGrid: TsuiStringGrid;

begin

SGrid := TsuiStringGrid(suiPMSGrid.PopupComponent);

if SGrid <> nil then

SGrid.ColCount := SGrid.ColCount + 1;

SetSGridTitle(SGrid);

end;

// 插入一列

procedure TReferContentForm.mmiN6Click(Sender: TObject);

var

i, CurCol: integer;

SGrid: TsuiStringGrid;

begin

SGrid := TsuiStringGrid(suiPMSGrid.PopupComponent);

if SGrid <> nil then

begin

CurCol := SGrid.Col; //记录当前选定行的位置

SGrid.ColCount := SGrid.ColCount + 1;

for i := SGrid.ColCount - 1 downto CurCol + 1 do

SGrid.Cols[i] := SGrid.Cols[i - 1];

SGrid.Cols[CurCol].Clear;

end;

SetSGridTitle(SGrid);

end;

// 删除一列

procedure TReferContentForm.mmiN7Click(Sender: TObject);

var

i: integer;

SGrid: TsuiStringGrid;

begin

SGrid := TsuiStringGrid(suiPMSGrid.PopupComponent);

if SGrid <> nil then

begin

for i := SGrid.Col to SGrid.ColCount - 1 do

SGrid.Cols[i] := SGrid.Cols[i + 1];

SGrid.ColCount := SGrid.ColCount - 1; //删除

end;

SetSGridTitle(SGrid);

end;

// 清空

procedure TReferContentForm.mmiN9Click(Sender: TObject);

var

SGrid: TsuiStringGrid;

begin

SGrid := TsuiStringGrid(suiPMSGrid.PopupComponent);

if SGrid <> nil then

begin

ClearSGrid(SGrid, 5, 5)

end;

SetSGridTitle(SGrid);

end;

// 右击选择单元格

procedure TReferContentForm.suiStringGridContentMouseDown(Sender: TObject;

Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

begin

if (Button = mbRight) then

begin

SendMessage(SGridContent.Handle, WM_LBUTTONDOWN, 0, MAKELONG(x, y));

SendMessage(SGridContent.Handle, WM_LBUTTONUP, 0, MAKELONG(x, y));

end;

end;

procedure TReferContentForm.FormShow(Sender: TObject);

begin

SetSGridTitle(SGridContent);

end;

// 把标题栏和数字列中是内容,居中显现

procedure TReferContentForm.SGridContentDrawCell(Sender: TObject; ACol,

ARow: Integer; Rect: TRect; State: TGridDrawState);

var

s: string;

r: TRect;

begin

if (ACol = 0) or (ARow = 0) then

begin

with TSuiStringGrid(Sender) do

begin

Canvas.Brush.Color := $00F0DDCE;

Canvas.FillRect(Rect);

s := Cells[ACol, ARow];

r := Rect;

DrawText(Canvas.Handle, PChar(s), Length(s), r, DT_CENTER or DT_SINGLELINE or DT_VCENTER);

end

end

end;

(转载)Delphi StringGrid常用属性和常用操作的更多相关文章

  1. (转载)StringGrid常用属性和常用操作

    Delphi StringGrid常用属性和常用操作 StringGrid组件用于建立显示字符串的网格,与电子表格相似.它可使表格中的字符串和相关对象操作简单化.StringGrid组件提供了许多可控 ...

  2. Delphi StringGrid常用属性和常用操作

    StringGrid组件用于建立显示字符串的网格,与电子表格相似.它可使表格中的字符串和相关对象操作简单化.StringGrid组件提供了许多可控制网格外观念的属性,以及利用表格的结构响应用户操作的事 ...

  3. Delphi中TStringList类常用属性方法详解

    TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 先把要讨论的几个属性列出来: 1.CommaText 2.Delim ...

  4. Delphi 常用属性说明(超长)

    Delphi组件的常用事件Onclick——当单击时触发这个事件中的代码Onchange——当改变该组件内容时触发其中的代码Oncreate——当创建时触发这个事件中的代码Onclose——当关闭的时 ...

  5. JavaScript BOM-11-BOM的核心-window对象; window对象的控制,弹出窗口方法; 超时调用; 间歇调用; location对象常用属性; 位置操作--location.reaplace,location.reload(); BOM中的history对象; Screen对象及其常用属性; Navigator对象;

    JavaScript BOM 学习目标 1.掌握什么是BOM 2.掌握BOM的核心-window对象 3.掌握window对象的控制.弹出窗口方法 什么是bom BOM(browser object ...

  6. js如何操作表格(常用属性方法汇总)

    js如何操作表格(常用属性方法汇总) 一.总结 一句话总结: 二.表格相关的属性和方法 1.1 Table 对象集合 cells[] 返回包含表格中所有单元格的一个数组. 语法:tableObject ...

  7. (转载)常用的Mysql数据库操作语句大全

    打开CMD,进入数据库命令:mysql -hlocalhost -uroot -p 退出数据库:exit 用户管理: 1.新建用户: >CREATE USER name IDENTIFIED B ...

  8. HTML a标签、4个伪类、常用属性(下载)、锚链接(待扩展:邮件、电话、短信、GPS)

    HTML 超链接<a> 1.超链接可以是一个字.一个词.一组词.一幅图像,您可以点击这些内容来跳转到新的文档或者当前文档中的某个部分. 2.当您把鼠标指针移动到网页中的某个链接上时,箭头会 ...

  9. iOS导航控制器常用函数与navigationBar常用属性

    导航控制器常用函数触发时机 当视图控制器的View将要出现时触发 - (void)viewWillAppear:(BOOL)animated 当视图控制器的View已经出现时触发 - (void)vi ...

随机推荐

  1. spring4使用websocket

    看到spring4的介绍上说已经支持websocket了,尝试了一下之后各种坑,不如servlet简单,写篇文章来讲解一下自己遇到的坑. 环境:tomcat8+spring4.1.6+jdk8+ngi ...

  2. 成员方法的this指针

    1.我们知道成员方法中,有个隐式的this常量指针.考虑,Derived继承的成员方法中this指针的表面类型是什么?子类重写的虚方法中this指针的表面类型是什么? 2.Derived继承的方法,就 ...

  3. 分页存储过程--From:桌面备份 -> sql2005新功能.docx

    二.以下示例将返回行号为 50 到 60(含)的行,并以 OrderDate 排序. USE AdventureWorks; GO WITH OrderedOrders AS (SELECT Sale ...

  4. Java SE学习之printf 日期转换符

    本文是学习网络上的文章时的总结,感谢大家无私的分享. System.out.printf()方法能够对日期做处理输出. 相应列表 转换符 类型 举例 c 完整的日期和时间 Mon Feb 09 18: ...

  5. 分布式助手Zookeeper(四)

    分布式助手Zookeeper(四)博客分类: Zookeeper zookeeper配置同步zookeeper编程 Zookeeper是分布式环境下一个重要的组件,因为它能在分布式环境下,给我带来很多 ...

  6. [GIF] Shape Objects in GIF Loop Coder

    This lesson is a quick tour of the predefined shape objects in GIF Loop Coder. function onGLC(glc) { ...

  7. Google高级技巧—google Hack★★★★

    google hacking事实上并算不上什么新东西,当时并没有重视这样的技术,觉得webshell什么的,并无太大实际用途.google hacking事实上并非如此简单... 经常使用的googl ...

  8. 从源码角度深入分析log4j配置文件使用

    log4j在日常开发中经常使用,但有时候对 配置文件应该放到什么位置有疑惑.现在我们通过从代码的角度来看待这个问题, 看完后你也许会恍然大悟哦. 开始吧. Log4j的组成及架构: Log4j由三个重 ...

  9. 获取随机颜色js

    获取随机颜色方法一: function randomColor1() { var rand = Math.floor(Math.random() * 0xFFFFFF).toString(16); i ...

  10. iOS NSDatePicker

    1.NSDate类 1>NSDate是系统一个日期,时间类 2>就是返回当前的日期,时间 3>+(id)date; 4>返回未来secs秒后的日期,时间 5>+(id)d ...