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

时间:2016-11-20 05:56:47

标签: c# .net winforms windows-forms-designer

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

user-defined control

flowlayout panel

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

// 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());
  }
}

1 个答案:

答案 0 :(得分:0)

如果您将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";
}

我希望这会对你有所帮助。

相关问题