[RESOLVED] [2005] Hotspot for click on picture
Hi,
I created a graphic that has some objects on it that I would like to use as buttons. To make the buttons look seamless, I want to have some sore of hotspot over top of what looks to be buttons but is actually part of one big graphic. Is there something in windows forms that is kinda like hotspots in web development? I tried adding a label over top of the button but I couldn't get the label to be invisible and still be able to take click events. I tried using the Transparent color on the "web" tab of the backcolor property but it still wasn't transparent.
Thanks
Re: [2005] Hotspot for click on picture
There are plenty of ways to do this. Ive posted one way of doing it in the codebank. If you need strange clickable shapes you could have use for my example, but if you just need rectangular clickable areas you might aswell go for a simpler alternative.
Re: [2005] Hotspot for click on picture
Thanks Atheist, that is a little more complex than what I am looking for. I just need a rectangle type label or button that is transparent. Is there a simplier method?
Re: [2005] Hotspot for click on picture
Heres some code that will do what you want, its two examples in one, you can use the one that suits you the most.
VB.Net Code:
Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
If New Rectangle(30, 30, 100, 10).IntersectsWith(New Rectangle(e.X, e.Y, 1, 1)) Then
MessageBox.Show("hey")
End If
'OR
Dim x As Integer = e.X
Dim y As Integer = e.Y
If x >= 30 AndAlso x <= 130 AndAlso y >= 30 AndAlso y <= 40 Then
MessageBox.Show("hey")
End If
End Sub
I would advice you to use the later option as it does not create any extra rectangle objects. Though the first option has its advantages aswell, but not in this case.
Re: [2005] Hotspot for click on picture
I am not understanding why you cannot use Labels too. Is the graphic always the same size or can it be resized?? If it is always the same size, then add a label where you want it and set these properties:
Visible=True
Enabled = True
BackColor = Transparent
Text = nothing
Autosize=false
borderStyle = none
These properties will give you what you want (you can now make the rectangle any size you want) and you can capture the click event of the label with no trouble.
You could also use GDI+ to create rectangles based on points and you can name them, add events (like click or mouseover). Some of that is beyond me, I know very very basic GDI+....for the moment :D
Good Luck, :wave:
D
Re: [2005] Hotspot for click on picture
Thanks for the responses guys. Dminder, I tried that earlier but when you set the backcolor to transparent then it takes the color of main form, which puts a grey block on my picture. I believe I have come up with a way to do this though. I ended up taking my picture, cropping it into multiple pieces and then adding multiple picture boxes and piecing them together.
Re: [RESOLVED] [2005] Hotspot for click on picture
Do you have to use a picturebox?? I figured out why it worked on mine and not yours. Panels have a background image property. Mine was set so when I dumped a transparent label onto it, I had no problem.
However there is one other thing I thought of. At runtime you can set the parent of the label = the picturebox. This will force the transparency to take the background/backcolor of the picturebox and not the form.
For a moment, thought I lost my mind!!!
D
Re: [RESOLVED] [2005] Hotspot for click on picture
Though I would say that using labels would be a bad practise. Labels are used to display text. You are catching the click event inside the labels boundaries, then why not just create a rectangle variable with the same boundaries and use it instead. That way you will not be having unneccessary controls on your form.
Re: [RESOLVED] [2005] Hotspot for click on picture
May I ask why it would be bad practice to use labels instead of manually having the program draw them? When you declare a new rectangle and give it the points to draw on, isn't it considered a control? So, when the app is running, it would be the same amount of controls on the form wouldnt it??
Not trying to be an arse here, I am truly curious. At some point I am sure I will come across the same issue and I want to have good practices in place. That way if anybody has to come behind me and work on something, I do not leave them with an unintelligable mess!!
Thanks!!! :afrog:
D
Re: [RESOLVED] [2005] Hotspot for click on picture
Quote:
Originally Posted by dminder
May I ask why it would be bad practice to use labels instead of manually having the program draw them? When you declare a new rectangle and give it the points to draw on, isn't it considered a control? So, when the app is running, it would be the same amount of controls on the form wouldnt it??
Not trying to be an arse here, I am truly curious. At some point I am sure I will come across the same issue and I want to have good practices in place. That way if anybody has to come behind me and work on something, I do not leave them with an unintelligable mess!!
Thanks!!! :afrog:
D
Im certainly not trying to be an arse either.:)
A rectangle is not considered a control. Its just a Structure of X,Y, width and height variables.
Think about what is needed for this task, we need do specify an area that will respond to being clicked and we dont need for it to be drawn on the screen.
The rectangle is perfect for this job, and it is better if you look to performance (thought you would need alot of clickable labels to notice the difference).
Always use the tool thats best for the job;)
Its the same reason that you dont use an invisible listbox to store strings, you use a List(of String)
Re: [RESOLVED] [2005] Hotspot for click on picture
Thank you Atheist. I understand and totally agree with you. It was my understanding of what was considered a "control" that confuzzled me:blush: .....That or the medication for this lovely sinus infection....:sick:
And hey, when did we stop using invisible listboxes??:eek:
I miss all the good memos,
D
Re: [RESOLVED] [2005] Hotspot for click on picture
Just to add my two cents, I'd would inherit the PictureBox control and put the extra functionality in there. Here's a start:
vb.net Code:
'A collection of Rectangles.
Public Class RectangleCollection
Inherits System.Collections.ObjectModel.Collection(Of Rectangle)
End Class
'Contains data for a HotSpotsClicked event.
Public Class HotSpotsClickedEventArgs
Inherits System.EventArgs
Private _hotSpots As RectangleCollection
Public ReadOnly Property HotSpots() As RectangleCollection
Get
Return Me._hotSpots
End Get
End Property
Public Sub New(ByVal hotSpots As RectangleCollection)
Me._hotSpots = hotSpots
End Sub
End Class
'A PictureBox with clickable hotspots.
Public Class ImageMapPictureBox
Inherits PictureBox
Private _hotSpots As RectangleCollection
Public ReadOnly Property HotSpots() As RectangleCollection
Get
If Me._hotSpots Is Nothing Then
Me._hotSpots = New RectangleCollection
End If
Return Me._hotSpots
End Get
End Property
Public Event HotSpotsClicked As EventHandler(Of HotSpotsClickedEventArgs)
Protected Overrides Sub OnMouseClick(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseClick(e)
Dim hotSpots As RectangleCollection = Nothing
'Find each hotspot that contains the point that was clicked.
For Each hotSpot As Rectangle In Me.HotSpots
If hotSpot.Contains(e.Location) Then
If hotSpots Is Nothing Then
hotSpots = New RectangleCollection
End If
hotSpots.Add(hotSpot)
End If
Next hotSpot
If hotSpots IsNot Nothing Then
'The mouse click occurred within at least one hotspot.
Me.OnHotSpotsClicked(New HotSpotsClickedEventArgs(hotSpots))
End If
End Sub
Protected Overridable Sub OnHotSpotsClicked(ByVal e As HotSpotsClickedEventArgs)
RaiseEvent HotSpotsClicked(Me, e)
End Sub
End Class
You can now add the Rectangles to the HotSpots property of the PictureBox and handle the HotSpotsClicked event to be notified when a hotspot is clicked and which one(s) it was.
Re: [RESOLVED] [2005] Hotspot for click on picture
Thanks! I hope I am to your level of programming one day JMC. Is all of your knowledge self taught?
Re: [RESOLVED] [2005] Hotspot for click on picture
Quote:
Originally Posted by jre1229
Thanks! I hope I am to your level of programming one day JMC. Is all of your knowledge self taught?
I taught myself VB.NET and C#, but I already had a computer science degree and two years industry experience in C/C++ so I can't take all the credit.
Re: [RESOLVED] [2005] Hotspot for click on picture
Well you deserve a pat on the back because you are a tremendous help. I am learning but sometimes you just blow my mind. Thanks again!