使用不同的类更改form1中的textlabel

时间:2017-02-13 17:15:06

标签: c#

按下按钮时更改标签文字有点问题。我的代码看起来像这样。我在这个论坛中搜索了一下,并在form1中尝试了这个解决方案,我有这个代码。

[17:50:58] ReferenceError: project is not defined
    at Gulp.<anonymous> (D:\Documenten\Howest\Semester 4\05 - Project\MarkeOnlinebis\Project Execution\MarkeOnline\gulpfile.js:9:23)
    at module.exports (D:\Documenten\Howest\Semester 4\05 - Project\MarkeOnlinebis\Project Execution\MarkeOnline\node_modules\orchestrator\lib\runTask.js:34:7)
    at Gulp.Orchestrator._runTask (D:\Documenten\Howest\Semester 4\05 - Project\MarkeOnlinebis\Project Execution\MarkeOnline\node_modules\orchestrator\index.js:273:3)
    at Gulp.Orchestrator._runStep (D:\Documenten\Howest\Semester 4\05 - Project\MarkeOnlinebis\Project Execution\MarkeOnline\node_modules\orchestrator\index.js:214:10)
    at Gulp.Orchestrator.start (D:\Documenten\Howest\Semester 4\05 - Project\MarkeOnlinebis\Project Execution\MarkeOnline\node_modules\orchestrator\index.js:134:8)
    at C:\Users\hein_\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:129:20
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)
    at Module.runMain (module.js:592:11)
    at run (bootstrap_node.js:394:7)

events.js:160
      throw er; // Unhandled 'error' event
      ^
Error: node_modules\node-sass\test\fixtures\depth-first\_vars.scss
Error: Undefined variable: "$import-counter".
        on line 1 of node_modules/node-sass/test/fixtures/depth-first/_vars.scss
>> $import_counter: $import_counter + 1;
   -----------------^

    at options.error (D:\Documenten\Howest\Semester 4\05 - Project\MarkeOnlinebis\Project Execution\MarkeOnline\node_modules\node-sass\lib\index.js:291:26)

在课堂搜索中我有这个

public string info
{
    get
    {
        return label11.Text;
    }
    set
    {
        label11.Text = value;
    }
}

按钮有此

public void fighting()
{
    character tom = new character();
    Form1 f = new Form1();
    Random explore = new Random();
    int exploration = explore.Next(0, 3);
    if (character.location == "Forest" || character.location == "Dungeon")
    {

        switch (exploration)
        {
            case 0:
                f.info = "You didnt find anything";
                f.Refresh();
                f.herodamage = exploration.ToString();
                break;
...
我在做错了什么?每次按下按钮,文本都不会改变,但它有效,因为它确实改变了按钮文本。

2 个答案:

答案 0 :(得分:4)

战斗方法是创建一个与UI不同的Form1新实例。

在更新label1.Text时,它引用不同的Form1。

我的建议是重构代码,使责任更加明确。

这样的东西
var info = find.fighting(); // Where fighting returns string, instead of creating new Form1 and setting value, just return the value.
this.info = info;

如果您仍然坚持从搜索方法更新UI,这不是解决此问题的正确方法,您可以从参数传递Form1。

Search find = new Search(this); // Store it as instance of Search class.
find.fighting(); // fighting should never create new instance of Form1 (reason of problem you are facing)

答案 1 :(得分:3)

您需要获取对此表单的现有实例的引用,而不是创建Form1的新实例。

一种简单的方法是使用Application.OpenForms属性:

character tom = new character();
Form1 f =  Application.OpenForms.OfType<Form1>().FirstOrDefault();
Random explore = new Random();
...