Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,

i have two windows form 1)form1 2)form2.I want to open form 2 in two ways.1st way is independently open throgh the form2 name with no argument pass class constructor(form2 frm2=new form2()).2 nd way is form1 has id value that will pass to form2 through one argument constructor.like( form2 frm2=new form2(id)).the issue is whenever the each constrctor is loaded, the controls in the form2 is intialized.the question is in memory management all the controls has been initialized twice due to this process.so that memory could be allcoated twice for a single controls.i want to avoid it by one time intializecomponent for both constrctors.Is there any way to solve this issue?
Posted

Hi,

if you are creating new instance of the Form2 then memory will be allocated separately. if you want to create only single instance of the Form2 then you need to use singleton class.

Check Simple Singleton Forms[^] code project article.

Hope this helps you,

Thanks
-Amit Gajjar
 
Share this answer
 
Try This

//Form 1//
C#
sing System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }

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


//Form 2//

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

namespace WindowsFormsApplication2
{
    public partial class Form2 : Form1
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
           this.Text="I am Form 2- You don't need to design me";
  
        }
    }
}
 
Share this answer
 
v3

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