By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,474 Members | 3,141 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,474 IT Pros & Developers. It's quick & easy.

Pass Procedure Name?

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Mike,

See this nice test from Jay where he shows that using a column as indexer is
the fastest to index a datarow/dataview item. However that is not important
for your question. In that test he uses (as I understand you well( what you
ask.

http://groups-beta.google.com/group/...505bbbff764679

Cor
"Mike Hoff" <mi**@home.com>

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

Nov 21 '05 #2

P: n/a
"Mike Hoff" <mi**@home.com> schrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3

P: n/a
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

"Mike Hoff" <mi**@home.com> wrote in message
news:7D*****************@fe03.lga... 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

Nov 21 '05 #4

P: n/a
Thanks for the responses. This is exactly what I needed and is working
perfectly!

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:u%****************@tk2msftngp13.phx.gbl...
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

"Mike Hoff" <mi**@home.com> wrote in message
news:7D*****************@fe03.lga...
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


Nov 21 '05 #5

P: n/a
Thanks for the responses. This is exactly what I needed and is working
perfectly!

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:u%****************@tk2msftngp13.phx.gbl...
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

"Mike Hoff" <mi**@home.com> wrote in message
news:7D*****************@fe03.lga...
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


Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.