简体   繁体   English

如何使用dll将消息发送到表单?

[英]how can I send a message to a form using a dll?

I am developing an gui application in c# that uses a dll. 我正在使用dll在c#中开发一个gui应用程序。 This dll contains several common functions, like validation of properties. 该dll包含几个常用功能,例如属性验证。

However, I need to validate the properties of an object with a function like this: 但是,我需要使用以下函数来验证对象的属性:

public static bool validate(MyObject object)
{

  bool success = true;

  // some validation
  if(!valid(object.property))
  {
    // log to database, this works
    log("property is not valid"); // log to database, this works

    // how can I do this?
    sendMessageToForm("property is not valid");

    success = false;
  }

  return success;

}

Additionally, validate() must return a boolean value, not a string of messages 此外,validate()必须返回布尔值,而不是消息字符串

how can I send a message to a form through a dll? 如何通过dll将消息发送到表单?

您需要在项目中添加dll的引用,之后再在类中添加dll的名称空间,稍后再声明该类的对象并调用validate消息并传递用于validate的参数。

If what you have shown here is the code inside your dll, I'm assuming you want to update the form from inside your dll. 如果您在此处显示的是dll内部的代码,则假定您要从dll内部更新表单。 The way to do that is for your dll class to publish an event, let's say ValidationError event, that will also carry with it as EventArgs the message you want to display on the form. 这样做的方法是让您的dll class发布一个事件,例如ValidationError事件,该事件还将随EventArgs一起携带您要在表单上显示的消息。

Then, on your form, where you instantiate the object from your dll class , you would also have to attach a listener for that event. 然后,在窗体上,从dll class实例化对象,还必须为该事件附加一个侦听器。

MyClassInDll myObject=new MyClassInDll();
myObject.ValidationError+=new EventHandler<ValidationErrorMessage>(YourMethodThatDisplaysTheMessagesOnTheForm);

Then you can use the object to call if (myObject.Validate(someOtherObject)) which will return a boolean , while still triggering the event that sends the error messages to your form. 然后,您可以使用该对象来调用if (myObject.Validate(someOtherObject)) ,该方法将返回boolean ,同时仍触发将错误消息发送到表单的event

public static bool validate(MyObject object, out message)

这将为您提供输出消息字符串,可以从调用validate的位置进行设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用端口从服务器向客户端发送消息? - How I can send message from server to client using the port? 如何使用Java发送标头消息? - How can I send header message in Java? 如何在应用程序中隐藏DLL注册消息窗口? - How can I hide DLL registration message window in my application? 我如何知道是否已启动并打开了DLL中的对话框形式? - How can I know if a dialog form from a DLL is launched and is open? 如何使用user32.dll的SendMessage向控制台应用程序发送正确的消息 - How to send a proper message to Console application using user32.dll's SendMessage 我如何使用asp.net signalR向特定组中的特定用户发送消息 - how can I send message to specific user in specific group using asp.net signalR 如何使用 asp.net 和 c# 发送电报消息? - How can I send telegram message using asp.net and c#? 如何使用YAHOO IM SDK向MSN联系人发送消息 - How can I send message to MSN contact using YAHOO IM SDK 如何在不使用 Azure SignalR 服务的情况下从 Azure Function 发送 SignalR 消息 - How can I send a SignalR message from an Azure Function without using Azure SignalR Service 如何在不使用SendMessage的情况下向.Net中的另一个进程发送“消息”? - How can I send a “Message” to another process in .Net, without using SendMessage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM