aardio连接mysql_aardio中call函数的一种使用

本文介绍了如何在aardio环境中通过定义一个`main`函数结合`call`函数,来减少重复代码,提高代码复用性。在处理多个按钮事件时,通过调用`main`并传入不同功能函数,实现了在点击每个按钮时自动执行前置、功能和后置函数的功能。示例代码展示了具体实现方式,并通过对话框显示结果验证了方法的有效性。
摘要由CSDN通过智能技术生成

问题描述

有三个按钮,分别对应了三种功能,用func1()、func2()和func2()表示。

在响应按钮时,要先运行前置函数pre(),再运行按钮对应的函数func(),最后运行后置函数suf()。如下所示:

mainForm.button1.oncommand = function(id,event){

pre();

func1();

suf();

}

mainForm.button2.oncommand = function(id,event){

pre();

func2();

suf();

}

mainForm.button3.oncommand = function(id,event){

pre();

func2();

suf();

}

这样就会将pre()和suf()写三遍.

一种解决方法

新定义一个函数

function main(func){

pre();

call(func);

suf();

}

按钮响应函数则为:

mainForm.button1.oncommand = function(id,event){

main(func1);

}

mainForm.button2.oncommand = function(id,event){

main(func2);

}

mainForm.button3.oncommand = function(id,event){

main(func3);

}

试验和结果

为了试验方便,将所有函数都设定为对话框,代码如下:

import win.ui;

/*DSG{{*/

mainForm = win.form(text="窗口试验";right=447;bottom=447)

mainForm.add(

button1={cls="button";text="Button1";left=152;top=64;right=272;bottom=120;z=1};

button2={cls="button";text="Button2";left=152;top=160;right=272;bottom=216;z=2};

button3={cls="button";text="Button3";left=152;top=256;right=272;bottom=312;z=3}

)

/*}}*/

function main(func){

pre();

call(func);

suf();

}

function pre(){

mainForm.msgbox("前置函数")

}

function suf(){

mainForm.msgbox("后置函数")

}

function func1(){

mainForm.msgbox("按钮1")

}

function func2(){

mainForm.msgbox("按钮2")

}

function func3(){

mainForm.msgbox("按钮3")

}

mainForm.button1.oncommand = function(id,event){

main(func1);

}

mainForm.button2.oncommand = function(id,event){

main(func2);

}

mainForm.button3.oncommand = function(id,event){

main(func3);

}

mainForm.show();

return win.loopMessage();

实际效果如图:

effff310b389c44939d46e8f4fa96aa5.gif

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值