Answered by:
Transparent Label on a Picturebox while runtime

Question
-
So i am trying to make a 'new message' function in my little game.
Its supposed to create a new label and fit an image to the label's content.
My code section looks like this:
Function NewMensaje(ByVal Mensage As String, ByVal x As Integer, ByVal y As Integer) As String Dim label As New Label With label .Name = "label" .ForeColor = Color.Black .Location = New Point(x, y) .Text = Mensage .AutoSize = True .Visible = True .BackColor = Color.Transparent End With Me.Controls.Add(label) Dim pic As New PictureBox With pic .Name = "IMensaje1" .Location = New Point(x, y - 10) .Image = Image.FromFile(filepath & "\Mensaje.png") .Width = label.Width + 20 .Height = label.Height + 20 .SizeMode = PictureBoxSizeMode.StretchImage End With Me.Controls.Add(pic)
Which makes the image under the text (mensaje.png) appear just fine, it fit the labels dimensions. but the label fails to be transparent above said image.
i heard something about parents and .settoback or something like that, but i have no idea how to make this work.
Thank you all
- Edited by NelsonPer Sunday, February 3, 2013 11:05 PM
Sunday, February 3, 2013 11:03 PM
Answers
-
I don't really understand your requirement. Do you want a transparent label that still shows its text or one that disappears altogether? Or one that has an Image and text in it and then disappears? Or what is the requirement?
This code will make the label transparent but the text in the label disappears too.
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load SetStyle(ControlStyles.SupportsTransparentBackColor, True) End Sub Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Label1.Parent = PictureBox1 Label1.BackColor = Color.FromArgb(0, 0, 0, 0) End Sub Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click End Sub End Class
You've taught me everything I know but not everything you know.
- Proposed as answer by Xiong Wei, Jin Monday, February 4, 2013 5:16 PM
- Marked as answer by Youen Zen Tuesday, February 12, 2013 9:47 AM
Sunday, February 3, 2013 11:52 PM -
Do you know how to archieve to make only the backgroudn transparent?
No but this may work for your game.
It draws text onto the picturebox using Graphics so there is no requirement for a label. You can add new text at will by changing the string being written to the picturebox or zero out the string and no text appears on the picturebox.
Problem - Requires a timer to continually draw to the picturebox as it is refreshed by your game (at least I think it does) using another resource.
Anyhow here's the code. Needs a Form, Button, PictureBox with a picture in it for visual perspective I suppose and a Timer.
Public Class Form1 Dim Test As String = "" Dim TestCount As Integer = 0 Dim Counter1 As Integer = 0 Dim Counter2 As Integer = 0 Dim stringFont As New Font("Arial", 24, FontStyle.Bold) Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Timer1.Interval = 1 Timer1.Start() End Sub Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If Counter1 = 0 Then Test = "Testing" TestCount = Test.Count End If If Counter1 = 1 Then Test = "Re Testing" TestCount = Test.Count End If If Counter1 = 2 Then Test = "Still Testing" TestCount = Test.Count End If If Counter1 = 3 Then Test = "" TestCount = Test.Count End If Counter1 = Counter1 + 1 If Counter1 = 4 Then Counter1 = 0 Counter2 = 1 End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick Dim string_format As New StringFormat() string_format.Alignment = StringAlignment.Center string_format.LineAlignment = StringAlignment.Near If Counter2 = 1 Then PictureBox1.Refresh() PictureBox1.CreateGraphics.DrawString(Test, stringFont, Brushes.Red, PictureBox1.Width / 2, PictureBox1.Height - 50, string_format) ' PictureBox1.Height / 2) Counter2 = 0 End Sub End Class
You've taught me everything I know but not everything you know.
- Marked as answer by Youen Zen Tuesday, February 12, 2013 9:47 AM
Monday, February 4, 2013 4:41 AM -
Hi NelsonPer
Welcome to MSDN
Try the following:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Label1.Parent = PictureBox1 Label1.Location = New Point(0, 0) Label1.BackColor = Color.Transparent 'transparent only to its parent End Sub
Set label position within picturebox client area: this is necessary, if not, in case when the label was drag and drop on the Form1 outside picturebox client area dimmension then we will not abble to see the label when the parent is changed from Form1 to PictureBox1 because the label location is outside the Picturebox client area.
Transparent only to its parent, if you need transparent to other controls the we can use GraphicsPath and set Region.
Option Strict On
Helpful Links:- Marked as answer by Youen Zen Tuesday, February 12, 2013 9:47 AM
Monday, February 4, 2013 5:15 PM
All replies
-
I don't really understand your requirement. Do you want a transparent label that still shows its text or one that disappears altogether? Or one that has an Image and text in it and then disappears? Or what is the requirement?
This code will make the label transparent but the text in the label disappears too.
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load SetStyle(ControlStyles.SupportsTransparentBackColor, True) End Sub Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Label1.Parent = PictureBox1 Label1.BackColor = Color.FromArgb(0, 0, 0, 0) End Sub Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click End Sub End Class
You've taught me everything I know but not everything you know.
- Proposed as answer by Xiong Wei, Jin Monday, February 4, 2013 5:16 PM
- Marked as answer by Youen Zen Tuesday, February 12, 2013 9:47 AM
Sunday, February 3, 2013 11:52 PM -
Thank you so much for helping me.
"Do you want a transparent label that still shows its text " Exactly, thats what i need.
Regards
Monday, February 4, 2013 12:26 AM -
Thank you so much for helping me.
"Do you want a transparent label that still shows its text " Exactly, thats what i need.
Regards
That code will not help you because the text disappears along with the label.You've taught me everything I know but not everything you know.
Monday, February 4, 2013 12:28 AM -
Do you know how to archieve to make only the backgroudn transparent?Monday, February 4, 2013 12:34 AM
-
Do you know how to archieve to make only the backgroudn transparent?
No but this may work for your game.
It draws text onto the picturebox using Graphics so there is no requirement for a label. You can add new text at will by changing the string being written to the picturebox or zero out the string and no text appears on the picturebox.
Problem - Requires a timer to continually draw to the picturebox as it is refreshed by your game (at least I think it does) using another resource.
Anyhow here's the code. Needs a Form, Button, PictureBox with a picture in it for visual perspective I suppose and a Timer.
Public Class Form1 Dim Test As String = "" Dim TestCount As Integer = 0 Dim Counter1 As Integer = 0 Dim Counter2 As Integer = 0 Dim stringFont As New Font("Arial", 24, FontStyle.Bold) Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Timer1.Interval = 1 Timer1.Start() End Sub Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If Counter1 = 0 Then Test = "Testing" TestCount = Test.Count End If If Counter1 = 1 Then Test = "Re Testing" TestCount = Test.Count End If If Counter1 = 2 Then Test = "Still Testing" TestCount = Test.Count End If If Counter1 = 3 Then Test = "" TestCount = Test.Count End If Counter1 = Counter1 + 1 If Counter1 = 4 Then Counter1 = 0 Counter2 = 1 End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick Dim string_format As New StringFormat() string_format.Alignment = StringAlignment.Center string_format.LineAlignment = StringAlignment.Near If Counter2 = 1 Then PictureBox1.Refresh() PictureBox1.CreateGraphics.DrawString(Test, stringFont, Brushes.Red, PictureBox1.Width / 2, PictureBox1.Height - 50, string_format) ' PictureBox1.Height / 2) Counter2 = 0 End Sub End Class
You've taught me everything I know but not everything you know.
- Marked as answer by Youen Zen Tuesday, February 12, 2013 9:47 AM
Monday, February 4, 2013 4:41 AM -
Hi NelsonPer
Welcome to MSDN
Try the following:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Label1.Parent = PictureBox1 Label1.Location = New Point(0, 0) Label1.BackColor = Color.Transparent 'transparent only to its parent End Sub
Set label position within picturebox client area: this is necessary, if not, in case when the label was drag and drop on the Form1 outside picturebox client area dimmension then we will not abble to see the label when the parent is changed from Form1 to PictureBox1 because the label location is outside the Picturebox client area.
Transparent only to its parent, if you need transparent to other controls the we can use GraphicsPath and set Region.
Option Strict On
Helpful Links:- Marked as answer by Youen Zen Tuesday, February 12, 2013 9:47 AM
Monday, February 4, 2013 5:15 PM