[英]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.