Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
1.60/5 (2 votes)
See more:
My question is if i have form A and Form B and i want the program to start with form A then if the user presses a button goes to form B but form B appears to the user as shown in the same form of A ie i don't want to show 2 different forms Form A and Form B I want either form A to be shown to the user or form B to be shown but not shown together???
Posted
Comments
joshrduncan2012 16-Dec-13 17:25pm    
This is unclear, but I'm going to take a stab at what I think you are asking about.

I think you want to call FormA.Hide() and FormB.Show().
gettgotcha 16-Dec-13 17:32pm    
Not clear about the question.

Convert your "forms" to UserControls and then create an instance of either one in your main form and show that instead.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Dec-13 20:26pm    
Basically, this is the best option, but there is no a special need in UserControl. There are many other possibilities: Panel, TabPage (perhaps the easiest). I voted 4.
—SA
Dave Kreskowiak 16-Dec-13 21:40pm    
Pretty much, yes. It really doesn't matter what the container is. UserControl just gives you direct designer support without doing anything special at all.
Sergey Alexandrovich Kryukov 16-Dec-13 22:04pm    
Which is available anyway with all existing designer controls. You put controls (which OP tried to put on a form) on a tab page or a panel in exact same way as on the UserControl. Not that UserControl is wrong, it is just irrelevant to the issue.

At the same time, the container control, to successfully replace a form, should provide some dynamic layout features: tabs select a tab page, docking docks it, or, say, list box can select which panel is made visible, and so on. This is all what matters.

I hope you can see my point.
—SA
Assume:

1. Form A is your main form in a Win Form Application.
2. Form A will create Form B.
3. There's a Button on Form A that if clicked will hide Form A and show Form B
4. There's a Button on Form B that if clicked will hide Form B and show Form A.
5. The Form that is hidden should not show in the TaskBar.

Form A:
C#
public partial class FormA : Form
{
    public FormA()
    {
        InitializeComponent();
    }

    FormB B = new FormB();
 
    // Form A Load EventHandler   
    private void Form1_Load(object sender, EventArgs e)
    {
        this.ShowInTaskbar = true;

        // subscribe to the Click Event of Form B's button
        B.FormBBtn.Click += btnShowA_Click;
    }

    // the Click EventHandler for the Button on FormB
    private void btnShowB_Click(object sender, EventArgs e)
    {
        this.ShowInTaskbar = false;
        this.Hide();

        B.ShowInTaskbar = true;
        B.Show();
    }

    // the Click EventHandler for the Button on FormA
    private void btnShowA_Click(object sender, EventArgs e)
    {
        B.ShowInTaskbar = false;
        B.Hide();

        this.ShowInTaskbar = true;
        this.Show();
    }
}
Form B:
C#
public partial class FormB : Form
{
    public FormB()
    {
        InitializeComponent();

        // set the reference to Form B's Button
        FormBBtn = btnShowA;
    }

    // expose Form B's Button via a Public Property
    public Button FormBBtn { get; private set; }
}
Note that we must set the reference to Form B's Button in Form B's Constructor because the instance of Form A needs access to it before the Load Event for Form B will be triggered in order to subscribe to Form B' Button Click Event.
 
Share this answer
 

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