2016-11-20 78 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"; 
} 

我希望這會對您有所幫助。

相關問題