Machaira Posted February 18, 2003 Posted February 18, 2003 Ok, hopefully this will make sense. :) I have a number of forms which call a Public procedure to display a form with a grid on it. The grid is populated with a dataset created in the procedure (via a function called from the procedure). What I want to do is pass a function pointer to a function on the form calling the procedure which will be called when the user double-clicks a row on the grid (confused yet ;)). Normally, an event handler would be set up to handle the double-click on the grid. What I can't quite figure out is passing a pointer to this event handler to the procedure and setting up the event handler in the procedure. Here's the procedure that shows the form: Public Sub ShowDataBrowser(ByVal sSQL As String, ByVal bNavigateToClickedRecord As Boolean, ByRef lClickEventHandle As Long) Dim oDataset As New DataSet() If GetDataset(sSQL, oDataset) > 0 Then Dim oBrowseForm As New DataBrowseForm(oDataset) If bNavigateToClickedRecord Then 'Add event handler for grid double-click event on DataBrowseForm End If oBrowseForm.ShowDialog() End If End Sub The last parameter in the declaration would be the function pointer to the event handler. It's declared as a Long as I don't quite know how to declare it. I've looked at Delegates and don't think they quite fit the bill here. I could be wrong however. :) Any advice would be appreciated. Quote Here's what I'm up to.
Cywizz Posted February 18, 2003 Posted February 18, 2003 Here goes: 1) Create a delegate declaration on the, e.g. form, and add a module level variable of the delegate type: Public Delegate Sub pointerToDelegate('enter params here) private toUseLaterPointer as pointerToDelegate 2) Overload the constructor on your form to pass a delegate Public Sub New(myPointer as pointerToDelegate) toUseLaterPointer = myPointer End Sub 3) Now you can fire this method anywhere you want, e.g. toUseLaterPointer('enter params here) The main thing is to decide where you will declare your delegate (e.g. on the form). Hope it helps and I understood your problem :) Quote Howzit??
Machaira Posted February 18, 2003 Author Posted February 18, 2003 Hmm, that's given me an idea as to how to go about it. Thanks! Quote Here's what I'm up to.
*Gurus* divil Posted February 18, 2003 *Gurus* Posted February 18, 2003 Use AddHandler. AddHandler oBrowseForm.myControl.myEvent, AddressOf myProcedure Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Machaira Posted February 18, 2003 Author Posted February 18, 2003 The problem with the AddHandler is how to pass a pointer to the handling procedure to the procedure that actually has that code. That's where I'm caught up at. Any ideas? Quote Here's what I'm up to.
Machaira Posted February 18, 2003 Author Posted February 18, 2003 Attached is an example of what I'm trying to accomplish. Perhaps I'm going about it the wrong way. :) Any tips would be appreciated.delegates.zip Quote Here's what I'm up to.
*Gurus* divil Posted February 18, 2003 *Gurus* Posted February 18, 2003 Firstly, stop using modules, modules are bad! Secondly, I'm still not entirely clear what it is you're trying to do. I suspect it's much easier than doing what you've been trying, though. Can you explain the problem entirely from the top? Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Machaira Posted February 19, 2003 Author Posted February 19, 2003 Why do you think modules are bad? As for the problem I found a way around it. It means duplicating a couple of lines of code, but that's ok. What I was trying to do was have a procedure which took as a parameter a function pointer. The procedure displays a form with a datagrid. When a row in the datagrid is selected the function pointed to by the function pointer is called. The problem I was having was how to pass the function pointer to the procedure and hook it to the datagrid. I hope that explains it better. Quote Here's what I'm up to.
*Gurus* divil Posted February 19, 2003 *Gurus* Posted February 19, 2003 Yes, that does. Modules are bad OOP design and should never have been included in VB.NET. There is no need for them. Any miscellaneous functions should be grouped together as static (shared) members of a class. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
*Experts* Volte Posted February 19, 2003 *Experts* Posted February 19, 2003 I have one question involving Modules; when you start a new Console application, it gives you a standard module with a Sub Main() in it; is it possible to replace this with a class in some way, or is a module required in that case? Quote
*Gurus* divil Posted February 20, 2003 *Gurus* Posted February 20, 2003 Simply changing "Module" to "Class" and adding the "Shared" modifier on the default Sub Main is all you need to do in that case. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.