Access functions from user control without events?

On some generic functions, it seems that accessing a function on mainform directly from the usercontrol is easier than raising an event. For example: A function on main form that displays one desired usercontrol centered and tweaked.

Making this function static, or access this function by reference from the user control to its parent, rather then raising an event, seems very convenient. Like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>usercontrol_A uc_a = new usercontrol_A();
MainForm mainform_functions = (MainForm)Parent;
mainform_functions.DisplayControl(uc_a);
</code>
<code>usercontrol_A uc_a = new usercontrol_A(); MainForm mainform_functions = (MainForm)Parent; mainform_functions.DisplayControl(uc_a); </code>
usercontrol_A uc_a = new usercontrol_A();
MainForm mainform_functions = (MainForm)Parent;
mainform_functions.DisplayControl(uc_a);

Raising an event seems more complex in such cases – I need to declare all new userControl_B’s events (which also includes initiating usercontrol_C and displaying it) inside the event that shows (and initiates) userControl_B:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> userControl_A.ShowUserControl_B+= (s,e) =>
{
usercontrol_B uc_b = new usercontrol_B(); DisplayControl(uc_b);
uc_b.show_uc_c += (s2,e2) => {usercontrol_C uc_c = new usercontrol_C(); DisplayControl(uc_c); }
}
</code>
<code> userControl_A.ShowUserControl_B+= (s,e) => { usercontrol_B uc_b = new usercontrol_B(); DisplayControl(uc_b); uc_b.show_uc_c += (s2,e2) => {usercontrol_C uc_c = new usercontrol_C(); DisplayControl(uc_c); } } </code>
  userControl_A.ShowUserControl_B+= (s,e) =>
 {
 usercontrol_B uc_b = new usercontrol_B();   DisplayControl(uc_b);
    uc_b.show_uc_c += (s2,e2) => {usercontrol_C uc_c = new usercontrol_C(); DisplayControl(uc_c); }
 }   

too much clutter here.

That’s hard to code and to read later. (In addition, passing parameters to another usercontrol is a nightmare like this – I need to save the parameter artificially on the sender usercontrol class and then cast it back later inside the event)

What is your opinion? Is it a completely “prohibited” to code like this, at least on generic functions like I mentioned? Isn’t clearer code that needs some small tweaking for reuse preferable?

3

This is why creational patterns exist, often times creation of types can be tricky as you have run into.

I think you want an abstract factory here (caveat: not true if user control A/B/C have basically identical attributes, if they’re identical you just need a single method that will create a control with the desired configuration which can be treated as control A, B, or C)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public interface IControlCreator
{
Control CreateControl();
}
public class ControlFactory<T> where T : IControlCreator, new
{
GetControl()
{
return (new T()).CreateControl();
}
}
public ControlACreator : IControlCreator
{
Control CreateControl()
{
Control controlA = new Control();
DisplayControl(controlA);
controlA.show_uc_b += (s,e) => new ControlFactory<ControlBCreator>().CreateControl();
}
}
public ControlBCreator : IControlCreator
{
Control CreateControl()
{
Control controlB = new Control();
DisplayControl(controlB);
controlB.show_uc_c += (s,e) => new ControlFactory<ControlCCreator>().CreateControl();
}
}
</code>
<code>public interface IControlCreator { Control CreateControl(); } public class ControlFactory<T> where T : IControlCreator, new { GetControl() { return (new T()).CreateControl(); } } public ControlACreator : IControlCreator { Control CreateControl() { Control controlA = new Control(); DisplayControl(controlA); controlA.show_uc_b += (s,e) => new ControlFactory<ControlBCreator>().CreateControl(); } } public ControlBCreator : IControlCreator { Control CreateControl() { Control controlB = new Control(); DisplayControl(controlB); controlB.show_uc_c += (s,e) => new ControlFactory<ControlCCreator>().CreateControl(); } } </code>
public interface IControlCreator
{
    Control CreateControl();
}

public class ControlFactory<T> where T : IControlCreator, new
{
    GetControl()
    {
        return (new T()).CreateControl();
    }
}

public ControlACreator : IControlCreator
{
    Control CreateControl()
    {
        Control controlA = new Control();
        DisplayControl(controlA);
        controlA.show_uc_b += (s,e) => new ControlFactory<ControlBCreator>().CreateControl();
    }
}

public ControlBCreator : IControlCreator
{
    Control CreateControl()
    {
        Control controlB = new Control();
        DisplayControl(controlB);
        controlB.show_uc_c += (s,e) => new ControlFactory<ControlCCreator>().CreateControl();
    }
}

Then anytime you want a control, it will have the appropriate events tied on in by it’s Creator class, all you need to call to get one is new ControlFactory<ControlWhateverCreator>().CreateControl();

Please, someone let me know if I got something wrong here; I’ve actually never written an abstract factory before. It’s a lot of overhead so I tend to avoid facotries, but it seems the right approach based on the problem scope the OP detailed.

Could you please just state conceptually what you are trying to achieve? The limited explanation and code make it hard to judge about the grander picture which is going to be important here.

However, to answer your initial question more generally:

On some generic functions, it seems that accessing a function on
mainform directly from the usercontrol is easier than raising an
event.

Depending on your scenario:

  • It is not prohibited if your user control always has to be located within that specific parent. In that case I would pass the parent (or an interface which the parent implements) to the constructor of the user control (constructor injection). This makes it impossible for the parent to be anything else than the correct expected type.
  • If it’s at all possible to reuse that user control in different parents, casting to the specific parent would make that impossible. That’s where the events would come in handy. This allows for a lot more loosely coupled code.

However, judging from your given code I would say probably the events are the way to go. It seems you are hard coding a lot stuff like userControl_A.ShowUserControl_B. Perhaps you could handle this conceptually in your parent by linking them together there instead? I’m thinking of creating an abstract user control class which has something along a Closed event which the parent class handles after which it shows the next user control.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật