Delphi的with…do语句的用法


本文整理自网络,侵删。

  with…do语句的用法

with…do语句用来指定一个块中的字段(记录的或者对象的)、属性和方法所属的记录或者对象。Delphi初学者可能已经习惯了如下的代码书写格式:

var

Button: TButton;

begin

Button := TButton.Create(Self);

Button.Parent := Self;

Button.Left := 50;

Button.Top := 50;

……

end;

每行都写一个Button是否让你感觉厌烦。Object Pascal提供它特有的with…do语句,可以帮你消除这个烦恼。上面的代码可以写为:



var

Button: TButton;

begin

Button := TButton.Create(Self);

with Button do {编译器会知道下面的三个属性属于Button}

begin

Parent := Self;

Left := 50;

Top := 50;

……

end;

end;

with…do中可以包含多个记录或者对象,用逗号隔开。with…do也可以嵌套使用。

相关阅读 >>

Delphi读写utf-8、unicode格式文本文件

Delphi计算两个时间差

Delphi 16进制字符串转原字符串

Delphi 解析json生成json

Delphi 鼠标跟随代码

Delphi xe5的新功能“ tlistview内置搜索过滤”

Delphi xe5读取android imei id

Delphi now 返回当前日期及时间

Delphi 中拖动无边框窗口的5种方法

Delphi中组件panel、splitter、groupbox、按钮组件(checkbox...)、计时器、滚动条、多选卡

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...