2016-11-20 73 views
1

我创建了包含面板的用户定义控件,面板中有一个标签和一个文本框。现在,在我的父母表单中有一个流程布局面板。我将我的用户定义控件添加到流布局面板。如何从父窗体中的用户定义控件获取控件的值

user-defined control

flowlayout panel

这里是我使用得到控制值的代码,但它总是给我检查ListBox控件值:

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

'(Window.Controls [x]作为UserControl).Control.Property'? –

回答

0

如果你把你的用户控件到您的ParentForm,Designer会创建它,并且您可以直接访问它。

例如:

myUserControl.Enable = false; 

您ParentForm不必知道一些关于用户控件内部的控件。只需花你的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"; 
} 

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

相关问题