Pass Procedure Name?

  • Thread starter Thread starter Mike Hoff
  • Start date Start date
M

Mike Hoff

Hello,

I am wondering if it is possible to pass a procedure name to another proc.
I have been looking into delegates, but cannot seem to get the code correct.
Basically what I have is a proc to add handlers to various controls on a
form. basically each control would get mostly the same handlers, but there
is a particular handler that would vary based on the parent on the control
in question. What I would like to do is something like below. The problem
is in the form load - calling the routine and passing it the name of another
proc.


Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'add panel 1 handlers
AddHandlers (pnlOne, ProcOne)

'add panel 2 handlers
AddHandlers (pnlTwo, ProcTwo)

'add panel 3 handlers
AddHandlers (pnlThree, ProcThree)

'add panel 4 handlers
AddHandlers (pnlFour, ProcFour)

End Sub


Public Sub AddHandlers(ByVal ctrlIn As Control, ChangeProc as ?????)
'add the proper event handlers to each control on a form
'ChangeProc would be either ProcOne, ProcTwo, ProcThree or ProcFour
' depending on what was passed in

Dim ctrlCur As Control

For Each ctrlCur In ctrlIn.Controls

If (TypeOf ctrlCur Is TextBox) Then
AddHandler ctrlCur.Enter, AddressOf txtBoxEnter
AddHandler ctrlCur.Leave, AddressOf txtBoxLeave
AddHandler ctrlCur.TextChanged, AddressOf ChangeProc

'etc...

End If
Next

End Sub

(NOTE the ProcOne etc code is more complex, this is just an example)

Private Sub ProcOne

cmdOne.Enabled = True
cmdTwo.Enabled = False
cmdThree.Enabled = False
cmdFour.Enabled = False

End Sub

Private Sub ProcTwo

cmdOne.Enabled = False
cmdTwo.Enabled = True
cmdThree.Enabled = False
cmdFour.Enabled = False

End Sub

Private Sub ProcThree

cmdOne.Enabled = False
cmdTwo.Enabled = False
cmdThree.Enabled = Three
cmdFour.Enabled = False

End Sub

Private Sub ProcFour

cmdOne.Enabled = False
cmdTwo.Enabled = False
cmdThree.Enabled = False
cmdFour.Enabled = True

End Sub
 
Mike Hoff said:
I am wondering if it is possible to pass a procedure name to another proc.
I have been looking into delegates, but cannot seem to get the code
correct.
Basically what I have is a proc to add handlers to various controls on a
form. basically each control would get mostly the same handlers, but
there
is a particular handler that would vary based on the parent on the control
in question. What I would like to do is something like below. The
problem
is in the form load - calling the routine and passing it the name of
another
proc.


Private Sub frmTest_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'add panel 1 handlers
AddHandlers (pnlOne, ProcOne)

'add panel 2 handlers
AddHandlers (pnlTwo, ProcTwo)

'add panel 3 handlers
AddHandlers (pnlThree, ProcThree)

'add panel 4 handlers
AddHandlers (pnlFour, ProcFour)

End Sub


Public Sub AddHandlers(ByVal ctrlIn As Control, ChangeProc as ?????)

'As [Delegate]'...

\\\
AddHandler ctrIn.Enter, ChangeProc
///
 
Mike,
In addition to the other comments.

You want to pass the address of the proc to AddHandlers, then you simply
want to use this address on AddHandler.

So when you call AddHandlers you need to use AddressOf:
'add panel 1 handlers
AddHandlers (pnlOne, AddressOf ProcOne)

With Option Strict On, you need the specific type of Handler being passed,
TextChanged matches EventHandler, so that is the one to use.
Public Sub AddHandlers(ByVal ctrlIn As Control, ChangeProc as
EventHandler)
'add the proper event handlers to each control on a form

Seeing as ChangeProc is already an address, you can simply use it:
AddHandler ctrlCur.TextChanged, ChangeProc

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top