Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
        private void button13_Click(object sender, EventArgs e)
        {

Form2 f2 = new Form2();
f2.show
button13.enable=false;

}



and in form 2



i want to enable the button 13 through this event


C#
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {

}



how to do it
Posted
Comments
Member 10051049 1-Aug-13 4:25am    
private void button13_Click(object sender, EventArgs e)
{
SplashScreen2 frm1 = new SplashScreen2();
button13.enable= false;
frm1.ShowDialog();
button13.enable= true;

}

It doesn't matter if the button is enabled or not, the form is closing and will be disposed of. The next time it is displayed all controls will be in default condition
 
Share this answer
 
The best way is to have the Form1 handle the Form2 instance closing, and enable the button:
C#
private void button13_Click(object sender, EventArgs e)
   {
   button13.Enable=false;
   Form2 f2 = new Form2();
   f2.FormClosing += new FormClosingEventHandler(f2_FormClosing);
   f2.Show();
   }

private void f2_FormClosing(object sender, FormClosingEventArgs e)
   {
   button13.Enable = true;
   }
 
Share this answer
 
In form 1, handle the form2 closing event and change the enabled status there. If you need to, set some sort of indication in for 2 that the button in form1 should be enabled/disabled, and check that value in form1, something like this:

C#
private void Form2_Closing(...)
{
    Form2 form = sender as Form2;
    if (form.SomeValue == true)
    {
        button13.Enabled = true;
    }
}
 
Share this answer
 
Hi mohanrajkiller,

Use a custom constructor instead and pass the value that you need.

For your reference:
http://www.yoda.arachsys.com/csharp/constructors.html[^]


Regards,
Eduard
 
Share this answer
 
This answer is based on the assumption that this is a WinForms Project, that Form1 is the "Main Form," started automatically when the Project is compiled and run, and that "button13" is in Form1 !

All the code examples shown here are meant to be inside Form1.

1. Why would you recreate Form2 every time Button13 is pressed ?

I suggest you create Form2 once, in the Form1_Load Event Handler, for example:
private Form2 f2;

private void Form1_Load(object sender, EventArgs e
{
   f2 = new Form2();

  // and then, as OriginalGriff suggested:
  // define an EventHandler for Form2's Closing Event
  f2.Closing += new CancelEventHandler(f2_Closing);
}

2. Then, in the Closing EventHandler for Form2, I would not close Form2 ! : I'd use this:
C#
private void f2_Closing(object sender, CancelEventArgs e)
{
    // enable button13
    button13.Enabled = true;

    // hide Form2 and cancel the Closing Event !
    f2.Hide();
    e.Cancel = true;
}

3. So, then, Form1's button13 Click EventHandler would look like this:
C#
private void button13_Click(object sender, EventArgs e)
{
  // disable button13
  button13.Enabled = false;
  // show Form2
  f2.Show();
}
Discussion: unless there's a very good reason you want to use a fresh/new version of Form2 every time you click button13 ... and there could be one ... I'd suggest this approach.

If we assume that Form1 is the "Main Form" which is launched in the Program.cs file, then closing Form1 ... even if Form2 is open ... will automatically close Form2. In other words: you do not have to worry about "dis-connecting" Form2's Closing Event Handler.

... edit #1 ...

If you are ever in a situation where Form1 is not the main form used as the argument to 'Run' in the Program.cs file, and you wish to guarantee that Form2 will be closed if Form1 is closed, you can add this code to Form1:
C#
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (f2 != null) f2.Dispose();
}
... end edit #1 ...
 
Share this answer
 
v3
Comments
mohanrajkiller 23-Nov-11 0:21am    
your solution is perfect !!!!!!!
C#
Form1 frm=Application.OpenForms["Form1"];
if (frm!=null)
{
frm.controls["button13"].enabled=true;
}


if button13 is public you can do it like this:

C#
Form1 frm=Application.OpenForms["Form1"];
if (frm!=null)
{
frm.button13.enabled=true;
 
Share this answer
 

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