简体   繁体   English

c#从另一个类访问WinForm控件属性

[英]c# Accessing WinForm control properties from another class

How does one access WinForm controls such as ProgressBar properties from another class? 一个如何从另一个类访问WinForm控件,例如ProgressBar属性?

Please see my code below. 请在下面查看我的代码。 I know this might not be the best option to expose WinForm class and its members as public but I am trying to clear the concept at this point. 我知道这可能不是将WinForm类及其成员public的最佳选择,但是我现在试图清除这个概念。

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

        public void button1_Click(object sender, EventArgs e)
        {
            Class1 c = new Class1();
            c.loop();
        }

        public void PBSetup()
        {
            progressBar1.Minimum = 0;
            progressBar1.Maximum = 100;
        }

        public void PBUpdate(int recno)

        {
            progressBar1.Value = Class1.recno;
        }
    }
}

namespace WindowsFormsApplication1
{
    class Class1
    {

        public static int recno;

        public void loop()
        {
            //How do I access Form1.PBSetup()??

            for (recno = 0; recno <= 100; recno++)
            {
                //How do I access Form1.PBUpdate(recno)??
            }
        }
    }
}

You do not want your business logic (your class) interacting with your UI (your form). 您不希望您的业务逻辑(您的类)与UI(您的表单)进行交互。 The business logic should be agnostic of the presentation layer. 业务逻辑应该与表示层无关。

If you want the form to respond to things that happen inside the class, you could consider exposing an Event inside the class that the form could subscribe to, much like it would subscribe to a button's click event. 如果您希望表单对类中发生的事情做出响应,则可以考虑在类内公开一个表单可以订阅的Event ,就像它订阅按钮的click事件一样。 The class instance could fire off the event completely unaware of who might be listening, and any subscribers would be notified. 类实例可能会完全不知道谁在监听而触发该事件,并且将通知所有订阅者。

This looks like a big time code smell :). 这看起来像一个大时间代码的气味:)。 You would need an instance of Form1 inside of Class1 in order to PBUpdate . 您需要Class1内的Form1实例才能进行PBUpdate Something tells me what you are doing is just not right. 有件事告诉我你在做什么,这是不对的。

Explain what you are trying to do and we can help. 说明您要做什么,我们可以提供帮助。 Otherwise there is no way to access PBUpdate unless you either made it a static function where you could call it like Form1.PBUpdate() or you had an instance of Form1 within your class Class1 否则,除非您将其Form1.PBUpdate()可以像Form1.PBUpdate()那样调用的静态函数,或者您在类Class1拥有Form1的实例,否则无法访问PBUpdate。

You can change the access modifiers of the progress bar from private to Internal or public , you can do this operation from properties pane . 您可以将进度条的访问修饰符从“私有”更改为“内部”或“公共”,可以从属性窗格中执行此操作。

属性窗格

Keep in mind that you have to pass to the second class the instance of the form and then you can change the value of the progress bar directly from the second class. 请记住,您必须将表单的实例传递给第二个类,然后才能直接从第二个类更改进度栏的值。

However is a tricky solution, the best should be keep the presentation layer implementation separated and work with an event. 但是,这是一个棘手的解决方案,最好的方法是保持表示层的实现分离并处理事件。

I do not recommend to use this method, for simple reason as mentioned here by one of the comments. 我不建议使用此方法,原因很简单,如其中一条注释所述。 But if you really want to access that form control, here is how: 但是,如果您确实要访问该表单控件,请执行以下操作:

1) Select that control and set its access modifier to internal. 1)选择该控件,并将其访问修饰符设置为internal。 2) Assume your form id is "Form1" and control id is "control1" 2)假设您的表单ID为“ Form1”,控件ID为“ control1”

Inside your method: 在您的方法内:

Form1 form = (Form1)Application.OpenForms["Form1"];
// form.control1  should now be available.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM