如何在 Windows Forms TextBox 中运行 Lua 脚本

我使用 Visual Studio 2019 构建了一个表单,想要在该表单中使用 TextBox 执行脚本,所以如果我执行 print("Hello"); ,下一行将是 hello。 我使用 FastColoredTextBox 来实现,我注意到它有一个语言属性,我已经将其设置为 Lua。但是,我怎样才能在该文本框中执行命令呢?

非常感谢你们的帮助 :)

点赞
用户3110834
用户3110834

你可以安装DynamicLua包在C#中编译和运行Lua代码。

例如,假设你在表单上有一个fastColoredTextBox1和一个运行代码的按钮toolStripButton1,那么你可以添加以下代码:

private void Form1_Load(object sender, EventArgs e)
{
    this.fastColoredTextBox1.Language = FastColoredTextBoxNS.Language.Lua;
    this.fastColoredTextBox1.Text= "function echo(s) return s end" + "\n" +
        "return echo(\"Hello!\")";
}

private void toolStripButton1_Click(object sender, EventArgs e)
{
    dynamic lua = new DynamicLua.DynamicLua();
    var result = lua(fastColoredTextBox1.Text);
    MessageBox.Show($"{result}");
}

enter image description here

2020-02-20 07:21:36