Reputation: 1766
I have made an application that allows users to add custom items to a menu overlay using an ini file to save the items on the menu.
I have a class for reading and saving ini files in my project.
To read the items contained in the ini file, I have used a for each
loop.
Here is my code to do this:
Dim newItem As New PictureBox
newItem.Image = Image.FromFile(item.GetKey("iconLocation").Value)
Me.Controls.Add(newItem)
newItem.SizeMode = PictureBoxSizeMode.StretchImage
newItem.Size = New Point(60, 60)
newItem.Location = New Point(QuickLaunchIcon.Location.X + distanceFromLaunchItem,
QuickLaunchIcon.Location.Y)
distanceFromLaunchItem = distanceFromLaunchItem + 72
doneItems.Add(item.Name)
As you can see, I declare a local variable named newItem. However, as the form adds multiple versions of this newItem, I cannot add a handler for each 'newItem' that can run a different sub so if I was to add a handler for newItem, all the instances of newItem would run the same sub on click. However, I would like each newItem to run a different sub on click.
Is there any way I can do this?
EDIT: I have been told how to do this but now I have run into another problem. All the pictureboxes run the same sub but I cannot differentiate which picturebox is being clicked not... Can anyone help me?
Upvotes: 1
Views: 86
Reputation: 26434
Each picture box can be assigned a Name
like this:
newItem.Name = "SomeName"
In your event handler you can branch by DirectCast(sender, PictureBox).Name
, choose either an If
, or a Select Case
, the latter is usually less code.
For a lot picture boxes, it's best to use a Dictionary(Of String, PictureBox)
and TryGetValue
in that. Regular String
comparison done in sequence is very slow on big volumes of data.
Upvotes: 2