How to create Event Handler for Dynamically added control

  • Thread starter Thread starter Amit D.Shinde
  • Start date Start date
A

Amit D.Shinde

I am adding a new picturebox control at runtime on the form
How can i create click event handler for this control
Amit Shinde
 
Amit D.Shinde said:
I am adding a new picturebox control at runtime on the form
How can i create click event handler for this control

You can not create a handler at runtime, you have to write it at designtime.
You can _attach_ an event handler to an event: Addhandler myobject.theevent,
addressof handler
 
Use AddHandler

ie.

Addhandler Picturebox1.Click, Addressof
MethodorDelegateThathandlesclickandhassamesigniture


..com
 
Armin,

hate to disagree with you here, but...

You can create a new delegate at runtime using the delegate class (it gets
tricky, but you can), and then you can bind events etc to the delegate to a
target method. And if you were so inclined (even though it seems like
overkill and no *real* use) you could

1) Load an assembly to grab an event out of...
2) Get the signiture of the event you want to hook (each event in .NET
automatically creates a delegate class at runtime, you have to watch your
assembly in debug to see it, but if you iterate through getTypes() you'll
see that there is a delegate created for the event.
3) Use CodeDom to create a class or whatever.
4) compile into a DLL
5) reference the newly created assembly
6) grab the method, use the delegate creator to hook the event and method.
7) Add Handler. =)

Now this is saying you do everything dynamically (including the creation of
a method). BUt you can simply hook to another method that matches the
signiture (depending on how strict you make it,you can check basetypes as
well to match sig.) And you can do this all dynamically at runtime. =)

-CJ
 
CJ Taylor said:
Armin,

hate to disagree with you here, but...

You can create a new delegate at runtime using the delegate class (it
gets tricky, but you can), and then you can bind events etc to the
delegate to a target method. And if you were so inclined (even
though it seems like overkill and no *real* use) you could

1) Load an assembly to grab an event out of...
2) Get the signiture of the event you want to hook (each event in
.NET automatically creates a delegate class at runtime, you have to
watch your assembly in debug to see it, but if you iterate through
getTypes() you'll see that there is a delegate created for the
event.
3) Use CodeDom to create a class or whatever.
4) compile into a DLL
5) reference the newly created assembly
6) grab the method, use the delegate creator to hook the event and
method. 7) Add Handler. =)

Now this is saying you do everything dynamically (including the
creation of a method). BUt you can simply hook to another method
that matches the signiture (depending on how strict you make it,you
can check basetypes as well to match sig.) And you can do this all
dynamically at runtime. =)

Um....yes....you are right - but _I_ still prefer to write applications
at design time, not at run time. ;-)
 
how's that application going anyway cj?


CJ Taylor said:
Armin,

hate to disagree with you here, but...

You can create a new delegate at runtime using the delegate class (it gets
tricky, but you can), and then you can bind events etc to the delegate to a
target method. And if you were so inclined (even though it seems like
overkill and no *real* use) you could

1) Load an assembly to grab an event out of...
2) Get the signiture of the event you want to hook (each event in .NET
automatically creates a delegate class at runtime, you have to watch your
assembly in debug to see it, but if you iterate through getTypes() you'll
see that there is a delegate created for the event.
3) Use CodeDom to create a class or whatever.
4) compile into a DLL
5) reference the newly created assembly
6) grab the method, use the delegate creator to hook the event and method.
7) Add Handler. =)

Now this is saying you do everything dynamically (including the creation of
a method). BUt you can simply hook to another method that matches the
signiture (depending on how strict you make it,you can check basetypes as
well to match sig.) And you can do this all dynamically at runtime. =)

-CJ
 
Getting there. A great deal of the framework is done, I do have dynamic
event hooking completed which is really the core of it. Ironically I have
been documenting the whole thing too, which is a suprise for me... because I
usually don't. But figured if I'm going to hand this out to anyone, they
gotta see what I'm thinking with it.

But yeah, its getting there, I wanted to write a few more modules to test it
before I let it out on the public. Or the private public that is =)
Working with a user security module right now, and this is the thing I'm
struggling with, not because its hard, but I don't know what would be the
best way.

I could embed it into the framework and make things much easier. But that
just seems to defeat the purpose of a 100% scalable app (plus I have to redo
some of my dynamic event handling... forget that...) So I'm trying a few
things and see where it goes. I wanted to deliver the framework and a few
samples of how it works before I put it out.

So hopefully soon. I keep getting backlogged with old VB6 at work, these
things happen, so I've been off it for like 3 days. Blah.

The one thing I'm suprsed at is it is incredibly light weight and seems to
have some enourmous potential from what I see. Now obviusly I want to make
it more dynamic, but I think the most important thing is the visual event
linker I built. Which should help out with development because it handles
ALL event handling between plugins for you. And it's just implementing a
single interface.

SoThanks for asking, that actually felt good that someone rememberd. =)

I'll keep you informed.

CJ
steve said:
how's that application going anyway cj?


CJ Taylor said:
Armin,

hate to disagree with you here, but...

You can create a new delegate at runtime using the delegate class (it gets
tricky, but you can), and then you can bind events etc to the delegate
to
 
If we could all only live in that perfect world where our applications where
next to godliness (which of course is next to cleanliness) and no one ever
wanted to create a new module or snapin to enhance the business logic.

I want to be there............


Buuuuuutttttt... not going to happen, so I work with the masses..







Oh yeah... I like doing it at design time too...
 
