Click here to Skip to main content
15,064,665 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have created Form1.in Form1 we had one menu strip. by e clicking on menu strip we have to open another form say Form2 and Form1 should be in deactivated state still Form1 closed.

i am using Following code . Is this the correct way of using threads with delegation. still i am using invoke() method it is not working fine . please explain with sample code.
 public delegate void OpenEnv();
    public partial class Home : Form
    {
	............
         public Home()
        {
            InitializeComponent();
            
        }
         private void cCEnvironmentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(OpenEnv));
            t.Start();

            //CreateEnvironment ce = new CreateEnvironment(working_directory);
            //ce.Show();
        }
        public void OpenEnv()
        {
            OpenEnv createenv_delinst = new OpenEnv(opennewform);
            this.Invoke(createenv_delinst);
           
            //CreateEnvironment ce = new CreateEnvironment(working_directory);
            //ce.Show();
        }
        void opennewform()
        {
           
            CreateEnvironment ce = new CreateEnvironment(working_directory);
            ce.Show();
         }
}
Posted
Comments
   
Why? why?!
--SA
johannesnestler 19-Jan-12 8:52am
   
Why you want to open a form on another thread?

This may sound like a silly question, but why are you doing that at all? If what you are after is "we have to open another form say Form2 and Form1 should be in deactivated state still Form1 closed." (which I assume means "until Form2 closed") then just use ShowDialog:
C#
CreateEnvironment ce = new CreateEnvironment(working_directory);
ce.ShowDialog();
And forget all the threading, Form1 will be inactive until Form2 closes at which point execution will continue with the instruction following the ShowDialog method call.

Or am I missing something?
   
Comments
   
Not a silly question, my 5.
--SA
use the following code to open any form on click of a menustrip...
I hope this is the only matter of opening and closing the forms.
C#
Form1 _form1 = new Form1()
this.Hide();
_form1.ShowDialog();
this.Show();

or if you want you can specify the opacity of the forms
Like ...
C#
Form1 _form1 = new Form1()
this.Opacity = 0.1;
_form1.ShowDialog();
this.Opacity = 1.0;
   
v2

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