Click here to Skip to main content
14,924,198 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm creating an application where I have one main form, and several different User Controls which the user works on. This helps me splitting the code, managing the different parts of the program. And it would be easy to expand the application later on.

I'm trying to create a class where I manage the active controls I want to call one function in that class with as argument the form that should become active.

An image can illustrate how I try to setup this application.[^]

Note that the control manager class is not a seperate class in the code i show below, but a partial class of the mainform. Any tips on how to get it like in the image are very welcome :)

The class to manage all active forms looks like this: Please note that all user controls are just a user control with some buttons/textboxes etc on it. No code is added at all yet.

public partial class STP2Main 
// I make each UserControl accessable for the whole clas
            MenuControl.MenuControl MenuControl = new MenuControl.MenuControl();        

            public void SelectActiveWindow()
                // Any active control should be hidden thats what this function does:
                // Check whether the window is already created
                if (!WindowExists())
                { // if not created; create the windows: 
                    switch (STP_Design.ProgramParameters.C.NextActiveControl)
                        case STP_Data.Data.MenuControl: // control 3
                            STP_Design.ProgramParameters.C.CurrentActiveControl = STP_Data.Data.MenuControl;
                            STP_Design.ProgramParameters.C.MenuControlIsCreated = true;  
                            MenuControl.Location = new Point(3, 30);
                            MenuControl.Parent = this;
                { // window is already created so needs to be called to front again, same switch case structure as above removed to shorten things up :)

What I experience is the following: I get no errors at all. But the controls simply not change at all. If I call a window, it is created only once, because I did make it as partial class of my Mainform. (I've tried a complete seperate class, which did result in errors with threading, As I am not an experienced c# programmer, I tried to avoid that using a partial class.)

I'll add another function; which does not do anything at all:

private void HideCurrentActiveControl()
       switch (STP_Design.ProgramParameters.C.CurrentActiveControl)
           case STP_Data.Data.MenuControl:
               MenuControl.Visible = false;
               tabControl1.Visible = false;


I've tried debugging this part of code and it executes the statements, but I see no changes at all.

I think I've shown what I am trying to do; and how I try to do that. My question is: How do I acces those forms so I can manage them from a seperate class (or in this case partial class of the main form).

Summery of what I am looking for: I am having a main form where display different user controlls in. I am trying to create a seperate class which is accessable from each control/form in my project. This class should manage the controls which are shown. In the code above I illustrated how I tried to do this, but this does not result in the expected result.
Updated 30-Oct-12 2:12am
Sushil Mate 30-Oct-12 7:21am
can you put in short. no ones reading this long questions.
pieterjann 30-Oct-12 8:09am
I'll shorten it
Rajesh Varma Buddaraju 30-Oct-12 8:27am
Very lenghthy!

1 solution

After 2 days of fighting with this code I've found the solution:

THe first code blok was fine; nothing to change there. And the second was fine as well.

I was calling the function the wrong way. If I use this code to call the function it works: (if I'm calling it from a child form)

var C = this.Parent as STP2Main;

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