Click Event of a Dynamically added object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
Hi Bob

One way you could do this is to use the TopLevelControl property of your
Image classes (assuming that you have inherited from Control). This will get
you the containing form for your control - let's say your form class is
called CustomForm, and it has a method you want to call like CustomMethod().
Then inside your SelectMe method in your control, you could do something like
this:

Dim myForm As CustomForm = CType(Me.TopLevelControl, CustomForm)
myForm.CustomMethod()

HTH

Nigel Armstrong
 
Bob said:
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.
 
Bob,

Not sure what you mean, but you know you cant override a function from an
external class. If you were creating another class from the base class, feel
free to override, but you cant at the form level.

In order to get information back from the object you can do 1 of 2 things...

#1) Create delegates and public events and use those to communicate back tot
he form (RaiseEvent in VB). Also remember to pass the "sender" so the form
knows what picture box the event was raised from.

#2) Second option, but for this I wouldnt recommend. Create a class or
interface with a method say "ClickMe". Pass the instance to a method on the
picturebox that takes the class (or interface) as a parameter. When the
picturebox wants to "notify" the instanced class of an event, it just calls a
method on the interface.


Hope I am understanding you on this one... Kinda tired on an early Tuesday
morning :).
 
Bruce, Just read your example... this is exacly what I did after purchasing a
book and doing a little reading. Thank you for responding. I plan my next
app in C# just so I know it as well.
 
Back
Top