从类获取Form1属性

发布于 2025-01-23 06:54:18 字数 123 浏览 2 评论 0原文

我希望能够从类中获得Form1属性。宽度,左,窗口等属性等属性。这样我就可以在类中执行此操作: messagebox.show(form1.width); ,所以我想参考整个form1。我该如何在代码中执行此操作?

I want to be able to get Form1 properties from within a class. Properties such as Width, Left, WindowState, and so on. So that I can then do this in the class: MessageBox.Show(Form1.Width); So I want to reference the whole Form1. How do I do that in code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自在安然 2025-01-30 06:54:18

示例1:
将实例存储在全局变量中

public static Form1 frm1;


//Form1 constructor
frm1 = this;

例如

public static Form1 frm1;

public Form1()
{
    InitializeComponent();
    frm1 = this;
    
    MessageBox.Show(Form1.frm1.Width.ToString());
}

Example 2:
You need to pass an instance to the class.

public class Class1
{
    private Form1 _frm1;
    public Class1(Form1 frm1)
    {
        this._frm1 = frm1;
    }
}

public class Class1
{
    private Form1 _frm1;
    public Class1(Form1 frm1)
    {
        this._frm1 = frm1;
        MessageBox.Show(this._frm1.Width.ToString());
    }
}

Example 1:
Storing the instance in a global variable,

public static Form1 frm1;


//Form1 constructor
frm1 = this;

e.g.

public static Form1 frm1;

public Form1()
{
    InitializeComponent();
    frm1 = this;
    
    MessageBox.Show(Form1.frm1.Width.ToString());
}

Example 2:
You need to pass an instance to the class.

public class Class1
{
    private Form1 _frm1;
    public Class1(Form1 frm1)
    {
        this._frm1 = frm1;
    }
}

e.g.

public class Class1
{
    private Form1 _frm1;
    public Class1(Form1 frm1)
    {
        this._frm1 = frm1;
        MessageBox.Show(this._frm1.Width.ToString());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文