Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a few UserControls on my C# Windows Form application on clicking of a button on the mainform it wll dock itself to the area on my mainform.

The first usercontrol (IncomeUC) is docked in a panel (panelContainer) on my MainForm when the Income button on my MainForm is clicked. This UC contains a button (Add Income) which once clicked opens the next usercontrol (AddIncomeUC).

But once the AddIncomeUC is opened When i click on the Income button on my Main form to view the IncomeUC (or even if i click any other button to view other main UCs) it is not visible and the AddIncomeUC stays visible.

Is there away to fix this issue? How can i get the AddIncomeUC to hide/close once i click another button?

What I have tried:

This is how the incomeUC is opened on the main form

private void btnIncome_Click(object sender, EventArgs e)
    {
        //when click the income button open the income records view
        //incomeUC1.BringToFront();

        this.panelContainer.Controls.Remove(dashboardUC1);
        this.panelContainer.Controls.Remove(expenseUC1);
        this.panelContainer.Controls.Remove(payerPayeeUC1);
        this.panelContainer.Controls.Remove(reportsUC1);
        this.panelContainer.Controls.Remove(predictionUC1);

        this.panelContainer.Controls.Add(incomeUC1);
        lblViewTitle.Text = "Your Income";
    }


this is how the AddIncomeUC is opened when click the add income button on the IncomeUC

public partial class IncomeUC : UserControl
{
    AddIncomeUC addIncomeUC1;
    public IncomeUC()
    {
        InitializeComponent();
        addIncomeUC1 = new AddIncomeUC();
    }

    private void btnAddIncome_Click(object sender, EventArgs e)
    {
        this.Hide();
        this.Parent.Controls.Add(addIncomeUC1);
    }
}
Posted
Updated 21-Nov-18 22:02pm

1 solution

Don't do it like that.
The idea of usercontrols is that they are standalone objects, they shouldn't know about each other (or even about any parent). Instead, they communicate with the parent container (your form) via events and properties, in just the same way you do with separate forms.

Your form creates the first usercontrol, and handles an event. When the usercontrol fires the event, it removes the original usercontrol, and adds the new one, handling an event from that. This sounds complex, but it isn't in practice - it's exactly what you have been doing with all other controls, even the .NET ones!

Have a look here: Transferring information between two forms, Part 3: Child to Child[^] - it's form based but it's exactly the same process (and eveny pretty much the same code!) for UserControl objects.
 
Share this answer
 
Comments
Member 14063275 22-Nov-18 4:26am    
Thank you for your response. I went through that but i do not understand that logic.
OriginalGriff 22-Nov-18 4:34am    
Which bit? That's basic OOPs technique!
Member 14063275 22-Nov-18 4:38am    
Excuse my lack of knowledge as i am new to c#. I did not understand the "Child Form Event" and the "The parent then passes the information across in the event handler" sections of that article.
OriginalGriff 22-Nov-18 4:54am    
OK. Forgive me in advance if I'm talking over your head or teaching your granny to suck eggs - I don't know what you do and don't know and I can't see when your eyes start to glaze over! So if I'm way over your head, tell me, and the same if you learned this stuff ages ago, OK?

What do you know about events?
You know that the whole system is based around them, yes? When you click a button, it raises an event and that causes any handler to be executed.

So in your child form (or usercontrol) you create an event - the code for that is pretty simple:
https://www.codeproject.com/Articles/400287/A-simple-code-snippet-to-add-an-event
Shows the code, and a simple way to add one to VS in the same way that you type "prop" TAB TAB to create a property template.
When you create the usercontrol in response to a button, you can add events to the control instance - you know that right?
Member 14063275 22-Nov-18 5:02am    
Thanks for taking your time to explain to me. Yes i know a button raises an onclick event. So that is in my second user control i should have an event like ""public event EventHandler AddIncome;"" plus the virtual method related to it?

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