I'm using LuaInterface for .NET to create Windows Forms objects. This works pretty good except for one thing:
I want to use the Anchor
property of Control
to make them resize automatically. If I only set one of the Anchors (e.g. only AnchorStyles.Top
), it works, but this doesn't really make sense. I have to set more than one Anchor, which is done by combining them with "bit-wise or" (or by just adding them numerically).
In VB.Net both works:
Dim myLabel As New Label()
myLabel.Anchor = AnchorStyles.Top
myLabel.Anchor = AnchorStyles.Top + AnchorStyles.Left + _
AnchorStyles.Bottom + AnchorStyles.Right
In Lua, this does work:
luanet.load_assembly("System.Windows.Forms")
local WinForms = luanet.System.Windows.Forms
local myLabel = WinForms.Label()
myLabel.Anchor = WinForms.AnchorStyles.Top
...but this additional line doesn't:
myLabel.Anchor = WinForms.AnchorStyles.Top + WinForms.AnchorStyles.Left +
WinForms.AnchorStyles.Bottom + WinForms.AnchorStyles.Right
It gives me the following error:
LuaInterface.LuaException: attempt to perform arithmetic on
field 'Top' (a userdata value)
which is in a sense correct as "LuaInterface treats enumeration values as fields of the corresponding enumeration typ" (says LuaInterface: Scripting the .NET CLR with Lua).
It is also not possible to assign the value as a number:
myLabel.Anchor = 15 -- 15 = 8 + 4 + 2 + 1 = Top+Left+Right+Bottom
This time, the error message is rather unspecific:
LuaInterface.LuaException: function
How can I work around this?
Is there a possibility to typecast the number to the correct enumeration type in Lua?