简体   繁体   English

在 C# 中,如何将变量的值而不是变量添加到 mouseclick 事件中?

[英]How do you add the value of a variable instead of the variable to a mouseclick event in C#?

I decided that I'd try creating a simple programming language.我决定尝试创建一种简单的编程语言。 Everything's working so far except for one thing - executing code by clicking on a GUI element.到目前为止,一切都在工作,除了一件事 - 通过单击 GUI 元素执行代码。 This is the line of code responsible for adding the event to a button:这是负责将事件添加到按钮的代码行:

btn[x].MouseClick += (sender, e) => runAct(act_alloc, sender, e);

The problem here is that the runAct();这里的问题是 runAct(); void requires an ID (act_alloc) to know what code to run, but act_alloc changes every time a new action for a GUI element is created, but I need the MouseClick event to be a constant number, for example: void 需要一个 ID (act_alloc) 才能知道要运行什么代码,但是每次为 GUI 元素创建新操作时 act_alloc 都会发生变化,但是我需要 MouseClick 事件是一个常数,例如:

btn[0].MouseClick += (sender, e) => runAct(2, sender, e);

I need to get the value of the act_alloc variable at the time of adding the event to the element, but just the value, not the variable itself - I don't want the MouseClick event to use a different ID (the value of act_alloc at the time the element is clicked), I want the MouseClick event to use the same ID every time (the value of act_alloc at the time the element's MouseClick event is assigned to runAct(...) .我需要在将事件添加到元素时获取act_alloc变量的值,但只是获取值,而不是变量本身 - 我不希望 MouseClick 事件使用不同的 ID( act_alloc的值在点击该元素的时间),我想鼠标点击事件,每一次的值(使用相同的ID act_alloc在元素的鼠标点击事件被分配到的时间runAct(...)

The app just reads code from a file line by line.该应用程序只是逐行从文件中读取代码。 The act_alloc integer gets increased by 1 every time a MouseClick event is added to a GUI element.每次将act_alloc事件添加到 GUI 元素时, act_alloc整数都会增加 1。

The runAct void requires int id, object sender, EventArgs e . runAct void 需要int id, object sender, EventArgs e The last two are there just so that it can be used as a MouseClick.最后两个只是为了它可以用作鼠标点击。 When some code gets added from the code file to the element's MouseClick event, it gets stored in a String[1024,128] - [Event ID, code (<=127 lines)].当某些代码从代码文件添加到元素的 MouseClick 事件时,它会存储在 String[1024,128] - [Event ID, code (<=127 lines)] 中。 This is the runAct void:这是 runAct 无效:

public void runAct(int id, object sender, EventArgs e)
{
    for (int x = 1; x <= Convert.ToInt32(act[id, 0]); x++)
        runCode(act[id, x].Split(';'));
}

act is the String[,] in which code is stored. act是存储代码的 String[,]。 The first string is always the amount of lines stored, which gets converted into an integer so that it won't just call runCode over and over again without any code to run.第一个字符串总是存储的行数,它被转换成一个整数,这样它就不会在没有任何代码运行的情况下一遍runCode遍地调用runCode The runCode void does what it's called - runs a line of code. runCode void 执行它所谓的操作 - 运行一行代码。 One of those can be act;(gui_element);其中之一可以是act;(gui_element); , which means that all following code gets stored in the act string[,]. ,这意味着以下所有代码都存储在行为 string[,] 中。 This stops at a endact;这到了endact; line.线。 After the code is stored in the string[,] ( act[act_alloc, line] ), act_alloc gets increased by 1 so that the app's ready to store some more code in the string[,].代码存储在 string[,] ( act[act_alloc, line] ) 后, act_alloc增加 1,以便应用程序准备在 string[,] 中存储更多代码。

This is the code that adds a MouseClick event to a GUI element:这是将 MouseClick 事件添加到 GUI 元素的代码:

case "act": //add code to an element
    bool wa = true; //keep looping
    int ln = 1; //line number
    while (wa) //loop for adding more code
    {
        string actstr = vars(sr.ReadLine()); //read the next line
        if (actstr == "endact;") //was that all the code?
        {
            wa = false; //stop the loop
        }
        else
        {
            act[act_alloc, ln] = actstr; //store the line
            ln++; //next line
        }
    }
    ln--; //decrease ln by 1 to make sure runAct runs the correct amount of times
    act[act_alloc, 0] = ln.ToString(); //the amount of lines stored gets saved
    switch (cmd[2]) //what GUI element is this code for?
    {
        case "text": //a label
            for (int x = 0; x < label_alloc; x++) //cycle through all the labels
                if (labels[x].Name == cmd[3]) //does the label name match?
                    labels[x].MouseClick += (sender, e) => runAct(act_alloc, sender, e); //add the event
            break;
            case "btn": //a button
                for (int x = 0; x < btn_alloc; x++) //cycle through all the buttons
                    if (btn[x].Name == cmd[3]) //does the button name match?
                        btn[x].MouseClick += (sender, e) => runAct(act_alloc, sender, e); //add the event
            break;
    }
    act_alloc++; //get ready for storing more code
    break;

If that's still not enough info, please let me know.如果这仍然不够信息,请告诉我。

How about copying the value of act_alloc right before that line, and then use that local variable in runAct(localvariable, sender, e)?如何在该行之前复制 act_alloc 的值,然后在 runAct(localvariable, sender, e) 中使用该局部变量? (I'm not certain that your problem is actually there in the first place though) (不过,我不确定您的问题实际上是否存在)

– lucidbrot – 清醒兄弟

That actually solved my problem, thank you!这实际上解决了我的问题,谢谢! I just added a new int right before adding the MouseClick event to the element:在向元素添加 MouseClick 事件之前,我刚刚添加了一个新的 int :

act[act_alloc, 0] = ln.ToString();
int eid = act_alloc;
switch (cmd[2])
{
    case "text":
        for (int x = 0; x < label_alloc; x++)
            if (labels[x].Name == cmd[3])
                labels[x].MouseClick += (sender, e) => runAct(eid, sender, e);
        break;
    case "btn":
        for (int x = 0; x < btn_alloc; x++)
            if (btn[x].Name == cmd[3])
                btn[x].MouseClick += (sender, e) => runAct(eid, sender, e);
        break;
}
act_alloc++;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM