Rotu
06-23-2011, 03:16 PM
I’ve been creating a lot of tools for the animators here, and I’ve been using more and more DotNet to make the UIs more versatile. I decided to try to get some re-use in building these dotnet UI’s, but it’s causing some problems that I just don’t understand.
I keep getting “-- Unable to convert: dotNetObject:System.Windows.Forms.Button to type: System.Windows.Forms.Control” and other similar errors. But here’s the awesome part – only sometimes. On my larger script, it’ll happen sometimes 5 times in a row, then randomly run without error! The following test case runs a little more frequently, but it is causing the same error maybe 1 in 5 times.
I think it has something to do with the variables holding references to the values instead of the values themselves? How do I force these variables to hold the correct values? Why is it randomly working and/or not working, but not consistently doing either?
Here’s a pared-down version of the script that’s causing me problems.
struct zmDotNetUITest (
fn zmAdjTableLayout t colStyles rowStyles =
(
t.columnCount = colStyles.count
t.rowCount = rowStyles.count
--
for i = 1 to colStyles.count do (
t.columnStyles.add (dotNetObject "system.windows.forms.columnstyle")
case colStyles[i][1] of (
#absolute: t.columnStyles.item[(i-1)].sizetype = t.columnStyles.item[(i-1)].sizetype.absolute
#percent: t.columnStyles.item[(i-1)].sizetype = t.columnStyles.item[(i-1)].sizetype.percent
#AutoSize: t.columnStyles.item[(i-1)].sizetype = t.columnStyles.item[(i-1)].sizetype.AutoSize
)
t.columnstyles.item[(i-1)].width = colStyles[i][2]
)
for i = 1 to rowStyles.count do (
t.rowStyles.add (dotNetObject "system.windows.forms.rowstyle")
case rowStyles[i][1] of (
#absolute: t.rowStyles.item[(i-1)].sizetype = t.rowstyles.item[(i-1)].sizetype.absolute
#percent: t.rowStyles.item[(i-1)].sizetype = t.rowstyles.item[(i-1)].sizetype.percent
#AutoSize: t.rowStyles.item[(i-1)].sizetype = t.rowstyles.item[(i-1)].sizetype.AutoSize
)
t.rowStyles.item[(i-1)].height = rowStyles[i][2]
)
),
fn zmDnTableLayout n parent:undefined col:1 row:1 pad:0 border:false =
(
local tabLay = dotnetobject "System.Windows.Forms.TableLayoutPanel"
if border then (
tabLay.cellBorderStyle = tabLay.cellBorderStyle.outset
)
tabLay.columnCount = col
tabLay.rowCount = row
tabLay.dock = tabLay.dock.fill
tabLay.Margin = (dotNetObject "system.windows.forms.padding" pad)
tabLay.padding = (dotNetObject "system.windows.forms.padding" pad)
tabLay
),
fn zmdn_createButton t w h evt tag:undefined =
(
local btn = dotnetobject "System.Windows.Forms.button"
btn.text = t
if (tag!=undefined) then btn.tag = tag
btn.padding = btn.margin = (dotNetObject "system.windows.forms.padding" 2)
if (w==0) then (
btn.dock = btn.dock.fill
) else (
btn.width = w
btn.height = h
)
dotNet.addEventHandler btn evt[1] evt[2]
dotNet.setLifetimeControl btn #dotnet
btn
),
fn zmdnui_form t w h =
(
dotnet.loadassembly "System.Data"
local newFrm = dotnetobject "MaxCustomControls.Maxform"
newFrm.size = dotnetobject "System.Drawing.Size" w h
newFrm.formborderStyle = newFrm.formborderStyle.Sizable
newFrm.text = t
--dotNet.setLifetimeControl newFrm #dotnet
newFrm
)
)
global zmDotNetUITest = zmDotNetUITest()
-- ------------------------------------------------------------------------------------------------
try (zmdnTest.form.close()) catch()
struct zmdnTest (
form,
maxbut,
xafbut,
otherbut,
fn zmpg_btn_evt =
(
format "yo\n"
),
fn initUI =
(
zmdnTest.form = zmDotNetUITest.zmdnui_form "animPropagate" 800 400
local mainlay = zmDotNetUITest.zmDnTableLayout "" col:4 row:1 pad:0 border:false
zmDotNetUITest.zmAdjTableLayout mainlay #(#(#absolute,150),#(#absolute,150),#(#absolute,150),#(#percent,100)) #(#(#percent,100))
local laySrc = zmDotNetUI.zmDnTableLayout "" col:1 row:3 pad:0 border:false
zmDotNetUI.zmAdjTableLayout laySrc #(#(#percent,100)) #(#(#percent,100),#(#percent,100))
zmdnTest.maxBut = zmDotNetUITest.zmdn_createButton "Max" 0 0 #("click",zmpg_btn_evt)
zmdnTest.xafBut = zmDotNetUITest.zmdn_createButton "XAF" 0 0 #("click",zmpg_btn_evt)
zmdnTest.otherbut = zmDotNetUITest.zmdn_createButton "Other" 0 0 #("click",zmpg_btn_evt)
mainlay.controls.addRange #(laySrc)
laySrc.controls.addRange #(zmdnTest.maxBut,zmdnTest.xafbut,zmdnTest.otherbut)
form.controls.addRange #(mainlay)
zmdnTest.form.showModeless()
)
)
global zmdnTest = zmdnTest()
zmdnTest.initUI()
It's mostly happening on the 7th line up from the bottom: "laySrc.controls.addRange ...".
Any help would be greatly appreciated.
…
rich
I keep getting “-- Unable to convert: dotNetObject:System.Windows.Forms.Button to type: System.Windows.Forms.Control” and other similar errors. But here’s the awesome part – only sometimes. On my larger script, it’ll happen sometimes 5 times in a row, then randomly run without error! The following test case runs a little more frequently, but it is causing the same error maybe 1 in 5 times.
I think it has something to do with the variables holding references to the values instead of the values themselves? How do I force these variables to hold the correct values? Why is it randomly working and/or not working, but not consistently doing either?
Here’s a pared-down version of the script that’s causing me problems.
struct zmDotNetUITest (
fn zmAdjTableLayout t colStyles rowStyles =
(
t.columnCount = colStyles.count
t.rowCount = rowStyles.count
--
for i = 1 to colStyles.count do (
t.columnStyles.add (dotNetObject "system.windows.forms.columnstyle")
case colStyles[i][1] of (
#absolute: t.columnStyles.item[(i-1)].sizetype = t.columnStyles.item[(i-1)].sizetype.absolute
#percent: t.columnStyles.item[(i-1)].sizetype = t.columnStyles.item[(i-1)].sizetype.percent
#AutoSize: t.columnStyles.item[(i-1)].sizetype = t.columnStyles.item[(i-1)].sizetype.AutoSize
)
t.columnstyles.item[(i-1)].width = colStyles[i][2]
)
for i = 1 to rowStyles.count do (
t.rowStyles.add (dotNetObject "system.windows.forms.rowstyle")
case rowStyles[i][1] of (
#absolute: t.rowStyles.item[(i-1)].sizetype = t.rowstyles.item[(i-1)].sizetype.absolute
#percent: t.rowStyles.item[(i-1)].sizetype = t.rowstyles.item[(i-1)].sizetype.percent
#AutoSize: t.rowStyles.item[(i-1)].sizetype = t.rowstyles.item[(i-1)].sizetype.AutoSize
)
t.rowStyles.item[(i-1)].height = rowStyles[i][2]
)
),
fn zmDnTableLayout n parent:undefined col:1 row:1 pad:0 border:false =
(
local tabLay = dotnetobject "System.Windows.Forms.TableLayoutPanel"
if border then (
tabLay.cellBorderStyle = tabLay.cellBorderStyle.outset
)
tabLay.columnCount = col
tabLay.rowCount = row
tabLay.dock = tabLay.dock.fill
tabLay.Margin = (dotNetObject "system.windows.forms.padding" pad)
tabLay.padding = (dotNetObject "system.windows.forms.padding" pad)
tabLay
),
fn zmdn_createButton t w h evt tag:undefined =
(
local btn = dotnetobject "System.Windows.Forms.button"
btn.text = t
if (tag!=undefined) then btn.tag = tag
btn.padding = btn.margin = (dotNetObject "system.windows.forms.padding" 2)
if (w==0) then (
btn.dock = btn.dock.fill
) else (
btn.width = w
btn.height = h
)
dotNet.addEventHandler btn evt[1] evt[2]
dotNet.setLifetimeControl btn #dotnet
btn
),
fn zmdnui_form t w h =
(
dotnet.loadassembly "System.Data"
local newFrm = dotnetobject "MaxCustomControls.Maxform"
newFrm.size = dotnetobject "System.Drawing.Size" w h
newFrm.formborderStyle = newFrm.formborderStyle.Sizable
newFrm.text = t
--dotNet.setLifetimeControl newFrm #dotnet
newFrm
)
)
global zmDotNetUITest = zmDotNetUITest()
-- ------------------------------------------------------------------------------------------------
try (zmdnTest.form.close()) catch()
struct zmdnTest (
form,
maxbut,
xafbut,
otherbut,
fn zmpg_btn_evt =
(
format "yo\n"
),
fn initUI =
(
zmdnTest.form = zmDotNetUITest.zmdnui_form "animPropagate" 800 400
local mainlay = zmDotNetUITest.zmDnTableLayout "" col:4 row:1 pad:0 border:false
zmDotNetUITest.zmAdjTableLayout mainlay #(#(#absolute,150),#(#absolute,150),#(#absolute,150),#(#percent,100)) #(#(#percent,100))
local laySrc = zmDotNetUI.zmDnTableLayout "" col:1 row:3 pad:0 border:false
zmDotNetUI.zmAdjTableLayout laySrc #(#(#percent,100)) #(#(#percent,100),#(#percent,100))
zmdnTest.maxBut = zmDotNetUITest.zmdn_createButton "Max" 0 0 #("click",zmpg_btn_evt)
zmdnTest.xafBut = zmDotNetUITest.zmdn_createButton "XAF" 0 0 #("click",zmpg_btn_evt)
zmdnTest.otherbut = zmDotNetUITest.zmdn_createButton "Other" 0 0 #("click",zmpg_btn_evt)
mainlay.controls.addRange #(laySrc)
laySrc.controls.addRange #(zmdnTest.maxBut,zmdnTest.xafbut,zmdnTest.otherbut)
form.controls.addRange #(mainlay)
zmdnTest.form.showModeless()
)
)
global zmdnTest = zmdnTest()
zmdnTest.initUI()
It's mostly happening on the 7th line up from the bottom: "laySrc.controls.addRange ...".
Any help would be greatly appreciated.
…
rich
