简体   繁体   English

从用户控件表单更新父表单控件

[英]update parent form control from user control form

i have one user control gridview which value changes every second, so I put one-timer of interval 1sec and updating gridview,我有一个用户控件 gridview,其值每秒都在变化,所以我放了一个间隔 1 秒的计时器并更新 gridview,

now I want to update its parent form label value as per gridview total number of rows, inside timer现在我想根据 gridview 总行数,在计时器内更新其父表单 label 值

i tried below code in user control form我在用户控件表单中尝试了以下代码

  if (ParentFormInstance != null)
                    {

                        Label mylabel = (ParentFormInstance.Controls["lblTotalDevices"] as Label);                     
                        mylabel.Text = totalcount.ToString();
                    }

i have one property in user control我在用户控件中有一个属性

 public FormEmuHost ParentFormInstance { get; set; }

this property value I am setting in parent form constructor: Parent form code我在父表单构造函数中设置的这个属性值:父表单代码

 public FormEmuHost(string ISdeveloper, EmuSettings emuSettings)
            {
                InitializeComponent();
            this.emuDeviceListControl1.ParentFormInstance = this;
            }

Problem is, I always get label control value null in the user control form问题是,我总是在用户控制表单中得到 label 控制值 null 在此处输入图像描述

You can create an Event and fire it off inside your User Control您可以创建一个事件并在您的用户控件中触发

public partial class CustomMapShapeEditor : UserControl
{
        public CustomMapShapeEditor()
        {
            InitializeComponent();
        }

        public EventHandler Ev_BTN_Pressed ;

        private void BTN_Click(object sender, EventArgs e)
        {
            if (Ev_BTN_Pressed != null)
                Ev_BTN_Pressed (this, e);
        }

}

And then in your parent form Subscribe to the event and handle the event.然后在你的父表单中订阅事件并处理事件。

this.childForm.Ev_BTN_Pressed += HandleChildBtnPress;

internal void HandleChildBtnPress(object sender, EventArgs e)
{

}

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

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