Solved

Easy WinForm Question

Posted on 2009-05-12
22
254 Views
Last Modified: 2012-05-06
Hello all,

I'm a asp.net guy not really a winform guy. I want to know something so simple it's stupid :)

I have two forms. form1 and form2

on form1 I have a button go to form2

when I click the button I do this:

this.Hide();
Form2 frm2 = new Form2();
frm2.Show();

and then we are now in form2.

on form2 I have a back button...

how can I get back to the first form...

obviously if I do this:

this.Hide();
Form1 frm1 = new Form1();
frm1.Show();

I will now have 2 form1 objects. (which I don't want.) and it will flush all the information that I put on Form1.

Any ideas how to do this?
0
Comment
Question by:copyPasteGhost
  • 14
  • 6
  • 2
22 Comments
 
LVL 6

Expert Comment

by:Cebik
ID: 24365512

this.Hide();

Form2 nf = new Form2();

nf.ShowDialog();
 

this.Show();

Open in new window

0
 
LVL 6

Expert Comment

by:Cebik
ID: 24365519
ShowDialog - open form and wait to end..
0
 
LVL 7

Expert Comment

by:zwei
ID: 24365574

form1Button.Click += new System.Eventhandler(form1Button_Click);

form2Button.Click += new System.Eventhandler(form2Button_Click);
 

form1Button_Click(object sender, Eventargs e)

{

    form1.Hide();

    form2.Show();

}
 
 

form2Button_Click(object sender, Eventargs e)

{

    form1.Show();

    form2.Hide();

}

Open in new window

0
 
LVL 13

Author Comment

by:copyPasteGhost
ID: 24365605
But for form 2 I want them to be able to go back to form1 and then if they want be able to go back to form2 again and still have everything there....like a previous/next kind of set up without loosing any information and without creating multiple objects.
0
 
LVL 13

Author Comment

by:copyPasteGhost
ID: 24365614
@zwei

where do I put that?
0
 
LVL 6

Expert Comment

by:Cebik
ID: 24365692
Try this then.
:)

public partial class Form1 : Form

{

    Form form2 = new Form();
 

    public Form1()

    {

        InitializeComponent();
 

        form2.FormClosing += new FormClosingEventHandler(nf_FormClosing);

    }
 

    private void button1_Click(object sender, EventArgs e)

    {

        this.Hide();
 

        form2.Show();

    }
 

    void nf_FormClosing(object sender, FormClosingEventArgs e)

    {

        e.Cancel = true;

        ((Form)sender).Hide();

        this.Show();

    }

}

Open in new window

0
 
LVL 13

Author Comment

by:copyPasteGhost
ID: 24365713
it's not really when form2 closes...it's when they click a back button..

how would I wire the event to the back button?
0
 
LVL 6

Expert Comment

by:Cebik
ID: 24365718
When somebady try to close form2 then we not allowed to do that and we hide form2 and show main form.

Form form2 = new Form();  //use your Form2 form2 = new Form2();
0
 
LVL 6

Expert Comment

by:Cebik
ID: 24365742
form2ButtonBack
.
Click
 
+=
 
new
 
System
.
Eventhandler
(
form2ButtonBack

_Click
);form2Button.Click += new System.Eventhandler(form2Button_Click);

or click 2 times on button in designer
and add code

Open in new window

0
 
LVL 13

Author Comment

by:copyPasteGhost
ID: 24365754
ok I see...

now what do we do with Form2? since when they click the back button I want them to go back to form1?
0
 
LVL 6

Expert Comment

by:Cebik
ID: 24365755
wow..
what was that !!??


click 2 times on button in designer
and add code

or

form2ButtonBack.Click += new System.Eventhandler(form2ButtonBack_Click);form2Button.Click += new System.Eventhandler(form2Button_Click);

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:Cebik
ID: 24365780
this
.
Show
();   //if we are in class FORM1
or
form1.Show();  //show form1... 

you cant do new form.. 
new Form1(..)
you must have acces to

form1.Show();

Open in new window

0
 
LVL 13

Author Comment

by:copyPasteGhost
ID: 24365790
right...that I knew...I mean here...In Form2...

private void btnBack_Click(object sender, EventArgs e) {
         this.Hide();
         this.Show();
      }
0
 
LVL 6

Accepted Solution

by:
Cebik earned 500 total points
ID: 24365823
no..

make this

Form form2 = new Form();
 

public Form1()

{

    InitializeComponent();
 

    form2.VisibleChanged += new EventHandler(form2_VisibleChanged);

}
 

void form2_VisibleChanged(object sender, EventArgs e)

{

    //set visible of form1 to form2.Visible negation!

    this.Visible = !form2.Visible;

}
 

private void buttonShowForm2_Click(object sender, EventArgs e)

{

    form2.Show();

}

Open in new window

0
 
LVL 6

Expert Comment

by:Cebik
ID: 24365833
it's all in FORM1

in FORM2 do only

private void btnBack_Click(object sender, EventArgs e) {
         this.Hide();
      }
0
 
LVL 6

Expert Comment

by:Cebik
ID: 24365848
i must go :/
i hope you got it
0
 
LVL 6

Expert Comment

by:Cebik
ID: 24365883
sorry..  in your example make this
Form2 form2 = new Form2();

0
 
LVL 13

Author Comment

by:copyPasteGhost
ID: 24365888
perfect thanks.
0
 
LVL 6

Expert Comment

by:Cebik
ID: 24365889

public partial class Form1 : Form

{

    Form2 form2 = new Form2();
 

    public Form1()

    {

        InitializeComponent();
 

        form2.VisibleChanged += new EventHandler(form2_VisibleChanged);

    }
 

    void form2_VisibleChanged(object sender, EventArgs e)

    {

        //set visible of form1 to form2.Visible negation!

        this.Visible = !form2.Visible;

    }
 

    private void buttonShowForm2_Click(object sender, EventArgs e)

    {

        form2.Show();

    }

}

Open in new window

0
 
LVL 7

Expert Comment

by:zwei
ID: 24365895
In my example I meant something like this:
//Form2 class:

public partial class Form2 : Form

    {

        private Form1 form1;

        

        public Form2(Form1 form)

        {

            InitializeComponent();

            form1 = form;

        }
 

        private void backBtn_Click(object sender, EventArgs e)

        {

            form1.Show();

            this.Hide();

        }

    }
 

//Form1 class:

 public partial class Form1 : Form

    {

        private Form2 form2;

        

        public Form1()

        {

            //

            // The InitializeComponent() call is required for Windows Forms designer support.

            //

            InitializeComponent();

            form2 = new Form2(this);

        }
 

        private void backBtn_Click(object sender, EventArgs e)

        {

            form2.Show();

            this.Hide();

        }

    }

Open in new window

0
 
LVL 6

Expert Comment

by:Cebik
ID: 24365900
ufff..
:)
youre welcome
0
 
LVL 6

Expert Comment

by:Cebik
ID: 24365928
it will work too..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
XAML: Layout 8 25
Runtime Error 2 28
how can i get the executable application's path in a specific form? ('\\' instead of '\ ') 5 28
Coding C# in Linux 8 21
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now