Re: Click Event of a Dynamically added object

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Bruce Wood (brucewood_at_canada.com)
Date: 11/15/04


Date: 15 Nov 2004 13:56:59 -0800

Bob <Bob@discussions.microsoft.com> wrote in message news:<1ECF8809-EF77-4C5D-84D4-6E88C02C8476@microsoft.com>...
> I have an Image object that is dynamically created in a tabcontrol 1 or many
> times. The Image is a custom class that has many mouse functions. I have
> trapped the LeftMouseDown and call an overrideable function called SelectMe()
> withine the Custom Image class. Ok here is where I am unsure. I need to
> be able to click on any of the one to many Images and bring that Image up in
> a Larger Edit area. My problem is that don't know the best way to be able to
> get that Click back to the Form Level so I can bring the image and its custom
> properties up in the larger Image area which is dynamically created at the
> form level. Is my concept correct? if so how do I override the SelectME()
> function at the form level? I assume it is somehere around where I add the
> Object to the TabPageControls(). Both the 1 or many images and the Edit
> Image are based on the same Custom Image Object I created. Ideas would be
> much appreciated. I am working in VB .Net

I just finished doing exactly this, but in C#, not VB. My VB is
terrible, so I'll advise you in prose rather than in code.

Your concept is rougly correct. As you add each Image object to the
container, you must register a method in the container to handle the
Click event of the Image. So, you have a Click event handler in your
Form class, and for every Image you add to the tab control (or
wherever), you say something like:

newImage.Click += new System.EventHandler(this.Image_Click);

where this.Image_Click is a method in your Form class. Yes, I know
that's C# (sorry), but I hope that you get the idea.

Inside the Image_Click method you'll receive one argument that is the
object that sent the event. This will be the Image that was clicked.
So, now the Image_Click method just has to do whatever it wants to do
when an image is clicked, since it has a reference to the clicked
Image passed to it.

So, to sum up:

o Write a method in your Form that responds to the Click event, but
don't hook it up to anything on your form. Just give it the correct
return type and arguments for the kind of method that handles a Click
event. Let's call the method Image_Click().
o When you create a new Image and add it to your tab control, add your
form's Image_Click method as a listener to the Click event of the
image.
o In the Image_Click method, the "sender" object will be the Image
that was clicked. Just cast it from an object to an Image, and do
whatever needs to be done.
o Image_Click will be called whenever any image is clicked.



Relevant Pages

  • RE: Click Event of a Dynamically added object
    ... One way you could do this is to use the TopLevelControl property of your ... Image classes (assuming that you have inherited from Control). ... The Image is a custom class that has many mouse functions. ... > get that Click back to the Form Level so I can bring the image and its custom ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Click Event of a Dynamically added object
    ... The Image is a custom class that has many mouse functions. ... get that Click back to the Form Level so I can bring the image and its custom ... Image are based on the same Custom Image Object I created. ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Set Statement vs Function
    ... So I guess I could create and throw a custom ... exception object in my Set statement, then catch it at the form level. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Control Access Rights
    ... Joe Richards Microsoft MVP Windows Server Directory Services ... Access Rights) in ADAM. ... I've walked through the procedure of creating a custom right and granting a user that right on an AD container. ...
    (microsoft.public.windows.server.active_directory)
  • Function that return System.Web.UI.Control[]
    ... It's a custom vb function that i call in C# that is defined as ... Public Function FindControlsByID(ByVal container As ... Prev by Date: ...
    (microsoft.public.dotnet.languages.csharp)