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