0
votes

I create 2 menus (one for admin and another for user) those two menus are user controls, and I have a panel which will shows all content (Other user control) ..

userControl 1 is : Admin_menu

Button1 : button in Admin_menu

Button2 : button in Admin_menu

userControl 3 is : content1

userControl 4 is : content2

I have a main form that contains two sides : left side : Admin_menu right side : panel where the content will shows up

All the userControls are added to main form

the problem is : when I click on a button1 for showing content1, or button2 for showing content2 , I get this message :

Problem pic

My code in Admin_menu.cs :

        public Admin_menu()
        {
            InitializeComponent();
        }

        private void btnTrainers_Click(object sender, EventArgs e)
        {
            Parent.Controls["trainersTab1"].BringToFront();
        }

        private void btnBranches_Click(object sender, EventArgs e)
        {
            //BranchesTab branchespage = new BranchesTab();
            //this.Parent.Controls.Add(branchespage);
            Parent.Controls["branchespage"].BringToFront();
        }

        private void btnTimeTables_Click(object sender, EventArgs e)
        {
            Parent.Controls["timeTableTab1"].BringToFront();
        }
1
Controls trainersTab1, branchespage are added to Admin_Menu as part of InitializeComponent code? Are they added directly to the Admin_Menu or they are part of some container control such as Group Box or Panel in Admin_Menu? Are you using correct names of the controls to find them?Chetan
Highly likely you cannot get the "trainersTab" by Parent.Controls["trainersTab1"]. Best to use Visual Studio Watch window to see what objects are in Parents.Controls Remember, there is a hierarchy of control you might need to go through My best guess is to try Parent.Controls["Admin_Menu"].Controls["trainersTab1"].BringToFront()dsum
@ChetanRanpariya I have a main form that contains two sides : left side : Admin_menu right side : panel where the content will shows up all the userControls are added to main formDev

1 Answers

0
votes

Most likely

Parent.Controls["trainersTab1"]

is returning null.

This answer might help you find the trainerTab1 control, assuming the name of the control is also call "trainerTab1".

Get a Windows Forms control by name in C#

Control trainerTab1 = this.Controls.Find("trainerTab1", true);
if (trainerTab1 != null) trainerTab1.BringToFront();