|
在做一个串口操作的小工具,需要用到多个下拉框,而且这些下拉框都完成相同的事件响应,每个控件写一个相同的响应事件感觉有些傻,特向大家请教看是否有更简洁的写法,代码如下:
- import win.ui;
- /*DSG{{*/
- var winform = win.form(text="端口配置";right=174;bottom=263)
- winform.add(
- button={cls="button";text="OK";left=9;top=225;right=55;bottom=251;z=9};
- button2={cls="button";text="Cancel";left=65;top=225;right=112;bottom=251;z=10};
- button3={cls="button";text="Clear";left=123;top=225;right=170;bottom=251;z=11};
- static={cls="static";text="端口0";left=17;top=9;right=67;bottom=32;font=LOGFONT(h=-16);transparent=1;z=1};
- static2={cls="static";text="端口1";left=17;top=36;right=67;bottom=59;font=LOGFONT(h=-16);transparent=1;z=2};
- static3={cls="static";text="端口2";left=17;top=63;right=67;bottom=86;font=LOGFONT(h=-16);transparent=1;z=3};
- static4={cls="static";text="端口3";left=17;top=90;right=67;bottom=113;font=LOGFONT(h=-16);transparent=1;z=4};
- static5={cls="static";text="端口4";left=17;top=117;right=67;bottom=140;font=LOGFONT(h=-16);transparent=1;z=5};
- static6={cls="static";text="端口5";left=17;top=144;right=67;bottom=167;font=LOGFONT(h=-16);transparent=1;z=6};
- static7={cls="static";text="端口6";left=17;top=170;right=67;bottom=193;font=LOGFONT(h=-16);transparent=1;z=7};
- static8={cls="static";text="端口7";left=17;top=197;right=67;bottom=220;font=LOGFONT(h=-16);transparent=1;z=8}
- )
- /*}}*/
- //批量添加下拉框控件
- var comboboxCtrls = winform.add({
- [1]={cls="combobox";left=73;top=9;right=154;bottom=29;edge=1;group=1;items={};mode="dropdown";z=2};
- [2]={cls="combobox";left=73;top=36;right=154;bottom=56;edge=1;group=1;items={};mode="dropdown";z=4};
- [3]={cls="combobox";left=73;top=63;right=154;bottom=83;edge=1;group=1;items={};mode="dropdown";z=6};
- [4]={cls="combobox";left=73;top=90;right=154;bottom=110;edge=1;group=1;items={};mode="dropdown";z=8};
- [5]={cls="combobox";left=73;top=117;right=154;bottom=137;edge=1;group=1;items={};mode="dropdown";z=10};
- [6]={cls="combobox";left=73;top=144;right=154;bottom=164;edge=1;group=1;items={};mode="dropdown";z=12};
- [7]={cls="combobox";left=73;top=170;right=154;bottom=190;edge=1;group=1;items={};mode="dropdown";z=14};
- [8]={cls="combobox";left=73;top=197;right=154;bottom=217;edge=1;group=1;items={};mode="dropdown";z=16};
- })
- eachComRead = function(){
- import win.reg;
- var b = {};
- //获取电脑中串口号
- var reg = win.regReader("HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM");
- if( reg ){
- for(name,value,t in reg.eachValue()){
- table.push(b,value);
- }
- var i = 0;
- return function(){
- i = i + 1;
- return b[ i ];
- };
- }else {
- return null;
- }
- }
- winform.button2.oncommand = function(id,event){
- winform.close()
- }
- winform.button3.oncommand = function(id,event){
- for(i=1;8;1){
- comboboxCtrls[ i ].text = ""
- }
- }
- //想所有的combobox在同一个事件里做处理
- comboboxCtrls[1].oncommand = function(id,event){
- win.msgbox(id,event)
- }
- init = function(){
- var comTable = eachComRead()
- if(comTable){
- for com in comTable{
- for(i=1;8;1){
- comboboxCtrls[ i ].add(com)
- }
- }
- }
- }
- winform.show()
- init()
- win.loopMessage();
复制代码 |
|