Results 1 to 15 of 15

Thread: [RESOLVED] [2005] Hotspot for click on picture

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    910

    Resolved [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

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    910

    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?

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. Private Sub PictureBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
    2.         If New Rectangle(30, 30, 100, 10).IntersectsWith(New Rectangle(e.X, e.Y, 1, 1)) Then
    3.             MessageBox.Show("hey")
    4.         End If
    5.         'OR
    6.         Dim x As Integer = e.X
    7.         Dim y As Integer = e.Y
    8.         If x >= 30 AndAlso x <= 130 AndAlso y >= 30 AndAlso y <= 40 Then
    9.             MessageBox.Show("hey")
    10.         End If
    11.     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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    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

    Good Luck,

    D
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    910

    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.

  7. #7
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    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
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

  8. #8
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  9. #9
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    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!!!

    D
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

  10. #10
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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!!!

    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)
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  11. #11
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    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 .....That or the medication for this lovely sinus infection....

    And hey, when did we stop using invisible listboxes??
    I miss all the good memos,

    D
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. 'A collection of Rectangles.
    2. Public Class RectangleCollection
    3.     Inherits System.Collections.ObjectModel.Collection(Of Rectangle)
    4. End Class
    5.  
    6.  
    7. 'Contains data for a HotSpotsClicked event.
    8. Public Class HotSpotsClickedEventArgs
    9.     Inherits System.EventArgs
    10.  
    11.     Private _hotSpots As RectangleCollection
    12.  
    13.     Public ReadOnly Property HotSpots() As RectangleCollection
    14.         Get
    15.             Return Me._hotSpots
    16.         End Get
    17.     End Property
    18.  
    19.     Public Sub New(ByVal hotSpots As RectangleCollection)
    20.         Me._hotSpots = hotSpots
    21.     End Sub
    22.  
    23. End Class
    24.  
    25.  
    26. 'A PictureBox with clickable hotspots.
    27. Public Class ImageMapPictureBox
    28.     Inherits PictureBox
    29.  
    30.     Private _hotSpots As RectangleCollection
    31.  
    32.     Public ReadOnly Property HotSpots() As RectangleCollection
    33.         Get
    34.             If Me._hotSpots Is Nothing Then
    35.                 Me._hotSpots = New RectangleCollection
    36.             End If
    37.  
    38.             Return Me._hotSpots
    39.         End Get
    40.     End Property
    41.  
    42.     Public Event HotSpotsClicked As EventHandler(Of HotSpotsClickedEventArgs)
    43.  
    44.     Protected Overrides Sub OnMouseClick(ByVal e As System.Windows.Forms.MouseEventArgs)
    45.         MyBase.OnMouseClick(e)
    46.  
    47.         Dim hotSpots As RectangleCollection = Nothing
    48.  
    49.         'Find each hotspot that contains the point that was clicked.
    50.         For Each hotSpot As Rectangle In Me.HotSpots
    51.             If hotSpot.Contains(e.Location) Then
    52.                 If hotSpots Is Nothing Then
    53.                     hotSpots = New RectangleCollection
    54.                 End If
    55.  
    56.                 hotSpots.Add(hotSpot)
    57.             End If
    58.         Next hotSpot
    59.  
    60.         If hotSpots IsNot Nothing Then
    61.             'The mouse click occurred within at least one hotspot.
    62.             Me.OnHotSpotsClicked(New HotSpotsClickedEventArgs(hotSpots))
    63.         End If
    64.     End Sub
    65.  
    66.     Protected Overridable Sub OnHotSpotsClicked(ByVal e As HotSpotsClickedEventArgs)
    67.         RaiseEvent HotSpotsClicked(Me, e)
    68.     End Sub
    69.  
    70. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    910

    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?

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Jan 2006
    Posts
    910

    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width