Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delegate & events

Status
Not open for further replies.

MarcoMB

Programmer
Oct 24, 2006
61
IT
Can someone explain me the events/delegate system in visual basic? this method exists also in visual c++?
 
Well, delegates aren't used in VB6. You can ask that question in the .Net forum.

As for events, the business of implementing the Observer pattern (registering a listener, broadcasting messages to the listeners, etc.) is handled internally to VB6. Writing event handlers is simple--an event is simply a procedure that takes the following syntax:
Code:
Sub validControlName_ValidEventName([SpecifiedArg1, SpecifiedArg2, ...])
Now, to make that a bit simpler, open a new VB6 project. Place a command button on the form. Double click it. You'll open up a code window, with the proper event handler procedure syntax for the Click event of the command button.

So, in VB6, handling events is very easy, and also somewhat more under the hood than with other languages.

HTH

Bob
 
thanks for help...but i found some people talking about delegates related with ActiveX objects and UserControl, to share event from UserControl to ActiveX user , in other words we can share the userControl interface and interfaces of the other control types that make the ActiveX control so that the user could find those events in the final ActiveX, thanks to delegate mechanism...in VB6.0..it's true?
I haven't understood if it's related also to event declaration and RaiseEvent function...can u explain me?
 
nobody knows about activeX and delegates?
 
Yes, I know about ActiveX and delegates. In fact, I even tried to help you with them, but you ain't listening. :p

I will repeat it one more time: delegates aren't a part of VB6, they're a part of .Net. If you found some people talking about this in VB6, then they were fishing for red herring, or they had created their own routines to emulate the behavior of delegates in VB6. Delegates are basically typesafe pointers, and are new to .Net.

Feel free to research this on your own, if you choose not to take my word for it. Then, when you find out that I'm right, you might want to investigate event-based interaction between objects. To that end, you might find the code I put together in thread708-1296481 helpful. And, if you wish to go WAY down the rabbit hole, take a look at faq222-3666, which explains how to create a class to broker events between a usercontrol's consituent controls and a form containing a container that contains the control. (This happens in a DataRepeater control, so the FAQ uses that as an example, but there are plenty of circumstances where this technique can be used.)

HTH

Bob
 
sorry, i'm listening you [glasses]
i believe in what u say...i only say that i 've found a tutorial in VB 6.0 that explain a UserControl that passes events to ActiveX in a sort of "delegate model" and using RaiseEvent...example...
UserControl has resize events so pass it to ActiveX that hasn't Resize event from Extender object...so:

Public Event Resize()

Private Sub UserControl_Resize()

RaiseEvent Resize
End Sub

So selecting the activeX object in combobox compare resize Event...

Private Sub myActiveX_Resize()
//Handling
End Sub

sorry i'm new in VB 6.0 and i don't know .NET u can be patient with me... thanks a lot
 
Ok, a "sort of delegate model" I'll buy.

So, now you're giving an example of what I called "Event based interaction between objects." What you are actually doing here is what Microsoft calls exposing events of constituent controls to users of your control. You give a simple example. Let's give you a little more involved one, that should be a little more useful to you.

Assume this code is in a UserControl called (tip of the hat to strongm) HappyFish, with two text boxes, Text1 and Text2. You want to expose the KeyPress events of the two textboxes as separate events. So, you declare the events, and raise them from the keypress events inside the control. By doing this, you pass the events from inside the control to whatever is containing the control (typically a form).

Code:
Public Event T1_KeyPress(KeyAscii as Integer)
Public Event ImSoCute(ReallyIAm as Integer)

Private Sub Text1_KeyPress(KeyAscii as Integer)
RaiseEvent T1_KeyPress KeyAscii
End Sub

Private Sub Text2_KeyPress(KeyAscii as Integer)
RaiseEvent ImSoCute KeyAscii
End Sub

Now, you use the control on a form (let's say you name it HappyFish1), and want to interact with the keypress events of the two text boxes. The event handlers in your form would look like this:
Code:
 Sub HappyFish1_T1_KeyPress(KeyAscii as Integer)
'do whatever, for example prevent anything but 0-9
End Sub

Sub HappyFish1_ImSoCute(ReallyIAm as Integer)
'do whatever
End Sub
I used an offbeat name in the Text2 example to illustrate that the name can be anything you want. It doesn't have to relate in any way to the name of the event it's exposing. However, it's a good idea to relate them, so that users of your control will intuitively understand which constituent control your event is addressing.

HTH

Bob
 
thanks a lot for your explaining now i've understand very well...when i'll effort .NET probably will now the "true" delegate system...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top