public void ItemGot()
{
number = number + 1; //Increment by 1
quantity.Text = ("x" + number); //Overwrite Text
quantity.Refresh(); //Updates the text
}
您好,我上面有這段代碼。當這個方法運行時,我之前設置的標籤文本應該將文本更改爲我在下面設置的文本。但是,它沒有這樣做。此外,通過設置在Visual Studio中斷點,我已經確定:標籤文字未更新
- 的方法被調用,
- 號碼被正確地遞增。
應該沒有理由不工作,因爲該程序正在認識到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);
號碼也是在構造和設置爲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
}
}
什麼是'quantity'?這是您在設計時放置在表單上的標籤嗎?還是你在運行時創建的東西? –
只需簡要說明一下,您可以將'number = number + 1;'簡化爲'number ++;'。 – Goose
@GrantWinney'quantity'是一個標籤增加的程序化標籤,如果有幫助 – user3170251