访问mdi运行时的方法 [英] accessing method of mdi runtime

查看:122
本文介绍了访问mdi运行时的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c#window application中将字符串转换为表单对象时访问mdi表单的方法

解决方案

在这里,我给你解释以计时器为例。



MDI子项在其MdiParent属性中引用其父项。此属性的类型为Form,因此必须将其强制转换为特定于mpst的类型才能访问该类型的成员。如果您的父窗体是Form1类型并且它有一个名为Timer1的Timer,那么您将从MDI子窗体启动该Timer,如下所示:

C#代码:



((Form1)this.MdiParent).Timer1.Start()



如果你关闭Option Strict,你可以不用转换,但这将使用后期绑定,因此Intellisense不会显示Timer1属性。选项Strict打开后,因为它应该总是没有充分的理由不这样做,代码将无法在没有演员的情况下编译。



如果您的子表格不是MDI孩子然后你需要自己给它一个父表格的引用。



另外,我总是建议所有控件都被声明为Private(默认为Friend)并且如有必要,您可以通过表单的公共属性和方法提供访问权限。例如,在这种情况下,您将使Timer成为私有,然后提供公共方法来启动和停止它。那样。你的MDI孩子有足够的权限去做他们需要的东西,但仅此而已。


输入t = Type.GetType(namespace+MdiFormName);



MethodInfo方法= t.GetMethod(CalluserRights);



表格c = Activator.CreateInstance (t)作为表格;



//将调用MDI的CalluserRights方法并返回数据表

object datatable = method.Invoke(c,空);

how to access methods of mdi forms while convert string to form object in c# window application

解决方案

Here, i give you explanation with taking timer as example.

An MDI child for has a reference to its parent in its MdiParent property. This property is of type Form, so you must cast it to its mpst specific type to get access to members of that type. If your parent form is of type Form1 and it has a Timer named Timer1, you would start that Timer from an MDI child form like this:
C# Code:

((Form1)this.MdiParent).Timer1.Start()

If you have Option Strict turned Off, you can do it without the cast, but that will be using late-binding so Intellisense will not show you the Timer1 property. With Option Strict turned On, as it should always be without good reason not to, the code will not compile without the cast.

If your child form is not an MDI child then you need to give it a reference to the parent form yourself.

Also, I always recommend that all controls be declared Private (the default is Friend) and that you provide access if necessary via public properties and methods of the form. For instance, in this case you would make the Timer private and then provide public methods to start and stop it. That way. your MDI children have enough access to do what they need to but nothing more.


Type t = Type.GetType("namespace" + "MdiFormName");

MethodInfo method = t.GetMethod("CalluserRights");

Form c = Activator.CreateInstance(t) as Form;

//will call CalluserRights method of MDI and return datatable
object datatable = method.Invoke(c, null);


这篇关于访问mdi运行时的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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