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 _
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.
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
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.
Use [code] source code here[/code] tags when you post source code.
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.
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?