are you kidding...you're working on something ver atl-ish AND
5gl-ish...kinda hard not to remember. what you've done w/ vb.net so far is
nearly impossible to do in vb6. i am compelled to remember and ask! this may
be your "dilbert-dead-possum" (explanation available upon request) project
but i find it fascinating.

do keep us posted!

steve
 
steve said:
are you kidding...you're working on something ver atl-ish AND
5gl-ish...kinda hard not to remember.

I'm familar with atl, what is 5gl?
what you've done w/ vb.net so far is
nearly impossible to do in vb6. i am compelled to remember and ask!

True...... =)
this may
be your "dilbert-dead-possum" (explanation available upon request) project
but i find it fascinating.

And explanation is requested. =) I think I have the jist of where its
going, but would still like to hear it. =)

-CJ
do keep us posted!

For sure. =)
 
fifth generation language. currently, we are considered 4gl. your app
basically interprets english and intuitively "knows" the intent of the
speaker/writer then takes appropriate action? the closer an language gets in
being written as one would write to another human being, the higher it's gl
rank. your app gets pretty high marks if it does what i think your boss
required it to do.

best of luck,

steve
 
sorry...dilbert describes some corporate projects as "dead-possum"
projects...those projects that no one knows what to do with and don't want
them anywhere near their desk. one day a man went to clean his pool and
noticed a dead possum in the bottom of it. he didn't know what to do with it
and feared that if he just threw it in the trash, animal control w/b
notified and he get busted for animal cruelty...even though it probably just
died from slipping in the pool while trying to get a drink but couldn't get
out. so he gets the idea to just pitch it over the fence into his neighbor's
yard...his neighbor actually had a pool too in which the possum landed. the
next day, he goes to clean his pool again and there, in the bottom, is the
possum. apparently, the neighbor went through the same thought process and
came to the same conclusion. needless to say, each morning that possum was
at the bottom of his pool even though he pitched it over the fence upon
fishing it out.

dead-possum projects are the messy ones that keep finding their way back to
you. hopefully, you do get to pass this one off in the maintenance
cycle...it helps that you've provided documentation.

;^ )
 
Ahhh! Ok, sorry brain fart and couldn't remember what you were talking
about. But yes, I am aware of generation levels and ther einterpretation.
But I do appreicate you explaining it again (and I'm sure many others do
to).

That is the goal of this. Even though its not necessarily a language (or
maybe it is with an intelligent interface, I'm not sure) but it does provide
a lot of common functionality that you don't have to program. I suppose you
could compare it to something like IBS (and inherited forms) with a lot more
functionality between modules. Also, I use interfacing to do the
communication instead of object inheritance because I understand why
something like IBS uses Object inheritance, but it isn't really useful in a
windows evironment at least for this. That is understanding stateless vs
stateful connections (including UI).

I just looked at a problem, understand that my boss can't code at the same
level as me, but knows how he wants things to work. And instead of
re-releasing packages all the time (which we all know has its expenses) I
just wanted to plug a module in, link it to what I want, and go. That way,
no module has to know anything about another, but can be directly affected
by it, without knowing this at the time the program was written. So modules
could constantly communicate with new modules regardless of versioning,
programming, etc. And that way, even users that "know" what they want could
build powerful applications that perform a multitude of functions in a
fraction of the time for development. Or for that matter, the need to know
how to develop at all.

So far its working, I just get weary when something is going my way. =) The
thing that scares me is the idea was relativly simple, implementation was
diffucult, but once I got the hang of it, we were cool. It was just
understanding all levels of how programs could communicate with eachother
(not with remoting) without actually knowing who/what they were talking to.
I suppose it was a reflection or philosophy on how OOP works.

But he tried it, understood it right away and saw it working (sure, I have
some bugs to work out, but nothing major) he just was like "Wow... that was
easy... So these two dll's that dont know eachother exist, are talking to
eachother and influencing eachothers functionality through the framework?"

"Yep."

"cool."

-CJ
 
steve said:
sorry...dilbert describes some corporate projects as "dead-possum"
projects...those projects that no one knows what to do with and don't want
them anywhere near their desk. one day a man went to clean his pool and
noticed a dead possum in the bottom of it. he didn't know what to do with it
and feared that if he just threw it in the trash, animal control w/b
notified and he get busted for animal cruelty...even though it probably just
died from slipping in the pool while trying to get a drink but couldn't get
out. so he gets the idea to just pitch it over the fence into his neighbor's
yard...his neighbor actually had a pool too in which the possum landed. the
next day, he goes to clean his pool again and there, in the bottom, is the
possum. apparently, the neighbor went through the same thought process and
came to the same conclusion. needless to say, each morning that possum was
at the bottom of his pool even though he pitched it over the fence upon
fishing it out.

Ahhh yes, I know what your talking about... I think dilbert works at
Caterpillar...
dead-possum projects are the messy ones that keep finding their way back to
you. hopefully, you do get to pass this one off in the maintenance
cycle...it helps that you've provided documentation.

That was the goal of the documentation. So that a user / developer could
understand how to implement there own modules easily. And not in overused
techno-jargon either. That way, liek you said, in the maintenence cycle I
can pawn it off, and continue focusing on improvement rather than training.
Also, the other thing I was thinking of is I don't want to write these
modules. Sounds kooky huh? But to say that I could write every module for
every application that would provide a useful function is narcissitic and
wrong. =) I just think to the number of apps I've developed, the procedures
I've recreated that are similar but different enough, and just want a
starting point so that eventually, even applciations that were never
intended to be related, now easily could interact with eachother leading to
more productive business.

talk soon,
cj
 
But, to think that 1 23 year dude from Illinois could do this is a little
hard to believe as well. =)
 
Back
Top