Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I have User Control and it shows in the main form in
after I click on the button. How I can hide this control after I click on the button that is in the control. OR how can I call main form's method from control? Any suggestions?
Posted

It's not a good idea to try and call any methods in the form from the user control, it locks the control to a particular form, and means that it can't be re-used later, and the form can't be changed without considering the effects on the control.

Instead, create an event in the Control which the Form handles. The form then does what is necessary and feeds information back to the Control either via a public method or a property. In this case, create a HideRequest event that the form handles:
C#
/// <summary>
/// Event to indicate Control wishes to be hidden
/// </summary>
public event EventHandler HideRequest;
/// <summary>
/// Called to signal to subscribers that Control wishes to be hidden
/// </summary>
/// <param name="e"></param>
protected virtual void OnHideRequest(EventArgs e)
    {
    EventHandler eh = HideRequest;
    if (eh != null)
        {
        eh(this, e);
        }
    }
/// <summary>
/// Event to indicate Control wishes to be un-Hidden
/// </summary>
public event EventHandler ShowRequest;
/// <summary>
/// Called to signal to subscribers that Control wishes to be un-Hidden
/// </summary>
/// <param name="e"></param>
protected virtual void OnShowRequest(EventArgs e)
    {
    EventHandler eh = ShowRequest;
    if (eh != null)
        {
        eh(this, e);
        }
    }
All you need to do in the Control is call the OnHideRequest at the appropriate time, and the form can do the rest (or not, if it isn't interested in that feature)

[edit]Code block type changed from "xml" to "c#" - paste incorrectly identified the code - OriginalGriff[/edit]
 
Share this answer
 
v2
In this respect, user control class is no different from any other class, no matter control or not. You always need to pass something to the instance of the control, something which can be called. It could be: 1) delegate instance, 2) a reference to the class with the method(s), in this case, a form instance, but this will strongly abuse encapsulation, 3) an interface reference, in this case, you should implement some interface by your form class, much better approach.

But I would rather advise you to invert control. Define and implement some event in your user control class, make the interface instance accessible to the user of the user control (internal or public). This way, the form class will be using the control. When the instance of the control is instantiated, the code of the form class should add an event handler to the invocation list of that user control event. In C#, this is the '+=' operator on the event instance. When an instance of the user control invokes the event, all event handlers will be called. As one even handler is your form's method, it will be called, and it can call any other method(s).

This way, the user control instance remains agnostic to the methods called. They can be private. And the form class remains agnostic to the detail of the call. All you need to know is the use of events: http://msdn.microsoft.com/en-us/library/awbftdfh.aspx[^].

And please read about the idea of inversion of control: http://en.wikipedia.org/wiki/Inversion_of_control[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900