Click here to Skip to main content
11,718,016 members (81,927 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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 25-Jan-13 7:25am
LokkiUA362
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
/// <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]
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 874
1 OriginalGriff 215
2 F-ES Sitecore 210
3 Maciej Los 140
4 Jochen Arndt 120
0 Sergey Alexandrovich Kryukov 1,649
1 OriginalGriff 535
2 Maciej Los 529
3 Richard MacCutchan 440
4 CHill60 415


Advertise | Privacy | Mobile
Web01 | 2.8.150901.1 | Last Updated 25 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100