Home > Mobile >  Change form location using nlua
Change form location using nlua

Time:07-20

I'm writing a simple program in c# using .net framework 4.8, winforms, and nlua. I want to change the location of my main form using a lua script. Here is what I tried:

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }
}
using NLua;

lua_context = new Lua();
lua_context.LoadCLRPackage();
lua_context.DoString("import 'System.Drawing'");
lua_context.DoString("import 'System.Windows.Forms'");

MainForm main_form = new MainForm();
lua_context["main_form"] = main_form;
lua_context.DoString("main_form.Location = Point(5, 5)");

I get this error when calling the last line:

Exception thrown: 'NLua.Exceptions.LuaScriptException' in NLua.dll An unhandled exception of type 'NLua.Exceptions.LuaScriptException' occurred in NLua.dll [string "chunk"]:1: attempt to call a nil value (global 'Point')

I have also tried:

lua_context.DoString("main_form.Location = System.Drawing.Point(5, 5)");

and I get this error:

Exception thrown: 'NLua.Exceptions.LuaScriptException' in NLua.dll An unhandled exception of type 'NLua.Exceptions.LuaScriptException' occurred in NLua.dll [string "chunk"]:1: attempt to index a nil value (global 'System')

CodePudding user response:

Try to add

import("System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

The form.lua used to work

CodePudding user response:

There is no global Point in Lua. Maybe you can use System.Drawing.Point(5, 5).

Alternatively adding lua_context.DoString("import 'System.Drawing'"); should fix the problem.

  • Related