Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need To Call a Sub Routine present in the Main Project from a Dll is that possible if yes how?

i have a
Class A in ProjectA

Public Sub EnableButton(Byval benable as bool)
btnSave.Enabled=benable
End Sub

i need to call this subroutine from the Imported DLL ProjectB

How To do This please give some code in VB.net

reverse here i have a button event in Dll library class where by clicking on the button in the dll class libary i need to enable or disable the button in the main project form
Posted
Updated 26-Jan-14 20:29pm
v2
Comments
Richard MacCutchan 22-Jan-14 7:13am    
You need to provide the address of the subroutine to the DLL by using a public event in the DLL that the main application can subscribe to.
Madhukumar N 27-Jan-14 2:29am    
Thanks for the reply but the thing is reverse here i have a button event in Dll library class where by clicking on the button in the dll class libary i need to enable or disable the button in the main project form

You have described the need for a callback mechanism. This can be achieved through the use of a Delegate.

Please read through the following for information on Delegates:
1. Delegates[^]
2. Action Delegate[^]

Assume that the following class is in your DLL Project B. In this example, I have used the method signature of your EnableButton method.
ASM
Public Class ClassCallBackTest
   ' Option 1, define your callback signature  as a method with a boolean paramter
   Public Delegate Sub CallBack(ByVal enable As Boolean)

   Private ConstructorPassedCallBack As CallBack ' local copy of Delegate passed in constructor

   Public Sub New(ByVal CallBack As CallBack)
      Me.ConstructorPassedCallBack = CallBack
   End Sub

   Public Sub CallTheCallBack()
      If ConstructorPassedCallBack IsNot Nothing Then ConstructorPassedCallBack(True)
   End Sub

   ' Option 2, pass then callback to the method needing to call it
   Public Sub CallACallBack(ByVal CallBack As CallBack, ByVal enable As Boolean)
      If CallBack IsNot Nothing Then CallBack(enable)
   End Sub

   ' Option 3, pass then callback as an Action(Of Boolean) to the method needing to call it
   ' This just a different way to define the required signature.
   Public Sub CallACallBack2(ByVal CallBack As System.Action(Of Boolean), ByVal enable As Boolean)
      If CallBack IsNot Nothing Then CallBack(enable)
   End Sub
End Class

Here is an example of the usage.
ASM
Public Class Form1
   Private Sub CallBackMethod(ByVal enable As Boolean)
      MsgBox("Enabled = " & enable.ToString())
   End Sub

   Private Sub CallBackMethod2(ByVal enable As Boolean)
      MsgBox("CallBackMethod2 called.  Enabled = " & enable.ToString())
   End Sub

   Private Sub CallBackMethod3(ByVal enable As Boolean)
      MsgBox("CallBackMethod3 called.  Enabled = " & enable.ToString())
   End Sub

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim test As New ClassLibrary1.ClassCallBackTest(AddressOf CallBackMethod)
      test.CallTheCallBack() ' use the constructor callback

      test.CallACallBack(AddressOf CallBackMethod2, False)

      test.CallACallBack2(AddressOf CallBackMethod3, True)
   End Sub
End Class
 
Share this answer
 
I Solved it Myself

in DLL Class declared a event
Public Event ButtonClick As EventHandler
and Rasied it in Button Click event

Private Sub btnPSMSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPSMSave.Click
RaiseEvent ButtonClick(Me, e)
End Sub

then in Main Project Form

i Handled it on load
AddHandler ucDetailPanePSM.ButtonClick, AddressOf UserControl_ButtonClick

Protected Sub UserControl_ButtonClick(ByVal sender As Object, ByVal e As EventArgs)
'handle the event
btnSave.Enabled = True
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900