C
C#10mo ago
Silme94

✅ Cannot make an label background color transparent (On a picture box)

I tried and searched a lot of way of making a background color label transparent. (btw im making it programmatically)
No description
21 Replies
Silme94
Silme94OP10mo ago
Label textLabel = new Label();
textLabel.Text = "TEST";
textLabel.BorderStyle = BorderStyle.None;
textLabel.ForeColor = Color.Transparent;
//textLabel.Parent = pictureBox;
textLabel.FlatStyle = FlatStyle.Standard;
textLabel.BackColor = Color.Transparent;
textLabel.Font = new Font("Arial", 12, FontStyle.Regular);
textLabel.Location = new Point(410, 40);
textLabel.Size = new Size(200, 100);
textLabel.TextAlign = ContentAlignment.MiddleCenter;

mainForm.Controls.Add(textLabel);
Label textLabel = new Label();
textLabel.Text = "TEST";
textLabel.BorderStyle = BorderStyle.None;
textLabel.ForeColor = Color.Transparent;
//textLabel.Parent = pictureBox;
textLabel.FlatStyle = FlatStyle.Standard;
textLabel.BackColor = Color.Transparent;
textLabel.Font = new Font("Arial", 12, FontStyle.Regular);
textLabel.Location = new Point(410, 40);
textLabel.Size = new Size(200, 100);
textLabel.TextAlign = ContentAlignment.MiddleCenter;

mainForm.Controls.Add(textLabel);
SparkyCracked
SparkyCracked10mo ago
So you can make your picturebox's background property transparent, this way you are showing the form behind it (maybe add a label directly behind your picturebox with a color). You should be able to achieve your goal
c#
pictureBox.Image = Image.FromFile("your_image_path.jpg"); // if you don't have an image you don't need this

Label textLabel = new Label();
textLabel.Text = "TEST";
textLabel.BorderStyle = BorderStyle.None;
textLabel.ForeColor = Color.Black;
textLabel.BackColor = Color.Transparent;
textLabel.Font = new Font("Arial", 12, FontStyle.Regular);
textLabel.Location = new Point(410, 40);
textLabel.Size = new Size(200, 100);
textLabel.TextAlign = ContentAlignment.MiddleCenter;

pictureBox.Controls.Add(textLabel); //adding the label to picturebox
mainForm.Controls.Add(pictureBox);
c#
pictureBox.Image = Image.FromFile("your_image_path.jpg"); // if you don't have an image you don't need this

Label textLabel = new Label();
textLabel.Text = "TEST";
textLabel.BorderStyle = BorderStyle.None;
textLabel.ForeColor = Color.Black;
textLabel.BackColor = Color.Transparent;
textLabel.Font = new Font("Arial", 12, FontStyle.Regular);
textLabel.Location = new Point(410, 40);
textLabel.Size = new Size(200, 100);
textLabel.TextAlign = ContentAlignment.MiddleCenter;

pictureBox.Controls.Add(textLabel); //adding the label to picturebox
mainForm.Controls.Add(pictureBox);
Silme94
Silme94OP10mo ago
this is working, thank you so much btw i got a little issue if you know how to solve it, when i put Visible of the form false and i run it, the form still shows i tried with Load event but no result
SparkyCracked
SparkyCracked10mo ago
So like in the future if stuff like this happens, try debug using breakpoints in you application, this tip can go far. Anyways, Just check a few things: ensure forms properties are not set to true anywhere else in your code or in the properties window of the form in the designer make sure the correct form is set as the start up object in your project settings, if its a different form, it will make it visible in your main method check if the form is set to visible (leading to check 1) check the form_load function. It's normally used for initialization, you can still try set the visibility to false. Below is an example:
c#
private void Form1_Load(object sender, EventArgs e)
{
// This ensures the form is hidden when it loads
this.Visible = false;
}
c#
private void Form1_Load(object sender, EventArgs e)
{
// This ensures the form is hidden when it loads
this.Visible = false;
}
Silme94
Silme94OP10mo ago
well i got the same code you got there but still nothing as i said and i rly have no idea of how to make it works
public Program()
{
#region FORM

this.Size = new Size(1100, 700);
this.TopMost = true;
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.None;
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
this.Load += (sender, e) => this.Visible = false;

#endregion
}
public Program()
{
#region FORM

this.Size = new Size(1100, 700);
this.TopMost = true;
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.None;
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
this.Load += (sender, e) => this.Visible = false;

#endregion
}
SparkyCracked
SparkyCracked10mo ago
This may just lead to timing issues with the forms initialization. Just try move the code itself to the 'Load' event handler method itself instead of trynna use a lambda expression:
c#
public Program()
{
#region FORM

this.Size = new Size(1100, 700);
this.TopMost = true;
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.None;
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
this.Load += Program_Load; // Attach Load event handler

#endregion
}

private void Program_Load(object sender, EventArgs e)
{
// Set form's visibility to false when it loads
this.Visible = false;
}
c#
public Program()
{
#region FORM

this.Size = new Size(1100, 700);
this.TopMost = true;
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.None;
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
this.Load += Program_Load; // Attach Load event handler

#endregion
}

private void Program_Load(object sender, EventArgs e)
{
// Set form's visibility to false when it loads
this.Visible = false;
}
Silme94
Silme94OP10mo ago
alr let me try
SparkyCracked
SparkyCracked10mo ago
This way we attach our function to the load event handler, making sure the code runs when the 'Load' event happens. No timing issues
Silme94
Silme94OP10mo ago
alright still didn't work
SparkyCracked
SparkyCracked10mo ago
Try change the event please, so from this.Load to this.Shown
Silme94
Silme94OP10mo ago
ok
SparkyCracked
SparkyCracked10mo ago
this.Shown += Program_Load;
Silme94
Silme94OP10mo ago
well it seem working, didn't knew about this event whats the difference between this one and Load?
SparkyCracked
SparkyCracked10mo ago
So the load event is when the form is initialized and loaded into memory, it happens before the form is shown, where'as the shown event happens after the form has been fully displayed for the first time The choice between using Load and Shown depends on when you need your code to execute in relation to the form's display lifecycle.
Silme94
Silme94OP10mo ago
ahh yes thats why alr thank you so much bro
SparkyCracked
SparkyCracked10mo ago
No stress dude <3
Silme94
Silme94OP10mo ago
you make me save a lot of hours
SparkyCracked
SparkyCracked10mo ago
Haha, I know the feeling @Silme94 $close
MODiX
MODiX10mo ago
Use the /close command to mark a forum thread as answered
Silme94
Silme94OP10mo ago
yes i forgot $close
MODiX
MODiX10mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server