Multiple button transparent to each picturebox in one form

zeroCodes

New member
Joined
Mar 25, 2014
Messages
4
Programming Experience
Beginner
I'm having hard time to resolve my problem.. How to put a transparent button over a picturebox.. MULTIPLE TIMES! Per picturebox, one transparent button inside.
I saw jmcilhinney's codes and only worked for one button.. Can i have a complete guide because im just a month new to VB ...
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,374
Location
Sydney, Australia
Programming Experience
10+
Transparency in Windows Forms is a tricky thing. When you set the BackColor of a control to Transparent, the control itself does not actually become transparent. What actually happens is that the control creates an image of its Parent behind it and displays that, even if its Parent is not what is directly underneath it. What that means for a PictureBox showing through a Button is that only one PictureBox can be shown through each Button and that Button must be wholly within the bounds of that PictureBox.

So, what exactly are you asking for? Are you saying that you want a Button to be able to overlap multiple PictureBoxes? If so then you're out of luck. If what you saying is that you have multiple Buttons and you want each of them to be able to show a PictureBox through them then you can do that easily enough. If you already have my code for doing it with one Button then you have all you need. You simply do the same thing multiple times, once for each Button.
 

zeroCodes

New member
Joined
Mar 25, 2014
Messages
4
Programming Experience
Beginner
I have 3 buttons and 3 pictureboxes, one button per picbox. But my problem is, i copied your code 3 times and works once only. I want to show my codes to fix whats wrong... Thanks for replying jmcilhinney :) What do you think is my problem here? Button1 is the only button transparent to its respective picturebox, but the other two don't work.. When I place the Me.EmbedButtonInPictureBox2(Me.Button2, Me.PicBox2) code before the first one, that button will be transparent the other two again, arent transparent same with the 3rd ones.


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Me.EmbedButtonInPictureBox1(Me.Button1, Me.PicBox1)
Me.EmbedButtonInPictureBox2(Me.Button2, Me.PicBox2)
Me.EmbedButtonInPictureBox3(Me.Button3, Me.PicBox3)

End Sub
Private Sub EmbedButtonInPictureBox1(ByVal btn1 As Button, ByVal pbx As PictureBox)
Dim buttonLocation As Point = pbx.PointToClient(Me.PointToScreen(btn1.Location))

btn1.Parent = pbx
btn1.Location = buttonLocation

Dim buttonBackground As New Bitmap(btn1.Width, btn1.Height)

Using g As Graphics = Graphics.FromImage(buttonBackground)
g.DrawImage(pbx.Image, _
New Rectangle(0, _
0, _
buttonBackground.Width, _
buttonBackground.Height), _
btn1.Bounds, _
GraphicsUnit.Pixel)
End Using

btn1.BackgroundImage = buttonBackground
End Sub
Private Sub EmbedButtonInPictureBox2(ByVal btn2 As Button, ByVal picbx As PictureBox)
Dim buttonLocation As Point = picbx.PointToClient(Me.PointToScreen(btn2.Location))

btn2.Parent = picbx
btn2.Location = buttonLocation

Dim buttonBackground2 As New Bitmap(btn2.Width, btn2.Height)

Using g As Graphics = Graphics.FromImage(buttonBackground2)
g.DrawImage(picbx.Image, _
New Rectangle(0, _
0, _
buttonBackground2.Width, _
buttonBackground2.Height), _
btn2.Bounds, _
GraphicsUnit.Pixel)
End Using

btn2.BackgroundImage = buttonBackground2
End Sub
Private Sub EmbedButtonInPictureBox3(ByVal btn As Button, ByVal pbx As PictureBox)
Dim buttonLocation2 As Point = pbx.PointToClient(Me.PointToScreen(btn.Location))

btn.Parent = pbx
btn.Location = buttonLocation2

Dim buttonBackground As New Bitmap(btn.Width, btn.Height)

Using g As Graphics = Graphics.FromImage(buttonBackground)
g.DrawImage(pbx.Image, _
New Rectangle(0, _
0, _
buttonBackground.Width, _
buttonBackground.Height), _
btn.Bounds, _
GraphicsUnit.Pixel)
End Using

btn.BackgroundImage = buttonBackground
End Sub
 

zeroCodes

New member
Joined
Mar 25, 2014
Messages
4
Programming Experience
Beginner
Please help me on this one @jimcilhinney, my deadline is in 2 days I need your help on this, I really help on this one
 
Top Bottom