Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,

I want to make label trasparent on a picture box.
The code I have written is
C#
label1.parent = picturebox1;// not working
label1.backcolor = color.transparent// 
even this is not working


Thanks in advance
Posted
Updated 7-Nov-10 21:39pm
v3
Comments
Dalek Dave 8-Nov-10 3:39am    
Edited for Readability.

Use both statements:
MIDL
this.label1.Parent = this.pictureBox1;
this.label1.BackColor = Color.Transparent;
 
Share this answer
 
Comments
Dalek Dave 8-Nov-10 3:39am    
Good Call.
MIDL
label1.parent = picturebox1; // not working

label1.backcolor = color.transparent // even this is not working

if you are same coding for your purpose then it will never run it because of label1 does not contain difinition of parent or backcolor

use Parent instead of parent and BackColor instead of backcolor because of keyword are case sensitive.

Use given code

MIDL
label1.Parent= pictureBox1;
         label1.BackColor = Color.Transparent;


it works :)
 
Share this answer
 
Hi,

Create a label in your code and add it to the form dynamically instead of drag-dropping it on to the form at design time.

Here's the code - I added it in the form's constructor method:

public HowToForm()
{
    InitializeComponent();
    Label lbl = new Label();
    lbl.Parent = pictureBox1;
    lbl.BackColor = Color.Transparent;
    lbl.ForeColor = Color.Black;
    lbl.Visible = true;
    lbl.Text = "DnG";
}


Thanks,
DnG

------------------------------------------------------------------
Please remember to mark this as answer if you find it useful :).
 
Share this answer
 
v2
You can't make the BackColor of a label truly transparent you need to create your own TransparentLabel class and inherit the normal Label control like;
using System;
using System.Windows.Forms;

class TransparentLabel : Label
{
    private Timer m_redrawTimer;

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams param = base.CreateParams;
            param.ExStyle |= 0x20;
            return param;
        }
    }

    public TransparentLabel()
    {
        this.SetStyle(ControlStyles.Opaque, true);
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);

        m_redrawTimer = new Timer { Interval = 50, Enabled = true };
        m_redrawTimer.Tick += new EventHandler(m_redrawTimer_Tick);
    }

    void m_redrawTimer_Tick(object sender, EventArgs e)
    {
        this.Invalidate();
    }
}
 
Share this answer
 
Comments
Dalek Dave 8-Nov-10 3:40am    
Great Answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900