Laurent
Laurent

Reputation: 101

open form and usercontrol and change user control by button winform c#

I have a c# form with 5 tabs which includes an user control. I have a button in this control, and I want that when the user clicks on the button, another user control will be opened and change the tab.

my form with 5 tabs

        public Form1()
    {
        InitializeComponent();
        PanelSlider.Controls.Add(new Accueil());
        PanelSlider.Controls.Add(new Outils());
        PanelSlider.Controls.Add(new Utilitaires());
        PanelSlider.Controls.Add(new Extras());
        PanelSlider.Controls.Add(new Options());
        Accueil_menuStrip.Renderer = new MyMenuRenderer_Menu();

        Load += new EventHandler(Form1_Load);
        Shown += Form1_Shown;

    }

the tab Accueil call user control Accueil

my button click user control Acceuil in my form

        public void UtilsBtn_Click(object sender, EventArgs e)
    {
        PanelSlider.Controls.Find("Utilitaires", false)[0].BringToFront();
    }

the tab Utilitaires call user control Utilitaires

        public Utilitaires()
    {
        InitializeComponent();

        PanelSlider_Utils.Controls.Add(new Project1());
        PanelSlider_Utils.Controls.Add(new Project2());
        PanelSlider_Utils.Controls.Add(new Project3());
    }

In the user control Utilitaires i can call 3 others user control

How i can directly call project 3 from my user control Accueil and select the tab utilitaires ?

I am able to select the utilitaires tab directly by the button in my acceuil user control but NO the project3 usercontrol

        public void btn_UserFrame_Click(object sender, EventArgs e)
    {
        Form1 essai = (Form1)this.FindForm();
        essai.UtilsBtn.PerformClick();

        how to call project3 in my utilitaires usercontrol...?
    }

Always show project1 in my utilitaires usercontrol no project3... I do not know how to do...

RESUME:

When i click on the button UserFrame i want to change the tab form and show the usercontrol Utilitaires with the PanelSlider_Utils Project3

Thanks

Upvotes: 0

Views: 1142

Answers (1)

Idle_Mind
Idle_Mind

Reputation: 39122

Since you're adding Project3 with no name:

PanelSlider_Utils.Controls.Add(new Project3());

You'd have to iterate over all the controls and look for a match based on TYPE. Another option would be to set the Name property and then use Controls.Find() like you're already doing.

Why don't you store those references, though?

private Project1 p1 = new Project1();
private Project2 p2 = new Project2();
private Project3 p3 = new Project3();

public Utilitaires()
{
    InitializeComponent();

    p1.Name = "p1";
    p2.Name = "p2";
    p3.Name = "p3";

    PanelSlider_Utils.Controls.Add(p1);
    PanelSlider_Utils.Controls.Add(p2);
    PanelSlider_Utils.Controls.Add(p3);
}

Now you can use "p1", "p2", and "p3" directly to manipulate them somehow...

Upvotes: 1

Related Questions