اگه قراره فقط یک text برگرده، شما حلقه رو درون get مربوط به اون property بگذارید، یعنی در خود UserControl که به CheckBox ها دسترسی دارید.
class MyUserControl : UserControl
{
// ...
public string SomeText
{
get
{
for ....
return result;
}
}
// ....
}
اگه قراره بیش از یک مقدار برگرده، باز میتونید به همین روش عمل کنید و آرایه ای از رشته ها رو برگردونید:
class MyUserControl : UserControl
{
// ...
public string[] SomeText
{
get
{
List<string> MyList = new List<string>();
for ....
return MyList.ToArray();
}
}
// ....
}
اگه هم میخواین کلا حلقه و عملیات رو در فرم انجام بدین، بجای ایجاد دسترسی مسقیم به CheckBox ها، میتونید متدها و property های مناسب رو در UserControl بگذارید تا از Form به وضعیت CheckBox ها دسترسی داشته باشید، مثلا:
class MyUserControl : UserControl
{
// ...
public ItemCount
{
get
{
return [Number of checkboxes];
}
}
public bool IsItemChecked(int Index)
{
return [true if specified checkbox is checked, false otherwise];
}
public bool GetItemText(int Index)
{
return [text of the specified checkbox];
}
// ....
}
اگه هم میخواین راحت ترین کار و بدترین کار رو انتخاب کنید، متغیر هایی که توشون CheckBox رو new میکنید رو به صورت public تعریف کنید، مثلا لیستی از CheckBox ها:
class MyUserControl : UserControl
{
public List<CheckBox> MyCheckBoxes = new List<CheckBox>();
private void UserControl_Load(object sender, EventArgs e)
{
CheckBox cb = new CheckBox();
MyCheckBoxes .Add(cb);
}
}
اینطوری توی فرم به مشخصه MyCheckBoxes که لیستی از CheckBox های شما در userControl هست دسترسی دارید.