简体   繁体   中英

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

I have created a user defined control which contains a panel and in the panel is a label and a textbox. Now, in my parent form there is a flowlayout panel. I am adding my user-defined control to the flowlayout panel.

用户定义的控件

flowlayout面板

Here is the code which I am using to get the control values, but it's always giving me checked listbox control values:

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

If you add your UserControl to your ParentForm the Designer creates it and you have direct access to it.

For example:

myUserControl.Enable = false;

Your ParentForm doesn't have to know something about your Controls inside the UserControl. Just spend your UserControl some Properties. Let's say you have a Textbox for the Name from Customers:

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";
}

I hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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