我需要从Dll调用主项目中的子例程是否可能如果是,如何? [英] I need To Call a Sub Routine present in the Main Project from a Dll is that possible if yes how?

查看:56
本文介绍了我需要从Dll调用主项目中的子例程是否可能如果是,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过Dll调用主项目中的子程序是否可能如果是的话?



i有一个

项目A中的A类



Public Sub EnableButton(Byval benable as bool)

btnSave.Enabled = benable

结束子



i需要从导入的DLL ProjectB中调用此子程序



如何做到这一点请在VB.net中给出一些代码



反向这里我在Dll库类中有一个按钮事件,点击我需要启用的dll类库中的按钮或者禁用主项目表单中的按钮

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

推荐答案

您已经描述了对回调机制的需求。这可以通过使用代表来实现。



请阅读以下有关代表的信息:

1. 代表 [ ^ ]

2. 行动代表 [ ^ ]



假设以下课程是在你的DLL项目B中。在这个例子中,我使用了EnableButton方法的方法签名。

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

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


我解决了它自己
DLL类中的


声明了一个事件

公共事件ButtonClick As EventHandler

并将它拉到Button Click事件中



Private Sub btnPSMSave_Click (ByVal sender As System.Object,ByVal e As System.EventArgs)处理btnPSMSave.Click

RaiseEvent ButtonClick(Me,e)

End Sub



然后在主项目表格中



i处理负载

AddHandler ucDetailPanePSM.ButtonClick,AddressOf UserControl_ButtonClick



受保护的子UserControl_ButtonClick(ByVal发送者为对象,ByVal e As EventArgs)

'处理事件

btnSave.Enabled = True

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


这篇关于我需要从Dll调用主项目中的子例程是否可能如果是,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