C
crferguson
I just wanted to share a solution I found to performing a dynamic
image swap on a group of imagebuttons in a webcontrol. The scenario
is I have a webcontrol with nothing but a table containing one column
of imagebuttons in it. This wil be a typical button column for a
vertical menu like on the left side of this site: http://www.nsr-inc.com
This code, when set to run in Form_Load, figures out the names of the
images to use in the image swap and then adds the onmouseover /
onmouseout attributes dynamically. Then, if you add or remove buttons
in the future, so long as you stick to the naming convention needed by
the code, you won't have to re-code each button to use specific
images.
The image naming convention to use for the button images should be
whatevernameyouwant + 01.gif and whatevernameyouwant + 02.gif (ex:
buttonup01.gif, buttondown02.gif). Next, make sure the "src='../
images/" part is set to wherever you're keeping your button images
relative to the web control. Last, add the AddAttributes call in the
Form_Load sub.
=================
Private Sub AddAttributes()
Dim sImage As String
For Each ctl As Control In Me.Controls
If TypeOf ctl Is ImageButton Then
sImage = GetImageName(DirectCast(ctl,
ImageButton).ImageUrl)
DirectCast(ctl,
ImageButton).Attributes.Add("onmouseover", "src='../images/" & sImage
& "02.gif'")
DirectCast(ctl,
ImageButton).Attributes.Add("onmouseout", "src='../images/" & sImage &
"01.gif'")
End If
Next
End Sub
Private Function GetImageName(ByVal ImageURL As String) As String
Dim intX As Integer = InStr(ImageURL, "/")
GetImageName = ImageURL
Do Until InStr(GetImageName, "/") = 0
GetImageName = Mid(GetImageName, InStr(GetImageName, "/")
+ 1)
Loop
GetImageName = Mid(GetImageName, 1, Len(GetImageName) - 6)
End Function
========================
image swap on a group of imagebuttons in a webcontrol. The scenario
is I have a webcontrol with nothing but a table containing one column
of imagebuttons in it. This wil be a typical button column for a
vertical menu like on the left side of this site: http://www.nsr-inc.com
This code, when set to run in Form_Load, figures out the names of the
images to use in the image swap and then adds the onmouseover /
onmouseout attributes dynamically. Then, if you add or remove buttons
in the future, so long as you stick to the naming convention needed by
the code, you won't have to re-code each button to use specific
images.
The image naming convention to use for the button images should be
whatevernameyouwant + 01.gif and whatevernameyouwant + 02.gif (ex:
buttonup01.gif, buttondown02.gif). Next, make sure the "src='../
images/" part is set to wherever you're keeping your button images
relative to the web control. Last, add the AddAttributes call in the
Form_Load sub.
=================
Private Sub AddAttributes()
Dim sImage As String
For Each ctl As Control In Me.Controls
If TypeOf ctl Is ImageButton Then
sImage = GetImageName(DirectCast(ctl,
ImageButton).ImageUrl)
DirectCast(ctl,
ImageButton).Attributes.Add("onmouseover", "src='../images/" & sImage
& "02.gif'")
DirectCast(ctl,
ImageButton).Attributes.Add("onmouseout", "src='../images/" & sImage &
"01.gif'")
End If
Next
End Sub
Private Function GetImageName(ByVal ImageURL As String) As String
Dim intX As Integer = InStr(ImageURL, "/")
GetImageName = ImageURL
Do Until InStr(GetImageName, "/") = 0
GetImageName = Mid(GetImageName, InStr(GetImageName, "/")
+ 1)
Loop
GetImageName = Mid(GetImageName, 1, Len(GetImageName) - 6)
End Function
========================