繁体   English   中英

如何从父表单中的用户定义控件获取控件的值

[英]How can I get values of a control from the user-defined control in the parent form

我创建了一个用户定义的控件,其中包含一个面板,在面板中是一个标签和一个文本框。 现在,在我的父表单中有一个flowlayout面板。 我将我的用户定义控件添加到flowlayout面板。

用户定义的控件

flowlayout面板

这是我用来获取控件值的代码,但它总是给我检查列表框控件值:

// Here 'panel_Attribute' is my parent form panel to which I have added the controls 
Control.ControlCollection listControls =  panel_Attribute.Controls;
foreach (Control attributeControl in listControls)
{ 
  if (attributeControl is Control)
  {
    log.Debug("attributeControl Values are attributeControl attributeControl.Name" +
        attributeControl.Name + ", Value: " + attributeControl.Text);

    attributeList.Add(((PHShowAttributeControl)attributeControl).
        ProbeRawProjectTaskAttributeEvent);
    //attributeList.Add(GetControlValues());
  }
}

如果将UserControl添加到ParentForm,Designer会创建它并且您可以直接访问它。

例如:

myUserControl.Enable = false;

您的ParentForm不必了解UserControl中的控件。 只需花一些UserControl一些属性即可。 假设您有一个来自客户姓名的文本框:

public class MyUserControl : UserControl
{
   public string Name
   {
      //Inside your UserControl you can access your Controls directly
      get{return textBoxName.Text;}
      set {textBoxName.Text = value;}
   }
}

public class MyForm : Form
{
   //This set the Text in your UserControl Textbox.
   myUserControl.Name = "Mr. Example";
}

我希望这能帮到您。

暂无
暂无

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

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