在winforms中访问usercontrol中的mainform变量 [英] Access mainform variables in usercontrol in winforms

查看:75
本文介绍了在winforms中访问usercontrol中的mainform变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在usercontrol点击事件中访问此down_time变量;



MainForm

I am trying to access this down_time variable in usercontrol click event;

MainForm

public partial class MainForm : Form
   {
      public MainForm()
       {
           InitializeComponent();

       }

       public int down_time;

   }







Usercontrols






Usercontrols

public partial class Machine_Running : UserControl
    {
 
        private void btn_Click(object sender, EventArgs e)
        {
            lable1.text = // this is where i wanna assign it 
        }

    }





我尝试了什么:



我试图在UserControl中使用this.Parent得到父表格,但没有运气。请帮助



What I have tried:

I tried to used this.Parent in UserControl to get the parent form but no luck. please help

推荐答案

在两种表格之间传递信息,第1部分:父母对儿童 [ ^ ]


您的UserControl代码不应该在任何情况下使用控件执行任何操作包含表格。



UserControl中的代码只能关注自己设计界面中的控件。



如果你想告诉包含显示一些数据,你的UserControl应该公开一个包含订阅的事件,告诉它有一些它可能感兴趣的数据。然后由包含的表单代码决定如何处理这些数据。



Google:C#usercontrol自定义事件 [ ^ ]
Your UserControl code should not be doing ANYTHING AT ALL with controls in the containing form.

The code in the UserControl should ONLY be concerned with the controls in its own design surface.

If you want to tell the containing for to show some data, your UserControl should expose an Event that the containing for subscribes to tell it there is some data it may be interested in. It's then up to the containing form code to determine what to do with that data.

Google: C# usercontrol custom events[^]


这篇关于在winforms中访问usercontrol中的mainform变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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