What's new

Why wouldn't this label show up despite its Visible parameter being set to True and its Text parameter containing a string?


Active member
Staff member
enter image description here

As can be seen in the image above, I have four labels that are on top of some PictureBoxes. Note that I added the blue text and arrow in the image in Paint; they are not elements of the form.

By default, these labels have their Visible parameters set to False and their Text parameters are set to being empty. At specific points in the program, one of the labels is selected to both have Visible set to True and to have a 3-digit-or-less number alongside a percent sign(in the format ### % in which # is a digit) in black size 12.75 Microsoft Sans Serif font. However, only the top label(the one being pointed at by the blue arrow) actually becomes visible and displays its text when its parameters are changed in this way. The top image below shows what the top label looks like when this happens, while the bottom image below shows what the other three labels look like when the same process is applied to any of them.

enter image description here

enter image description here

Ignoring the difference in aspect ratio between the two images, the only important difference between the two is that in the first one, the label is visible and is displaying "75 %", while in the second one, the label is not visible so there is no number or percent sign.

I know for a fact that in the second instance the label's Visible property is set to True and its text is set to "75 %" through debugging, but none of this shows up when the form is running. Another thing that I have determined through debugging is that this issue is either caused by or at least apparent in the function below, since this function is the only code block that:

a) is used in every instance in which the labels are set to having Visible be True and their text being set to that "### %" format, and also

b) distinguishes between the different labels.

private void UpdateHpText(PictureBox HPBox) //the function in question
//this conditional tree takes "HPBox," one of the Pictureboxes that the labels overlap(the green bars), and uses it to decide which label needs to be altered
if(HPBox == side2HPbox1)
//Side2HPText1 is the top label, Side2HPText2 is the next from the top label, and etc.
Side2HPText1.Visible = HPBox.Visible; //I know from debugging that HpBox.Visible should be set to True in the instance I am having trouble with
side2HPbox1DisplayNum = (BasicProcess.side2[0].HP.value * 100) / (BasicProcess.side2[0].HP.max); //calculates the number displayed in the Label's text
Side2HPText1.Text = side2HPbox1DisplayNum + " %"; //sets the label's text
//in the above instance, the label should have Visible set to True, and its Text should be assigned to a string. However, I'm not having issues with this block.
//so tell me, is there ANY difference between the above block and the below block other than:(see differences in documentation of each line)
else if (HPBox == side2HPbox2)
Side2HPText2.Visible = HPBox.Visible; //a different label is targeted here
side2HPbox2DisplayNum = (BasicProcess.side2[1].HP.value * 100) / (BasicProcess.side2[1].HP.max); //BasicProcess.side2[1] is being used instead here because it targets the object that is assigned to the label in this block, and side2HPBoxDisplayNum is just used in the final line of this block for the displayed string
Side2HPText2.Text = side2HPbox2DisplayNum + " %";
//from what I see, both blocks are functionally the same, and I also know that to be the case from debugging, but when the form runs, I get a completely different result for each block.
else if (HPBox == side2HPbox3)
Side2HPText3.Visible = HPBox.Visible;
side2HPbox3DisplayNum = (BasicProcess.side2[2].HP.value * 100) / BasicProcess.side2[2].HP.max;
Side2HPText3.Text = side2HPbox3DisplayNum + " %";
Side2HPText4.Visible = HPBox.Visible;
side2HPbox4DisplayNum = (BasicProcess.side2[3].HP.value * 100) / BasicProcess.side2[3].HP.max;
Side2HPText4.Text = side2HPbox4DisplayNum + " %";

As you can read in the code above, there shouldn't be any difference between what the top label looks like when its block runs and what the second label looks like when its block runs, but there is. This makes me think that I might be having a GUI error that I'm not noticing in my code. One thing I've learned about GUI in Winforms is that that threading can have an impact on how the GUI updates itself. For that reason, I will post the relevant code in my program below that uses Async and Await, which might have an impact on threading.

Side2HPBoxList[BasicProcess.side2.IndexOf(targetList[0])].Visible = true; //sets one of the green bar PictureBox's Visible parameter to True
await Task.Delay(500); //waits half a second
targetList[0] = char1ChosenSkill.effect(targetList[0]); //this can be ignored since it does not effect the PictureBox or Label
Side2HPBoxList[BasicProcess.side2.IndexOf(targetList[0])].Image = UpdateHpBar(targetList[0], BasicProcess.side2); //runs UpdateHPBar
await Task.Delay(1500); //waits 1.5 seconds
Side2HPBoxList[BasicProcess.side2.IndexOf(targetList[0])].Visible = false; //sets one of the green bar PictureBox's Visible back to false
Menu2Background.Visible = (char2 != null); //this can be ignored since it does not effect the PictureBox or Label

However, I don't think that Task.Delay could genuinely scuff up the GUI that much, especially considering that it runs for each and every label the same way.

Another potential reason for my program not running correctly is the properties of the labels. For that reason, I will post them below. Please don't forget that even though the Text and Visible elements are set to being empty and False respectively here, they should be set to containing a string and being True at the point in the program that I am having trouble with.

// Side2HPText1
this.Side2HPText1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.Side2HPText1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(2)))), ((int)(((byte)(218)))), ((int)(((byte)(55)))));
this.Side2HPText1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Side2HPText1.Location = new System.Drawing.Point(1109, 127);
this.Side2HPText1.Name = "Side2HPText1";
this.Side2HPText1.Size = new System.Drawing.Size(58, 26);
this.Side2HPText1.TabIndex = 66;
this.Side2HPText1.Visible = false;

// Side2HPText2
this.Side2HPText2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.Side2HPText2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(2)))), ((int)(((byte)(218)))), ((int)(((byte)(55)))));
this.Side2HPText2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Side2HPText2.Location = new System.Drawing.Point(1109, 279);
this.Side2HPText2.Name = "Side2HPText2";
this.Side2HPText2.Size = new System.Drawing.Size(58, 26);
this.Side2HPText2.TabIndex = 70;
this.Side2HPText2.Visible = false;

I haven't noticed any differences here that could be relevant to my issue, but maybe someone will.

Anyhow, if I haven't provided enough relevant information here, feel free to ask me to provide more.

EDIT: I'm not entirely sure how to provide a reproducible example without sharing absolutely all of my code. I will start documenting a big chunk of it and tack it to the bottom of this post by the end of today, but it will take time. Hopefully doing that will help

EDIT 2: Actually, I can provide some more info before I do that. Firstly, when I said that I determined that the Visible parameter was set to True and the Text was set to a string by debugging, I meant that I set up a breakpoint and looked in the Autos field in Debug mode to determine that this was the case. I also have brought the Labels to the front several times now(thinking that maybe I did it wrong before or something), so I am confident that they are brought to the front. I also put a line of code in UpdateHPText saying:

Side2HPText2.Parent = Side2HPText2.Parent;

so I could see that value in the Autos field. It turns out that this value was set to the form it's on(BattleTemplate4), so I think that that is fine.

EDIT 3: Here's a pastebin link to the InitializeComponent function since it won't fit the character limit: https://pastebin.com/WRRJ5n4d (a commenter asked for it)

Continue reading...