[英]How can I get values of a control from the user-defined control in the parent form
我創建了一個用戶定義的控件,其中包含一個面板,在面板中是一個標簽和一個文本框。 現在,在我的父表單中有一個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.