Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I have a small clarification about c# windows application.
The moment i have two form such as form1 and form2.
In form1 i have one textbox and one button.
when i have enter any text in to the textbox and then i click ok button which is by the given below code,
my code is: form1

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 fm = new Form2();
            fm.Show();
        }
    }
}


In form2 i have one back button.

In the back button i have return the below code which is redirect to the previous page which mean the were would i came.

my code:form2

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 fm = new Form1();
            fm.Show();
        }
    }
}




But my problem is when i go to second form from form1 and click form2 button then my form1 textbox value automatically cleared in form1.

But my goal is i dont want cleared my textbox value which i entered in form1 textbox

so please let me know if anybody knows this issue.. so that its very kind helpful for me

thanks in advance.
Posted
Comments
[no name] 23-Aug-13 7:34am    
No... the textbox is not "cleared" as it is a completely new and different textbox. You would need to hide the first form and then reshow the same form when you close form 2.

Instead of creating (and showing) a new instance of Form1, in your Form2 'back' button, you might simply call the Close() method (this would close Form2 and show again the Form1, with the same state it had before).
 
Share this answer
 
As ThePhantomUpvoter pointed out, you are showing a new form every time you click one of the buttons. Instead of this, you can hide each form when its button is pressed and show the other form. Here's how:

First, you need to have a reference to each form accessible in the other form. For this, I modified the Program.cs file as follows:
C#
public static Form1 f1;
public static Form2 f2;

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    f1 = new Form1();
    Application.Run(f1);
}


Then, you need to modify the buttons' events to hide the current form and show the other form:
C#
private void button1_Click(object sender, EventArgs e)
{
    if (Program.f2 == null)
    {
        Program.f2 = new Form2();
    }

    this.Hide();
    Program.f2.Show();
}

C#
private void button1_Click(object sender, EventArgs e)
{
    if (Program.f1 == null)
    {
        Program.f1 = new Form1();
    }

    this.Hide();
    Program.f1.Show();
}
 
Share this answer
 
Comments
stellus 23-Aug-13 8:37am    
thank you so much Mr.Botcar, the code which was send to me the above one is working fine in my end, thanks a lot bot.
simply you have to call the active forms close event
you have to write same code

Form.ActiveForm.Close();

and in vs 2008


you have to write


me.close()
 
Share this answer
 
v2
Comments
[no name] 23-Aug-13 8:43am    
"Me.anything" is not valid in C#

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