[Closed] Use structure encapsulation .net controls's problem
I …want to encapsulation .net controls with Maxscript ‘s Struct .
In this example, i want to create a titlebar in a .net panel, and when i click this title bar then close the rollout.
this is the code:
struct TitleBarStruct
(
parent, -- parent panel
bar, -- titlebar
rol, -- the rollout
-- mouse click event handler
fn MouseClick s e= (
destroydialog rol
),
-- initialize function
fn init = (
-- create the title bar
bar = dotnetobject "panel"
bar.height = 20
bar.backColor = (dotnetclass "system.drawing.Color").fromargb 50 150 150
-- add event MouseClick to the bar
dotnet.addeventhandler bar "mouseclick" MouseClick
-- add the bar to parent panel
parent.controls.add bar
),
-- initilaize
tmp = init()
)
rollout testRollout "Untitled" width:203 height:105
(
dotnetcontrol panel "panel" width:300 height: 300
on testRollout open do (
-- instance a TitleBarStruct to create a title bar
k = TitleBarStruct parent:panel rol:testRollout
)
)
createdialog testRollout
The problem is when i click the title bar will show this ERROR:
– Error occurred in MouseClick(); filename: D:; position: 171; line: 9
– Frame:
– e: dotNetObject:System.Windows.Forms.MouseEventArgs
– s: dotNetObject:System.Windows.Forms.Panel
>> MAXScript dotNet event handler Exception:
– Runtime error: Struct member access requires instance: rol <<
brothers … how to fix it??
:buttrock:
keep a reference to the struct instance inside the .tag property of the dotnet control.
bar.tag = this
and then, in the event handler:
destroydialog sender.tag.rol
I use “bar.tag = dotNetMXSValue rol” … not “bar.tag = dotNetMXSValue this”.
how to use “this” in struct??
like this:?
struct test
(
this,
val,
fn foo = (
print this.val
)
)
k = test()
k.this = k
k.foo()
you don’t need to add a this member to a struct. this refers to the instance of the struct when called from inside the struct.