Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
This may sound stupid, But I am having hard time to figure this out; any help would be appreciated:

I have two user controls called “Safety_Check” and “OEE_Track”. In my MainForm I have a panel called “pnl_main_controller” this is where I am displaying both my user controls. I have two buttons on my main form and I am dynamically switching between both without any issue.

Safety_Check User control;
C#
public partial class Safety_Check : UserControl
    {
        private static Safety_Check _instance;

        public static Safety_Check instance
        {
            get
            {
                if (_instance == null)
                    _instance = new Safety_Check();
                return _instance;
            }
        }

        public Safety_Check()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ///////------------------------
        }

    }

OEE_Track User control

public partial class OEE_Track : UserControl
    {

        private static OEE_Track _instance;

        public static OEE_Track instance
        {
            get
            {
                if (_instance == null)
                    _instance = new OEE_Track();
                return _instance;
            }
        }

        public OEE_Track()
        {
            InitializeComponent();
        }
    }

MainForm:

public partial class MainForm : Form
    {
     private void btn_reg_Click(object sender, EventArgs e)
        {
            if (!pnl_main_controller.Contains(Safety_Check.instance))
            {
                pnl_main_controller.Controls.Add(Safety_Check.instance);
                Safety_Check.instance.Dock = DockStyle.Fill;
                Safety_Check.instance.BringToFront();
            }
            else
            {
                Safety_Check.instance.BringToFront();
            }        }

       private void btn_OEE_Click(object sender, EventArgs e)
        {
            if (!pnl_main_controller.Contains(OEE_Track.instance))
            {
                pnl_main_controller.Controls.Add(OEE_Track.instance);
                OEE_Track.instance.Dock = DockStyle.Fill;
                OEE_Track.instance.BringToFront();
            }
            else
            {
                OEE_Track.instance.BringToFront();
            }
     }

What I am trying to do is I have a button called “Button1” on my “Safety_Check” Usercontrol, whenever I press this , I want “Safety_Check” to be disappear on “pnl_main_controller” and bring “OEE_Track” to the panel

What I have tried:

C#
if (!main.pnl_main_controller.Contains(Safety_Check.instance))
            {
                Safety_Check.instance.Hide();
                main.pnl_main_controller.Controls.Add(OEE_Track.instance);
                OEE_Track.instance.Dock = DockStyle.Fill;
                OEE_Track.instance.BringToFront();
            }
            else
            {
                OEE_Track.instance.BringToFront();
            }
Posted
Updated 8-Mar-19 6:14am
v3
Comments
BillWoodruff 9-Mar-19 9:13am    
Have you considered the simple solution of using a TabControl ?

This is how I might do it in your event handler in WPF (of course, you didn't tell us what framework you're working with, so I'm just guessing at a solution):
C#
// toggle the first item
Safety_Check.Visibility = (Safety_Check.Visibility == Visibility.Collapsed) ? Visibility.Visible 
                                                                            : Visibility.Collapsed;
// and then toggle the second item, based on the first item's visibility
OEE_Track.Visibility    = (Safety_Check.Visibility == Visibility.Collapsed) ? Visibility.Visible 
                                                                            : Visibility.Collapsed;
 
Share this answer
 
Comments
Dinuka Kuruppu 8-Mar-19 13:55pm    
Thanks for the answer. I forgot to mention about the framework, i am using winforms
Don't do it like that.
That's a poor solution, because it's against OOPs principles: the "child" controls have to not only be aware of their "parent" (which compromises design) but also of the existence (and type) of each other - which makes it impossible to reuse, and also means you can't make any changes without considering the effect of other code in other classes.
That's a lot of bad design ideas going on here!

Instead, use Events which are handled in the "parent form" to control what is going on.
See here: Transferring information between two forms, Part 3: Child to Child[^] - it's about forms (but user controls work in exactly the same way, forms are controls, after all) and communications but the same principles and mechanisms apply.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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