Results 1 to 8 of 8

Thread: Click event of a dynamically created PictureBox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    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:
    1. Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As _
    2.       System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
    3.         Dim newpic As New PictureBox
    4.         newpic.Image = System.Drawing.Image.FromFile _
    5.             ("red circle.bmp")
    6.         newpic.SizeMode = PictureBoxSizeMode.AutoSize
    7.         newpic.Left = e.X - newpic.Width / 2
    8.         newpic.Top = e.Y - newpic.Height / 2
    9.         Me.Controls.Add(newpic)
    10.     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?

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Click event of a dynamically created PictureBox

    You can add an event handler at runtime to handle the click event.
    VB Code:
    1. Private Sub CustomPic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    2.         'your code for handling the event
    3.     End Sub
    And then in your code for adding a new picture you can write AddHandler statement
    VB Code:
    1. Dim newpic As New PictureBox
    2.         AddHandler newpic.Click, AddressOf CustomPic_Click
    .

    Take a look at AddHandler in your MSDN
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Click event of a dynamically created PictureBox

    Also when you initialise your picturebox you should do it like this:

    VB Code:
    1. Dim withevents temp as picturebox = new picturebox
    2. ...
    3. AddHandler temp.Click, AddressOf CustomPic_Click
    4. MyPicArray(x) = temp

    There are a few old tutotials all about this in the codebank.
    I don't live here any more.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    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?

  5. #5
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

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

    My Articles

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    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.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    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?
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width