-1

Possible Duplicate:
Winform Forms Closing and opening a new form

In my windows application I have two forms; form1 and form2

In form1 I listen to a button click with the following handler,

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

In Form2 I want to click on a button and show the first form, form1, so the button click handler in form2 does the following

private void btn2_Click(object sender, EventArgs e)
{
    try
        {

            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "codedata.xml");
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            XmlNodeList nodelist = doc.SelectNodes("Root/data[Sno='" + getsnofromform1 + "']");
            nodelist[0].ChildNodes[2].InnerText = txt_productcodeupdate.Text;
            nodelist[0].ChildNodes[3].InnerText = txt_productnameupdate.Text;
            nodelist[0].ChildNodes[4].InnerText = txt_brandcodeupdate.Text;
            nodelist[0].ChildNodes[5].InnerText = txt_brandnameupdate.Text;
            doc.Save(path);
            MessageBox.Show("Selected Record Updated Successfully");

        }
        catch
        {
        }
        finally
        {
            //txt_sno.Text = "";
            // txt_companycode.Text = "";
            txt_productcodeupdate.Text = "";
            txt_productnameupdate.Text = "";
            txt_brandcodeupdate.Text = "";
            txt_brandnameupdate.Text = "";

            BarcodeCount form1 = new BarcodeCount();
            form1.BringToFront();
            form1.Invalidate();
            Application.OpenForms["BarcodeCount"].Refresh();
            this.Close();
        }
}

The problem is I want to display the old form, but instead a new Form1 window is opening.

i want to refresh the form1 form form2

Community
  • 1
  • 1
chitra
  • 155
  • 3
  • 5
  • 16
  • If you want only to close the form, why you instantiate a new form1 again? – walther Aug 31 '12 at 10:30
  • 2
    when you do `= new Form1()` you are creating a *new Form1*. You are not referring to the already created one. Is that the issue you are having? – default Aug 31 '12 at 10:33
  • 1
    i want the form1 in deactive mode, and if i click the form2 button, then form2 will close and form1 should in active mode – chitra Aug 31 '12 at 10:36
  • @chitra I am still not understanding your problem. Are you trying to open the **old** Form1 or do you want to create a **new** one? – default Aug 31 '12 at 10:46
  • i want to again redirect to form1 and it should refresh form1 – chitra Aug 31 '12 at 10:52
  • @chitra: I edited your question based on how I interpreted your actual problem to highlight it further. You are free to rollback if I misinterpreted. – Patrick Aug 31 '12 at 11:23
  • All things apart, How does compiler allowed you to declare variable "for"!!!! – Swanand Aug 31 '12 at 11:35
  • @SwanandPurankar: Not that the example in the OP:s question illustrates it, but you can slap a @ in front of for and that's a valid variable name. `int @for = 0` – Patrick Aug 31 '12 at 11:37
  • is there any other options plz; – chitra Aug 31 '12 at 11:53

2 Answers2

3
FormName x = default(FormName);
x = new FormName();
x.Show();
x = null;

x being either Form1 or Form2 when needed.

Ryan McDonough
  • 9,732
  • 3
  • 55
  • 76
  • OP's using C#, yet you post VB? – walther Aug 31 '12 at 10:32
  • You usually shouldn't explicitly set a variable to Nothing. In almost all cases you should let the [garbage collector](http://stackoverflow.com/a/2839/540352) do the cleanup for you. – Laoujin Aug 31 '12 at 10:38
  • @Laoujin I like to be effcient ;) – Ryan McDonough Aug 31 '12 at 10:44
  • 1
    Since when is setting to null an efficient strategy? Never saw that in recommended practices... If that was the optimal approach, why on Earth would anyone invent GC, if all you needed to do was `=null`? Also, are you 100% sure it correctly disposes all the resources associated with the form? (sql connection etc.) – walther Aug 31 '12 at 10:50
  • It just frees up the memory that is no longer required. Otherwise 'click, click, click...oh no memory left' Does the code above answer the question? Yes. OP can decide if he wants to include the last line. – Ryan McDonough Aug 31 '12 at 10:57
  • 1
    @Ryan McDonough: Did you read the link in my previous comment? First, setting x to null will not actually free up memory. The form itself cannot be GC'd since it's currently visible to the user! The only thing that you could free up at this point is the x *reference* (ie 32/64 bits) but even that doesn't happen. Memory has been reserved for x, it doesn't matter if it is pointing to an instance or not. Second, setting `x = null` might actually result in that memory being in use *longer*. (For example when the GC kicks in between the `x.Show()` and `x = null` statements.) – Laoujin Aug 31 '12 at 11:23
  • @Laoujin fair enough, I'll keep that mind in future. – Ryan McDonough Aug 31 '12 at 11:25
  • @walther: No it will not do such thing! Setting `x = null` doesn't really do anything. If the form holds onto unmanaged resources such as an SQLConnection, the cleanup for this should be done by implementing [IDisposable](http://stackoverflow.com/a/538238/540352) (or a more visual explaination [here](http://www.codeproject.com/Articles/4874/Another-Look-At-IDisposable).) Even more off topic, having a form hold onto a SQLConnection is in violation of [Separation of Concerns](http://en.wikipedia.org/wiki/Separation_of_concerns). – Laoujin Aug 31 '12 at 11:32
  • any idea for this, still its not working for me – chitra Sep 01 '12 at 06:47
0

If you make the _for a class field, you can always open that one instance by calling for.Show();.

Example:

private Form1 _for = new Form1();
private void btn2_Click(object sender, EventArgs e)
{
 for.Show();
}
Laoujin
  • 9,962
  • 7
  • 42
  • 69