Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C# C#4.0
        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
 

private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
 
}
 

how to do it
Posted 21-Nov-11 3:30am
Comments
Member 10051049 at 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;

}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The best way is to have the Form1 handle the Form2 instance closing, and enable the button:
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;
   }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
 
private void Form2_Closing(...)
{
    Form2 form = sender as Form2;
    if (form.SomeValue == true)
    {
        button13.Enabled = true;
    }
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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:
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:
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:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (f2 != null) f2.Dispose();
}
... end edit #1 ...
  Permalink  
v3
Comments
mohanrajkiller at 23-Nov-11 0:21am
   
your solution is perfect !!!!!!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Form1 frm=Application.OpenForms["Form1"];
if (frm!=null)
{
frm.controls["button13"].enabled=true;
}
 
if button13 is public you can do it like this:
 
Form1 frm=Application.OpenForms["Form1"];
if (frm!=null)
{
frm.button13.enabled=true;
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Mathew Soji 334
1 Maciej Los 295
2 BillWoodruff 290
3 Afzaal Ahmad Zeeshan 257
4 DamithSL 225
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,045


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 23 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100