|
-
Jan 24th, 2011, 09:43 AM
#1
Thread Starter
New Member
[RESOLVED] Clickable pictureboxes that were added dynamically
Hello,
I am working a small project where I would like the user to be able to add images to a form, specifying a URL in the tag of pictureBox, so that when it is clicked. They are taken to the website they specified when adding the pictureBox.
I have a separate form for adding the URL and image so I need to pass those variables into the first form.
I can't seem to figure out how to access the Tag property of the individual pictureBox when the user clicks it. Here is my pictureBox creation code which is in the second form:
Code:
Dim tool As String
Dim ptext As String
Dim url As String
url = TextBox2.Text
ptext = TextBox1.Text
tool = TextBox3.Text
Dim pb As PictureBox = New PictureBox()
pb.BackColor = Color.Transparent
pb.Image = System.Drawing.Image.FromFile(ptext)
pb.SizeMode = PictureBoxSizeMode.AutoSize
pb.Visible = True
pb.Tag = url
Form1.Controls.Add(pb)
Form1.FlowLayoutPanel1.Controls.Add(pb)
AddHandler pb.Click, AddressOf Form1.Link_Click
In order to have the new image linked to the PictureBox click method.
Here's the Link_Click event code (in the first form) I have if it helps:
Code:
Public Sub Link_Click(ByVal url As String)
Dim theWebSite As String
theWebSite = url
Call Shell("explorer.exe " & theWebSite, vbNormalFocus)
End Sub
Any help is greatly appreciated. Thank you in advance.
-
Jan 24th, 2011, 10:20 AM
#2
Re: Clickable pictureboxes that were added dynamically
The signature of your Link_Click is wrong. It should match the event procedure declaration of PictureBox.Click to add the click handler.
Usually event procedures will have a signature something similar to this:
Code:
Private Sub ControlName_EventName(ByVal sender As System.Object, ByVal e As System.EventArgs)
To add a handler, the easiest way is to put this line in declaration section of your form:
Code:
Dim WithEvents pb as PictureBox
And Then select pb from the left dropdown and Click from the right dropdown in code window.
This will add the signature of the picturebox click event like this:
Code:
Private Sub pb_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles pb.Click
End Sub
Now remove the "Dim WithEvents pb as PictureBox" declaration you added and the "Handles pb.Click" from this procedure. What is left is the event procedure with correct signature.
-
Jan 24th, 2011, 10:23 AM
#3
Re: Clickable pictureboxes that were added dynamically
And for the rest of the answer:
As long as you are using the correct signature for the event handler, which you have to do anyways, then the sender variable IS the picturebox, so you would do this to get the tag:
Dim theURL = DirectCast(sender,PictureBox).Tag.ToString
My usual boring signature: Nothing
 
-
Jan 24th, 2011, 10:34 AM
#4
Thread Starter
New Member
Re: Clickable pictureboxes that were added dynamically
That worked perfectly. =)
So just so that I understand what's going on here:
- Signatures between forms and methods must match
- DirectCast allows me to inherit the properties of specific ( in this case, PictureBox ) control I'm dealing with.
Thanks again.
-
Jan 24th, 2011, 10:46 AM
#5
Re: Clickable pictureboxes that were added dynamically
Not quite. What is actually happening with events is that each object that can raise events keeps an internal list of methods it needs to call when the event occurs. When the event does occur, the object goes through that list calling each method in turn. When you use AddHandler (or the Handles clause), you are passing that method to the object to put onto its list. Obviously, the methods on that list all have to have the exact same signature, because otherwise the object that raised the event would have no way of knowing what arguments to pass to the method. In fact, it is the object that raises the event that dictates what arguments the methods must accept, because it is only going to pass certain things, and the methods had better be able to handle them. So you only get freedom as to what arguments are passed if YOU created the object and especially if YOU created the events that it will raise.
All objects (reference type objects, that is) are just the addresses in memory of the actual object. Therefore:
Dim obj As Object
obj doesn't hold the actual object, but just the address in memory of the actual object. Therefore, on a 32-bit system, you could say that obj is always 32 bits long, no matter what the object is, because it just needs to be large enough to hold an address (it might be longer, but it is certainly always a fixed size).
What DirectCast is doing is saying that the address doesn't point to an object of type Object, but actually points to an object of a different type. For DirectCast to work, the type that it is being cast to MUST inherit from the type that it is being cast from. Since every object derives from type Object (in .NET, anyways), then the cast I described will work. Of course, if you cast an object to something it isn't, for example, if you tried to cast Sender to a Button, when it is actually a PictureBox, you will get a runtime error, because Sender is NOT a Button.
If the object you are casting to is not derived from the one you are casting from, you can use CType, which is slower, and will also fail if you are doing a cast to a type that it is not, but in this case PictureBox is derived from Object (actually, there are a couple intermediary steps, such as Control, but that doesn't matter), so DirectCast is faster and better than CType.
My usual boring signature: Nothing
 
-
Jan 24th, 2011, 10:46 AM
#6
Re: Clickable pictureboxes that were added dynamically
 Originally Posted by Franneldort
That worked perfectly. =)
So just so that I understand what's going on here:
- Signatures between forms and methods must match
- DirectCast allows me to inherit the properties of specific ( in this case, PictureBox ) control I'm dealing with.
Thanks again.
When in doubt Google
DirectCast http://msdn.microsoft.com/en-us/libr...(v=vs.71).aspx
-
Jan 24th, 2011, 11:10 AM
#7
Thread Starter
New Member
Re: Clickable pictureboxes that were added dynamically
@Shaggy: Thanks, that helps a lot. I was actually trying to use CType to do this for awhile yesterday, but couldn't get it working. Clarification helps. =D
@Kevininstructor: Yeah, I already have that bookmarked, ha.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|