Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 标签文本未更新_C#_Winforms - Fatal编程技术网

C# 标签文本未更新

C# 标签文本未更新,c#,winforms,C#,Winforms,你好,我有上面的代码。当此方法运行时,我前面设置的标签的文本应更改为我在下面设置的文本。然而,它并没有这样做。此外,通过在Visual Studio中设置断点,我确定: 正在调用该方法 数字正在正确递增 应该没有理由不起作用,因为程序正在识别number正在增加1。我的朋友也提出了类似的问题。仍然没有帮助,现在代码已经过时了。请帮忙 编辑:我如何添加数量标签 首先,我在构造函数中初始化它:公共标签数量 然后我这样做:quantity=newlabel() 最后,在另一种方法中,我给了数量以下属性

你好,我有上面的代码。当此方法运行时,我前面设置的标签的文本应更改为我在下面设置的文本。然而,它并没有这样做。此外,通过在Visual Studio中设置断点,我确定:

  • 正在调用该方法
  • 数字正在正确递增
  • 应该没有理由不起作用,因为程序正在识别
    number
    正在增加1。我的朋友也提出了类似的问题。仍然没有帮助,现在代码已经过时了。请帮忙

    编辑:我如何添加
    数量
    标签

    首先,我在构造函数中初始化它:
    公共标签数量

    然后我这样做:
    quantity=newlabel()

    最后,在另一种方法中,我给了
    数量
    以下属性:

    public void ItemGot()
    {
        number = number + 1;            //Increment by 1
        quantity.Text = ("x" + number); //Overwrite Text
        quantity.Refresh();             //Updates the text
    }
    
    数字也在构造函数中,并设置为0

    编辑2:我将发布我的整个方法

    quantity.Size = new Size(24, 24);
    quantity.Text = ("x" + number);
    
    quantity.Left = 48;
    
    Controls.Add(quantity);
    

    据我所知,在调用
    Update
    方法时,您已经用标签打开/显示了表单(类
    InventoryScreen
    的实例),但是

    在方法
    Update
    中,您创建了
    InventoryScreen
    的一个新实例,并使用这个新的表单实例调用函数
    ItemGot

    我认为您需要在方法
    Update
    中传递当前
    InventoryScreen
    实例的引用,然后使用该引用调用
    ItemGot
    方法

    public void Update(Vector2 pos)
            {
                this.position = pos; //get char position
                Inv = new InventoryScreen(); //create instance of object
                charRange = new Rectangle((int)position.X, (int)position.Y, 64, 57); //create rectangle
    
    
                //Intersection Code, If the character intersects with the item while the item is showing, run below
                if (alive && charRange.Intersects(itemRect))
                {
                    alive = false; //stop showing the item
                    Inv.ItemGot(); //Call the ItemGot class, which adds the item to the inventory screen
                }
            }
    

    仅供参考,您可以简化
    number=number+1到<代码>数字++
    @grantwiney
    quantity
    是通过编程添加的标签,如果helps@GrantWinney请参见上面的编辑。上面的代码应该可以工作。如何调用此方法?@Steve well我从另一个类调用了它,因此
    Inv.ItemGot()
    其中Inv是保存
    ItemGot()方法的对象的实例。
    
    public void Update(Vector2 pos)
            {
                this.position = pos; //get char position
                Inv = new InventoryScreen(); //create instance of object
                charRange = new Rectangle((int)position.X, (int)position.Y, 64, 57); //create rectangle
    
    
                //Intersection Code, If the character intersects with the item while the item is showing, run below
                if (alive && charRange.Intersects(itemRect))
                {
                    alive = false; //stop showing the item
                    Inv.ItemGot(); //Call the ItemGot class, which adds the item to the inventory screen
                }
            }
    
    public void Update(Vector2 pos, InventoryScreen invscreen)
    {
        this.position = pos;
        charRange = new Rectangle((int)position.X, (int)position.Y, 64, 57);
    
        if (alive && charRange.Intersects(itemRect))
        {
            alive = false;
            invscreen.ItemGot(); 
        }
    }