C#从另一个类访问WinForm控件的属性 [英] c# Accessing WinForm control properties from another class

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

问题描述

如何一次访问的WinForm控件,如进度从另一个类的属性?



请参见下面我的代码。我知道这可能不是公开的WinForm类及其成员为公共,但我想清楚在这一点上的概念。

$ B $最好的选择b

 命名空间WindowsFormsApplication1 
{
公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent();
}

公共无效的button1_Click(对象发件人,EventArgs五)
{
Class1的C = 1类新();
c.loop();
}

公共无效PBSetup()
{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
}

公共无效PBUpdate(INT RECNO)

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

命名空间WindowsFormsApplication1
{
类class1
{

公静态INT RECNO;

公共无效循环()
{
//我如何访问Form1.PBSetup()?

为(RECNO = 0;&RECNO LT; = 100; RECNO ++)
{
//我如何访问Form1.PBUpdate(RECNO)?
}
}
}
}


解决方案

您不希望您的业务逻辑(类)与你的UI(表单)的交互。业务逻辑应该是无关的表示层。



如果您希望的形式的东西里面的类中发生反应,可以考虑暴露的事件 的类,它的形式可以订阅,就像它会订阅按钮的Click事件中。类的实例可以关火事件完全不知道谁可能是听的,任何用户将收到通知。


How does one access WinForm controls such as ProgressBar properties from another class?

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.

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). 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. The class instance could fire off the event completely unaware of who might be listening, and any subscribers would be notified.

这篇关于C#从另一个类访问WinForm控件的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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