Click here to Skip to main content
15,916,846 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create a project that contains two forms in it but here is the problem i do not have the least knowledge of using multiple forms so please help me...........my project would be like this first comes form1 which ask your name and then after the name is entered and a button is pressed the second from comes up and the first closes..........




Thanks in advance..........


Regards,
Ahsan Naveed
Posted
Updated 6-Sep-17 0:06am

Don't close the first form - when you do that, it closes the application.

The easiest way to do it is to Hide the current form then show teh new one:

C#
MySecondForm f = new MySecondForm();
Hide();
f.ShowDialog();
Close();
This hides the current form, displays the new one, and then closes the main form (and thus the application) when the new form closes.
 
Share this answer
 
Comments
Member 13831327 3-Jun-18 0:24am    
i want to add two forms in a single solution under a same project ,,,but after designed a first form it is working and running properly but after added and designed a new form to the existing solution i unable to run second form ., to check whether it is working or not ..please give the solution
OriginalGriff 3-Jun-18 1:06am    
So show us the code you are trying to use and explain exactly what it does that you didn't expect, or doesn't do that you did.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
 
Share this answer
 
I'm going to answer your question very "literally," assuming that you want to start a WinForms application with its "Primary Form" hidden, and an InitialForm displayed where the user enters a name, and then, when the user closes the InitialForm, you want your PrimaryForm (the Application) to become visible, and to have "captured" the information entered on the InitialForm, so you can use it in your Application (the PrimaryForm).

So, the InitialForm code is something like this: assuming you have a TextBox, and a Button, on it, and the Button Click Event wired-up:
C#
public partial class InitialForm : Form
{
    public InitialForm() { InitializeComponent(); }

    // this 'exposes a string to the 'outside
    public string UserEnteredText { get; private set; }

    // see below for why you need to implement this
    //private void InitialForm_FormClosing(object sender, FormClosingEventArgs e)
    //{
        //UserEnteredText = textBox1.Text;
    //}

    private void button1_Click(object sender, EventArgs e)
    {
        UserEnteredText = textBox1.Text;

        Close();
    }
}
So, the PrimaryForm (that you start your Application with) contains:
C#
// this code would not work in the Form Load Event
private void PrimaryForm_Shown(object sender, EventArgs e)
{
    Visible = false;

    InitialForm iform = new InitialForm();

    iform.FormClosing += iform_FormClosing;

    iform.Show();
}

// this will contain the text the user entered on the InitialForm
private string UserEnteredText;

// note that this could be called by either the user pressing
// the Button on the InitialiForm, or by closing the InitialForm
// by clicking its 'CloseBox
private void iform_FormClosing(object sender, FormClosingEventArgs e)
{
    // note that we have to cast to 'InitialForm here
    // to access its public property
    UserEnteredText = (sender as InitialForm).UserEnteredText;
    
    // show the Primary Form
    Show();

    // for testing only
    MessageBox.Show("The user entered: " + UserEnteredText);
}
But, wait: what happened if the InitialForm had a CloseBox, and the user closed it ? The public string property of the InitialForm would never have been set: so you need to add a FormClosing EventHandler to the InitialForm, and "wire it up" (as shown in the commented out code above for InitialForm):
C#
private void InitialForm_FormClosing(object sender, FormClosingEventArgs e)
{
    UserEnteredText = textBox1.Text;
}
Consider the upside, and downside, of using the technique shown here:

1. upside: InitialForm is disposed of: you never "kept a reference to it." You didn't have to write a custom Event in the InitialForm, and subscribe to it in the Primary Form. You didn't need (praise be) to muck around with WinForm's tricky Property Change notification. You didn't need to do anything "modal."

2. downside: You don't keep InitialForm around for possible re-use: and that's likely (?). Writing a custom Event in InitialForm, triggered by the Button Click Event, that you subscribed to in the PrimaryForm, might well result in "cleaner," more understandable, maintainable code.

Finally: by simply making InitialForm have no CloseBox, you can eliminate the fancy-dancing shown here the handle the case of the user closing InitialForm in a way that prevents you from "getting" the information from its TextBox.
 
Share this answer
 
v2
Hi,

Try this

C#
private void btnShowForm_OnClick(...)
{
    if(/* perform credential test here */)
    {
        (new Form2()).Show(); // create and show the other form
        this.Hide(); // Hides this form
    }
    else
        MessageBox.Show("your Message!");
}


also Visit these links..

http://www.homeandlearn.co.uk/csharp/csharp_s13p1.html[^]


http://support.microsoft.com/kb/821773[^]
 
Share this answer
 
v2
Comments
BillWoodruff 19-Aug-13 9:47am    
This will not work: your closing the primary Form will automatically close the second Form: unless you have modified the way launching your WinForm application is handled. Also, since the second Form is created in an 'if block: you'd be unable to access any information entered into it outside that block.
Adarsh chauhan 20-Aug-13 6:20am    
you are right. by mistake I wrote it as this.close() instead of this.Hide()... corrected now..
and about if block, I have written it as if I want to show the second form if textbox value is not blank.. so if block may or may not be used.. it depends on need.. :) any ways thanks for pointing out about this.close(); :)
I had a similar task: -
Main form to open and pass values (from a textbox) to a new form which could modify, and pass the modified data back to the original.
This set of solutions, plus a lot of trial and error, helped me come up with this solution : -
Main form (form1) add a button and a text box.
Sub form (form2) add a button and a textbox, then in the form properties set "ControlBox" property to "false"

Code for Form 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TwoForms
{
    public partial class Form1 : Form
    {
        public static TextBox tb = new TextBox();
        Form2 secondForm = new Form2();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            tb = textBox1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            secondForm.Show();
        }
    }

}


Code for form 2: -
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TwoForms
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            textBox1.Text = Form1.tb.Text;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            Form1.tb.Text = textBox1.Text;
        }
    }
}
 
Share this answer
 
Comments
Graeme_Grant 6-Sep-17 8:34am    
Why are you answering a 4-YEAR-OLD question? Do you think that they are still waiting for your answer? Please don't. There are plenty of current questions that require help.
Member 13395890 6-Sep-17 13:26pm    
Basically because this "thread" was near the top of the list when searching for info on using multiple forms.
It wasn't immediately helpful, but gave me a hint, so after a LOT of trial and error I found the way forward.
So in a spirit of helpfulness for future people like myself decided to post my findings!

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