C# 如何在c中调用已打开的用户控件窗体#

C# 如何在c中调用已打开的用户控件窗体#,c#,winforms,C#,Winforms,在窗口窗体中,我使用这个调用打开窗口窗体 但在我的用户控制窗口中,我尝试了这个方法,但它返回null异常错误 我创建了homeDeliveryUC用户控件,在其中我添加了名为panel1的面板,在panel1中我有文本框和其他..,现在我想要的是用值填充文本框 其他窗口FRM homeDeliveryUC homeDelivery = (homeDeliveryUC)panel1.Controls["homeDeliveryUC"]; // return null excep

在窗口窗体中,我使用这个调用打开窗口窗体

但在我的用户控制窗口中,我尝试了这个方法,但它返回null异常错误

我创建了homeDeliveryUC用户控件,在其中我添加了名为panel1的面板,在panel1中我有文本框和其他..,现在我想要的是用值填充文本框

其他窗口FRM

homeDeliveryUC homeDelivery = (homeDeliveryUC)panel1.Controls["homeDeliveryUC"]; // return null exception (homeDelivery is null)
 homeDelivery.txtCustomerName.Text = row.Cells["Customer Name"].Value.ToString();

任何帮助谢谢

有很多方法可以完成你的任务

第一种方式(不推荐)

不建议使用此方法,因为强制
otherWindowForm
类假定存在mainForm类型的表单及其名称和其中的usercontrol名称。
换句话说,它会在两种形式之间产生耦合,这可能是将来的维护问题

第二种方法(更好)是在
otherWindowForm
类中声明事件,并让外部客户端(例如mainForm实例)绑定该类中的事件处理程序以接收所需信息

public class otherWindowForm:Form
{
    // Create an event to let other forms subscribe
    public delegate bool onUpdatedText(string value);
    public onUpdatedText UpdatedText = null;


   // inside some event handler in your otherWindowForm class
   // call Invoke when you want to inform subscribers 
   // of the updated text you call
   this.UpdateText?.Invoke(row.Cells["Customer Name"].Value.ToString()); 
}

public class MainForm: Form
{
      // when you create an instance of otherWindowForm and 
      // you want to be notified then subscribe the event 
      otherWindowForm f = new otherWindowForm();
      f.UpdatedText += onUpdate;

      // add the event handler in main form
      private void onUpdate(string newText)
      {
          this.homeDelivery.txtCustomerName.Text = newText;
      }
}

注意:我假设otherWindowForm、mainForm和homeDeliveryUC是类名,而不是实例名,此外,homeDelivery是mainForm实例中usercontrol实例的名称

您的用户控件是面板的子控件还是包含表单的子控件?mainFrm的子控件form然后您需要在mainFrmI have
mainFrm
表单的控件集合中搜索,homeDeliveryUc是此表单的子控件。在用户控件内部,我有一个按钮,它可以打开新窗口otherWindowFrm with table,当我单击表时,我想从表中填充用户控件文本框。你能给我看一些代码吗,谢谢!在第一种情况下,它通过
表单的错误不包含控件的定义find
对不起,我的坏消息。这是不完整的。现在它应该被修复了。再次感谢它返回
null
值。我遵循的步骤是:首先创建用户控件,然后重建它并将其放置在主窗体上,然后单击选项卡,它将显示前面的
homeDeliveryUC1.BringToFront(),这意味着我的用户控件是mainForm的子控件?或者我犯了什么错误吗?是的,它是一个孩子,但您应该确保没有将它插入mainForm中的另一个控件容器中,例如面板或GroupBox。如果是这样的话,那么您的usercontrol是容器的子项而不是表单的子项它是面板的子项,谢谢!我公开了这个小组,它工作得很好
form.panel1.Controls.find()…
// Inside some events in the otherWindowForm class
mainForm form = Application.OpenForms["NameOf_mainFrm"] as mainForm;
homeDeliveryUC uc = form.Controls.Find("NameOf_homeDeliveryUC", false).FirstOrDefault() as homeDeliveryUC;
uc.TextBox = "yourValue";
public class otherWindowForm:Form
{
    // Create an event to let other forms subscribe
    public delegate bool onUpdatedText(string value);
    public onUpdatedText UpdatedText = null;


   // inside some event handler in your otherWindowForm class
   // call Invoke when you want to inform subscribers 
   // of the updated text you call
   this.UpdateText?.Invoke(row.Cells["Customer Name"].Value.ToString()); 
}

public class MainForm: Form
{
      // when you create an instance of otherWindowForm and 
      // you want to be notified then subscribe the event 
      otherWindowForm f = new otherWindowForm();
      f.UpdatedText += onUpdate;

      // add the event handler in main form
      private void onUpdate(string newText)
      {
          this.homeDelivery.txtCustomerName.Text = newText;
      }
}