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