0

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();
        }
Dev
  • 45
  • 1
  • 12
  • 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 Jan 08 '18 at 00:02
  • 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 Jan 08 '18 at 00:14
  • @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 form – Dev Jan 08 '18 at 00:16

1 Answers1

0

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();
dsum
  • 1,433
  • 1
  • 14
  • 29
  • it doesn't work, I get an error at this line : Control trainerTab1 = this.Controls.Find("trainerTab1", true); error : cannot implicitly convert type 'system.windows.forms.control[] ' to 'system.windows.forms.control' – Dev Jan 08 '18 at 01:03
  • Sorry, forgot that the Controls.Find return Control[] using System.Linq ... if (trainerTab1 != null && trainerTab1.Any()) trainerTab1.First().BringToFront(); – dsum Jan 08 '18 at 04:42