如何从另一个类访问主要的Form组件 [英] how to access main Form component from another class

查看:100
本文介绍了如何从另一个类访问主要的Form组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi ..
我想知道如何从另一个类(不是form ...只是一个类)访问主要的Form组件...谢谢您

hi..
i want to know how to access main Form component from another class(not form...just a class)...thank you

推荐答案

通常,这是Application.OpenForms[0](您需要将其强制转换为派生的表单类型).或者,您可以将对主表格的引用存储在静态变量中.

[更新]
-----------

为了回应您的评论,请参见以下示例代码:

Typically, this would be Application.OpenForms[0] (You will need to cast it to the derived form type). Alternatively you could store a reference to the main form in a static variable.

[Update]
-----------

In response to your comment, see this example code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
}

public class Class1
{
  public Class1()
  {
    Form1 mainForm = (Form1)Application.OpenForms[0];
  }
}


我认为最可靠的另一种方法是使用接口.

请在此处查看我的解决方案和其他解决方案:
如何在列表框之间复制所有项目两种形式 [ ^ ].

—SA
Another way which I think is the most robust is using interfaces.

Please see my Solution and other solutions here:
How to copy all the items between listboxes in two forms[^].

—SA


这是来自类的
This is the from class
<pre lang="cs">namespace WindowsFormsApplication
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}



这是另一个名为Class1 ...的类.



This is the other class named Class1...

<pre lang="midl">namespace WindowsFormsApplication
{
    class Class1
    {
    }
}



这是一个例子,我想从class1以形式访问oblect(listbox)...如何?



this is the exemple, i want access to oblect(listbox) in form, from class1 ...how?


这篇关于如何从另一个类访问主要的Form组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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