too many images

jagguy
jagguy used Ask the Experts™
on
i have a number of images i use as buttons and how do i simplify this so I dont need a seprate procedure for every image?

i am using silverlight

 

  AddHandler _img2.MouseLeftButtonDown, AddressOf img2_click
  AddHandler _img3.MouseLeftButtonDown, AddressOf img3_click
 Public Sub img2_click(ByVal sender As System.Object, ByVal e As MouseButtonEventArgs)
        RaiseEvent getOption(1)

    End Sub
    Public Sub img3_click(ByVal sender As System.Object, ByVal e As MouseButtonEventArgs)
        RaiseEvent getOption(2)

    End Sub
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can use an image map control to define regions of a larger image, responding to mouse events within them.

Commented:
Have all of the image click events point to one event handler then perform whatever action you need in that one handler based on the object that raised the event.
Yes, I would suggest the same thing as TSmooth. If you want to know specifically which image is calling your common routine add some Tag to you images and use them on the routine start to determine which image is calling the routine.

DOH! I misread the question. I thought he didn't want as many images!

You're right, guys ... there's absolutely no need to have a separate handler for each image.
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
(haven't worked with Silverlight)

That might look something like:

        AddHandler _img2.MouseLeftButtonDown, AddressOf img_click
        AddHandler _img3.MouseLeftButtonDown, AddressOf img_click
        AddHandler _img4.MouseLeftButtonDown, AddressOf img_click
        AddHandler _img5.MouseLeftButtonDown, AddressOf img_click

Then you parse the Name to get the number value and convert it to an Integer:

    Public Sub img_click(ByVal sender As System.Object, ByVal e As MouseButtonEventArgs)
        Dim strIndex As String = CType(sender, Control).Name.Substring(4)
        Dim Index As Integer
        If Integer.TryParse(strIndex, Index) Then
            RaiseEvent getOption(Index - 1)
        End If
    End Sub

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial