Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here's the scenario ..

I have two UserControls named ucAdmin and ucTransactions which will be added to the Main form in a split container particularly. First the ucAdmin will show and the ucTransactions is hidden initially. From the ucAdmin, there is a toolstrip button that will show the ucTransactions and hide ucAdmin. And in the ucTransactions is a link that will show the ucAdmin and hide ucTransactions.

How can i do this without re initializing the Main form to new?

Can I do this with an EventHandler which will be placed in the Main form?

I am new in programming in I want to make a design like that. Please help me through this.

Thank you all..
Posted
Updated 30-Sep-14 20:52pm
v2
Comments
BillWoodruff 1-Oct-14 3:16am    
If only one of the two controls is visible at any one time, why use a SplitContainer ?
Romeland C. dela Peña 1-Oct-14 3:22am    
thank you for the comment.

so what should i use instead?
BillWoodruff 1-Oct-14 3:58am    
You could put both your two UserControls on the "surface" of your Form, or both in any other Container(s) of your choice, if you don't want to use a SplitContainer. All depends on what kind of app you want to design.

Do you understand how to raise an Event from a UserControl that can be subscribed to ?

1 solution

Assume you designed a Form, 'Form1, and two UserControls, 'TestUC1, 'TestUC2.

Both UserControls have a Button Control: TestUC1 has 'btnShowTestUC2 ... TestUC2 has 'btnShowTestUC1.

I'll start with showing you two variations of the standard ways to create Events in C#, then, we'll show you a technique that is radically simpler.

Here's a quick sketch:
C#
// in the Form that contains design-time placed instances of TestUC1 and TestUC2:
private void Form1_Load(object sender, EventArgs e)
{
    // note 0
    testUC11.MakeUC2Visible += testUC11_OnMakeUC2Visible;

    testUC21.MakeUC1Visible += testUC21_OnMakeUC1Visible;

    testUC21.Visible = false;
}

// EventHandlers defined in the Form:

private void testUC21_OnMakeUC1Visible(object sender, EventArgs e)
{
    testUC11.Visible = true;
    testUC21.Visible = false;
}

private void testUC11_OnMakeUC2Visible(object sender, EventArgs e)
{
    testUC21.Visible = true;
    testUC11.Visible = false;
}
The UserControls:
C#
// user control one: TestUC1
public partial class TestUC1 : UserControl
{
    public TestUC1()
    {
        InitializeComponent();
    }

    // note 1
    public delegate void MakeUC2VisibleEventHandler(object sender, EventArgs e);

    public event MakeUC2VisibleEventHandler MakeUC2Visible;

    protected virtual void OnMakeUC2Visible(EventArgs e)
    {
        if (MakeUC2Visible != null) MakeUC2Visible(this, e);
    }

    private void btnShowTestUC2_Click(object sender, EventArgs e)
    {
        OnMakeUC2Visible(e);
    }
}

// user control two: TestUC2
public partial class TestUC2 : UserControl
{
    public TestUC2()
    {
        InitializeComponent();
    }

    // note 2
    public event EventHandler MakeUC1Visible;

    protected virtual void OnMakeUC1Visible(EventArgs e)
    {
        EventHandler handler = MakeUC1Visible;

        if (handler != null) handler(this, e);
    }

    private void btnShowTestUC1_Click(object sender, EventArgs e)
    {
        OnMakeUC1Visible(e);
    }
}
Note 0: EventHandlers for the UserControls defined in the Form

Note 1: The "older" style of Event declaration requires both Delegate and Event.

Note 2: The "newer" style ... available with .NET 2.0 and later ... does not require an explicit Delegate

Yes, you're right: we did a lot of shifting cargo just to get a notification of an event passed out to the UserControl's container (the Form). Is there a simpler way: try this:
C#
// the Form
private void Form1_Load(object sender, EventArgs e)
{
    testUC11.BtnAction = setUCVisibility;
    testUC21.BtnAction = setUCVisibility;
}

private void setUCVisibility(UserControl userControl)
{
    if (userControl is TestUC1)
    {
        testUC11.Visible = false;
        testUC21.Visible = true;
    }
    else
    {
        testUC21.Visible = false;
        testUC11.Visible = true;
    }
}
The UserControls:
C#
// TestUC1
public Action<UserControl> BtnAction;

private void btnShowTestUC2_Click(object sender, EventArgs e)
{
    if (BtnAction != null) BtnAction(this);
}

// TestUC2    // guard against undefined Action
public Action<UserControl> BtnAction;

private void btnShowTestUC1_Click(object sender, EventArgs e)
{
    // guard against undefined Action
    if (BtnAction != null) BtnAction(this);
}
We declared a Public delegate of Type Action (an Action always has no return value, and, in this case, takes one parameter) [^] in each UserControl. In each UserControl's Button Click EventHandler we invoke the action, passing a reference to the UserControl instance the Button is 'in.

Technically, what this example does is inject a reference to a method at run-time into the instances of the UserForms. One could argue that this, like the other ways of raising events shown, results in loose-coupling between the UserControls and their "consumers:" there's no direct-linkage, no dependency, between the Form and the UserControls code. The UserControls "don't know" who/what inserts a method whose signature matches the Action declared in their code, or what that method will do when executed.

In the Form we define one method, setUCVisibility, which is assigned to the 'Action delegate in both UserControls. In this method, we detect which UserControl fired the 'Action and do the right thing to modify which UserControl is visible, which is hidden.
 
Share this answer
 
v5
Comments
Romeland C. dela Peña 1-Oct-14 20:30pm    
nice .. thank you for this information .. i'm sure it will help a lot .. i am thankful that you're helping newbies in programming ..
BillWoodruff 2-Oct-14 3:24am    
Do keep in mind that an Event is a powerful structure: internally it maintains a queue of pointers to the methods (EventHandlers) of all subscribers to the Event, and, when the Event is triggered, all subscribers' methods are invoked.

With an Event you can add new subscribers, or remove current subscribers using the += and -= operators.

The final example uses a delegate of Type Action: it also, like the Event delegates, maintains an internal queue of subscribers, and can be subscribed to: in the example shown here we assign to the Action delegate rather than subscribe to it using the += operator, but you can use += !

There are also other ways to raise events in .NET, such as implementing the INotifyPropertyChangin, and INotifyPropertyChanged interfaces.

cheers, Bill
Romeland C. dela Peña 11-Oct-14 3:55am    
i have used your sample codes and it worked!! so much thanks to you..

GOD BLESS YOU ALWAYS!

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