如何将对象作为参数传递给 powerbuilder 中的函数
我正在使用 powerbuilder 9 .0.1 Build 7171 我想写一个通用的 函数来处理对象属性,如字体名称。我想改变字体 对象按钮,列表框的想法是我将传递 窗口名称或窗口句柄和函数将获取控制列表,如果 control.type 是列表框或按钮或图片按钮,它会更改其字体。
我不想为每个窗口都编写窗口函数,有吗 我可以操纵 Windows 的属性
FOR n = 1 到 UpperBound(Control[]) IF Control[n].TypeOf() = CommandButton!或 Control[n].TypeOf() = PictureButton!THEN set_button_name(this.Control[n]) 结束如果 下一步
在这种情况下,我必须在每个窗口的打开事件中硬编码此代码 使用 function() 来调用它
I am using powerbuilder 9 .0.1 Build 7171 I want to write a generalised
function to handle object properties like font name. I want to change font
of the object buttons, list box idea is I will just pass on
window name or window handle and function will get the control list and if
control.type is listbox or button or picture button it will change its font.
I don't want to write windows function for every window is there any
way where I can just manipulate properties of windows
FOR n = 1 to UpperBound(Control[])
IF Control[n].TypeOf() = CommandButton! OR Control[n].TypeOf() = PictureButton!THEN
set_button_name(this.Control[n])
End if
NEXT
In this case I have to hardcode this code in open event of every window
to call this by using function()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,您确实遇到了两个问题:
控件列表
您已经获得了对窗口的 Control 数组的隐式引用,这是一个很好的第一步,但可能会忽略选项卡和用户对象等对象中的嵌入式 Control 数组。像下面这样的函数(警告:未经测试,仅用于说明)将递归所有控件,并处理选项卡中的选项卡等情况。
类型转换
下一个问题是如何访问或操作对象的属性,其中编译器无法识别该属性对该对象类型的所有权。解决方案是转换为编译器能够识别属性关联的类型。您在上面的示例中看到了转换:
编译器无法识别 WindowObject 和我试图引用的 Control 数组之间的关联,因此我通过将 WindowObject 分配给 UserObject 类型的变量来将 WindowObject 转换为 UserObject。之后,我可以通过该变量引用 Control 数组。
您将执行与我类似的操作,可能在循环内执行 CHOOSE CASE TypeOf(),然后在每个 CASE 内转换为与类型匹配的特定变量并在那里操作属性。
我猜您希望有一个单行赋值选项,但 PowerScript 中没有。 (如果您在每个函数上调用该函数,则可以使用 DYNAMIC 关键字在 WindowObject 上调用该函数,但您有责任确保该函数在调用之前就存在。)我相信 有一种方法可以直接使用 PBNI 来做到这一点,但我无法帮助你,而且我强烈怀疑它的努力远远超过了它的价值。
祝你好运,
特里。
So, you've really got two issues:
List of Controls
You've got an implicit reference to the window's Control array, which is a good first step, but may neglect embedded Control arrays in objects like tabs and userobjects. A function like the following (warning: untested, for illustration only) will recurse through all your controls, and handle cases like tabs within tabs.
Casting
The next issue is how to access or manipulate an attribute of an object where the compiler doesn't recognize ownership of that attribute to that object's type. The solution is casting to a type where the compiler will recognize the attribute association. You saw casting in the sample above:
The compiler wouldn't have recognized the association between the WindowObject and the Control array I was trying to reference, so I cast the WindowObject to a UserObject by assigning the WindowObject to a variable of type UserObject. After that, I can reference the Control array via that variable.
You'll be doing something similar to mine, probably inside a loop doing a CHOOSE CASE TypeOf(), then inside each CASE casting to a specific variable matching the type and manipulating the attribute there.
I'm guessing you were hoping there was a one-line assignment option, but there's not in PowerScript. (If it was a function you were calling on each, you could use the DYNAMIC keyword calling the function on the WindowObject, but it would be your responsibility to ensure that function was there before you called it.) I believe there's a way to directly do this with PBNI, but I can't help you with that, and I strongly suspect it's far more effort than it's worth.
Good luck,
Terry.