今天做了个发送消息的例子。下面把代码贴出来大家探讨探讨。
设计一个类库,里面2个文件。Message.vb ,Notify.vb
'Message.vb 代码 Namespace Notification Public Interface IPJMessage '定义一个接口 Function ToHTML() As String '向接口中添加发送方法 Function SubJect() As String '添加 End Interface Public Class Message End Class Public Class ThirdClass : Implements IPJMessage Public Function SubJect() As String Implements IPJMessage.SubJect SubJect = "Send" '函数返回一个Send字符串 End Function Public Function ToHTML() As String Implements IPJMessage.ToHTML ToHTML = "下午好!" '函数返回一个下午好字符串 End Function End Class End Namespace
'Notify.vb代码 Imports EdmLib Namespace Notification Public Class Notification Dim duser As IEdmUser7 Public Sub New(ByVal user As IEdmUser7) duser = user '把传入的用户赋值给IEdmUser7 End Sub ''' <summary> ''' 发送消息 ''' </summary> ''' <param name="context">IPJMessage实例</param> ''' <remarks></remarks> Public Sub SendMessage(ByVal context As IPJMessage) duser.SendMsg(context.SubJect(), context.ToHTML()) '调用duser的SendMsg方法 End Sub End Class End Namespace
类库中的文件编译成dll 导入到应用程序中。添加一个窗体,窗体上添加一个button 按钮 和一个combobox控件
'窗体的Load事件代码 Private Sub frmTestBatchList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load vault.LoginAuto("Trainning", Me.Handle.ToInt32) '登陆PDM Dim userMgr As IEdmUserMgr5 '定义UserMgr5类型对象 userMgr = vault Dim pos As IEdmPos5 pos = userMgr.GetFirstUserPosition '获得第一个用户 Dim user As IEdmUser5 While Not pos.IsNull user = userMgr.GetNextUser(pos) '获得用户 ComboBox1.Items.Add(user.Name) '添加用户到comboBox End While End Sub'按钮事件的代码 Private Sub btnSendMsg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendMsg.Click Dim userMgr As IEdmUserMgr5 userMgr = vault Dim user As IEdmUser5 user = userMgr.GetUser(ComboBox1.Text) '获取ComboBox中所选中的用户 Dim smessage As IPJMessage = New ThirdClass() Dim noti As Notification noti = New Notification(user) '传user到Notification类的构造函数 noti.SendMessage(smessage) '发送消息 End Sub