首页 > 解决方案 > 从表单将值传递给用户控件

问题描述

我在我的 WFA(Windows 窗体应用程序)中创建了用户控件,我想从我的 to 传递值MainForm.csUserControl.cs但我不知道如何做到这一点。这是我想传递给UserControl.cs

public partial class MainForm : Form
{
        private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
             if (ProcOpen)
             {
                 //THESE 
                 int vlInt = m.ReadByte("base+007C1DAC,0x14,0x4");

                 int roomID = m.ReadByte("base+003CA150,0x0");

                 double diffValue = m.ReadDouble("base+007B4A3C,0x0,0x2c,0x10,0x7ec,0x300");
             }
       }
}

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

        private void FirstCustomControl_Load(object sender, EventArgs e)
        {
            //GET THE VALUES HERE
        }

    }

标签: c#

解决方案


您可以为您的 UC 定义一个属性,然后从父级设置该属性;

public partial class FirstCustomControl : UserControl
{

    public static dynamic vlInt;
    public static dynamic roomID; 
    public static dynamic diff; 



    public FirstCustomControl()
    {
        InitializeComponent();
    }
    public void NotifyValueChanged(){
           label1.text = vlInt.ToString();
           label2.text = roomID.ToString();
           label3.text = diff.ToString();

    }
    private void FirstCustomControl_Load(object sender, EventArgs e)
    {

    }
}

然后在你的 MainForm

    public partial class MainForm : Form
{
        private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
             if (ProcOpen)
             {
                 //THESE 
                 FirstCustomControl.vlCount = m.ReadByte("base+007C1DAC,0x14,0x4");

                 FirstCustomControl.roomID = m.ReadByte("base+003CA150,0x0");

                 FirstCustomControl.diff = m.ReadDouble("base+007B4A3C,0x0,0x2c,0x10,0x7ec,0x300");
                 firstCustomControl1.NotifyValueChanged();
             }
       }
}

推荐阅读