Click event of a dynamically created PictureBox
Hi,
I'm designing a graphical asset management program for the organisation I work for so that we can keep track of all the computers etc. we have in various buildings. I'm developing in VS2005 and was thinking that something like having a main map displayed in the centre and giving the user the ability to add and remove assets simply by clicking on it would be the best way.
As a little prototype, I'm using the following code to create new asset images (using a red circle as a test):
VB Code:
Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
Dim newpic As New PictureBox
newpic.Image = System.Drawing.Image.FromFile _
("red circle.bmp")
newpic.SizeMode = PictureBoxSizeMode.AutoSize
newpic.Left = e.X - newpic.Width / 2
newpic.Top = e.Y - newpic.Height / 2
Me.Controls.Add(newpic)
End Sub
First off, this isn't ideal as there's no transparency, so the circle appears with its bounding white square. Secondly, what's the best way to capture the click event? You can't declare an array WithEvents, which would be a problem as there are going to be a lot of these things.
Any ideas?
Re: Click event of a dynamically created PictureBox
You can add an event handler at runtime to handle the click event.
VB Code:
Private Sub CustomPic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
'your code for handling the event
End Sub
And then in your code for adding a new picture you can write AddHandler statement
VB Code:
Dim newpic As New PictureBox
AddHandler newpic.Click, AddressOf CustomPic_Click
.
Take a look at AddHandler in your MSDN
Re: Click event of a dynamically created PictureBox
Also when you initialise your picturebox you should do it like this:
VB Code:
Dim withevents temp as picturebox = new picturebox
...
AddHandler temp.Click, AddressOf CustomPic_Click
MyPicArray(x) = temp
There are a few old tutotials all about this in the codebank.
Re: Click event of a dynamically created PictureBox
Thanks to both of you - I'd forgotten about AddHandler.
RE wossname: Why do I need the WithEvents keyword if I'm using AddHandler? Also, it tells me you can't create local variables using WithEvents. Is this 'temp' variable meant to be form-level?
Re: Click event of a dynamically created PictureBox
Quote:
Originally Posted by olamm2k
Thanks to both of you - I'd forgotten about AddHandler.
RE wossname: Why do I need the WithEvents keyword if I'm using AddHandler? Also, it tells me you can't create local variables using WithEvents. Is this 'temp' variable meant to be form-level?
WithEvents cannot be used inside a function or a sub. When we use WithEvents keyword you have to declare the object at a module level. And ya you are right, the temp varuable needs to be form-level when you use withevents with it.
But I personally use AddHandler where ever required.
Re: Click event of a dynamically created PictureBox
The whole point of using WithEvents is so that you can use the variable in a Handles clause. If you declare a class-level variable WithEvents then there is no need to use the AddHandler statement as you can just use the variable in a Handles clause then any object assigned to that variable will automatically be linked to the event handler. AddHandler is normally used for objects created at run time and not assigned to a variable declared WithEvents. AddHandler assigns an event handler to an object. WithEvents is used to assign an event handler to a variable, and thus any object that is assigned to that variable at any particular time.
Re: Click event of a dynamically created PictureBox
So what's the best way to go about this? I almost want a VB6-style control array, where each PictureBox has a unique index.
If I use AddHandler, I still need to identify the PictureBox clicked on as there will be additional information stored, such as the manufacturer. I can use 'sender.Visible = False' to hide the image, but I'm also going to have to remove it from the database.
1 Attachment(s)
Re: Click event of a dynamically created PictureBox
Okay, here's the latest idea (attached).
I have an Asset class with a (PictureBox) pic property and an (Integer) id property. I use the Tag property of the PictureBox to store the id, so any database work can be done using the 'sender.Tag' argument.
Any recommendations?
It doesn't resolve the transparency issue, of course. Maybe I should have the pic property as something other then a PictureBox?