如何访问控制在从其他形式的一个窗口的形式 [英] How to access a control in one window form from other form

查看:104
本文介绍了如何访问控制在从其他形式的一个窗口的形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主席先生,我有两个窗口的形式,每种形式都有一定的控制。我想从另一种形式访问一个形式的控制权。我已经尝试了两种ways-
1制作控制公共和访问它们。
2 - 让公共属性获取并根据需要设置控制。
,而在这两个情况下,我有为了访问属性或公共控制以创建第一形式的对象。我已经instaintiated一个套接字对象,并将其绑定到第一种形式的构造函数的本地端点。现在,如果我为了访问控制创建第一种形式的另一个目的,构造再次发射和插槽的同一代码在结合执行导致异常。请建议我该怎么办?
Thankssssss ....

Sir, I have two window forms, each form has some controls. I want to access control of one form from another form. I have tried two ways- 1- Make controls public and access them. 2- Make public properties getting and setting control according to requirement. But in both the case i have to create the object of first form in order to access the properties or public control. I have instaintiated a socket object and bind it to the local endpoint in the constructor of the first form. Now if I create another object of first form in order to access the control, constructor is fired again and the same code of socket binding in executed which results an exception. Please suggest me what to do ??? Thankssssss....

推荐答案

您可以通过多种方式做到这一点...

You can do this in many ways...

您可以声明一些静态的getter / setter管理compononent的静态实例:

You can declare some static getter/setter to manage a static instance of the compononent:

private static Type _myObject;
public static Type MyObject
{
   get
   {
      return _myObject;
   }
}

在这种情况下,你可以从evrywere访问它,如果你只需要一个特定的共享对象

In this case you can access it from evrywere if you only need a specific shared object

MyClass.MyObject.Function();



,也可以定义为全班同学一个getter:

Or you can define a getter for the whole class:

public class MyClass
{
   static MyClass _myClass;
   public static MyClass Instance { get { return _myClass; } }

   public MyClass()
   {
      _myClass = this;
      ...
   }

   public void Hello()
   {
      Console.WriteLine("CIAO!")
   }
} 

和让所有的方法和类的属性:

And getting all the methods and properties of the class:

MyClass.Instance.Hello();

您也可以通过在类的构造,性质或功能,但我不喜欢的方式..

You can pass also the class in a constructor, property or function, but i dislike this way...

这篇关于如何访问控制在从其他形式的一个窗口的形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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