标签文字未更新

时间:2014-01-09 00:08:08

标签: c# winforms

public void ItemGot()
{
    number = number + 1;            //Increment by 1
    quantity.Text = ("x" + number); //Overwrite Text
    quantity.Refresh();             //Updates the text
}

您好,我上面有这个代码。运行此方法时,我之前设置的标签文本应将文本更改为我在下面设置的文本。但是,它没有这样做。此外,通过在Visual Studio中设置断点,我已确定:

  1. 正在调用该方法,
  2. 正在递增数字。
  3. 没有理由说这不起作用,因为程序认识到number正在增加一个。我的朋友说了一个类似的问题here。仍然没有帮助,现在代码已经过时了。请帮忙!

    编辑:我如何添加quantity标签

    首先,我在构造函数中初始化它:public Label quantity;

    然后我这样做了:quantity = new Label();

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

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

    number也在构造函数中,并设置为0。

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

        public InventoryScreen()
        {
            btnItems = new Button();
            quantity = new Label();
    
            //call the methods for spawning the buttons 
            ButtonGenItems(cNumber, btnItems, quantity);
    
            InitializeComponent();
        }
    
        public void InventoryScreen_Load(object sender, EventArgs e)
        {
    
        }
    
        #region ButtonGenItems Method
        public void ButtonGenItems(int cNumber, Button btnItems,Label quantity)
        {
            int xPos = 126;
            int yPos = 25;
    
            for (int n = 0; n < 1; n++)
            {
                btnItems.Tag = n;
                btnItems.Size = new Size(48, 52);  //Button size X and Y
                btnItems.BackColor = Color.CornflowerBlue;
    
                quantity.Size = new Size(24, 24);
                quantity.Text = ("x" + number);
    
                if (yPos > 60) // Five Buttons in one column
                {
                    yPos = 25; //spawn position Y
                    xPos = xPos + btnItems.Width + 10; //spacing X
                }
    
                btnItems.Left = xPos; //Start Button spawn at the Left side
                btnItems.Top = yPos;  //Start spawn at the top side
    
                quantity.Left = 48;
                quantity.Top = 60;
    
                yPos = yPos + btnItems.Height + 10;
    
                btnItems.Text = "Use";
    
                Controls.Add(btnItems); //place Buttons
                Controls.Add(quantity);
    
                // the Event of click Button
                //btnItems.Click += new System.EventHandler(ItemUse); //to be implimented
            }
        }
    
        #endregion
    
        public void ItemGot()
        {
            //*Interestingly, the program recognizes that 'number' is increasing by 1, but label won't update the text
            //Furthermore, pressing the actual button will trigger the text update, but simulating a buttonclick WONT DO ANYTHING 
    
            Console.WriteLine("Text should now increment by 1");  //Debugging to test method
            number = number + 1;            //Increment by 1
            quantity.Text = ("x" + number); //Overwrite Text
        }
    }
    

    2.5 这是调用方法的方法。此方法位于另一个类

    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
                }
            }
    

1 个答案:

答案 0 :(得分:1)

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

在方法Update内部,您创建了InventoryScreen的新实例,并使用此新的表单实例调用函数ItemGot

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

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(); 
    }
}