Click here to Skip to main content
15,897,151 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
have two forms , The first one contains some textboxes that the Enabled Proprieties is set to false by default , i want to set the Enabled to true when i'm closing the second form .

What I have tried:

I set the modifies to public for the textboxes

Code
private void Data_FormClosing(object sender, FormClosingEventArgs e)
    {
        Main frm = new Main();

        frm.bSave.Enabled = true;
        frm.bEdit.Enabled = true;
        frm.tClient.Enabled = true;
        frm.tType.Enabled = true;
        frm.tFirst.Enabled = true;
        frm.tSecond.Enabled = true;
        frm.tDriver.Enabled = true;
        frm.tCity.Enabled = true;
        frm.tDriver.Enabled = true;
        frm.tNumber.Enabled = true;
        frm.tPrice.Enabled = true;
    }
Posted
Updated 29-Sep-17 22:37pm

You create a new main form, and then try to affect it's controls that has nothing to do with the existing one, and will not affect anything on it.
In addition, your second form should not even know that the main form exists, much less be able to access it's controls.

Move the form closing handler into the main form, so it fires when the second form closes, and the main form can then affect it's own controls with no problem.
 
Share this answer
 
Comments
Mohamed Ali Khamis 30-Sep-17 4:38am    
thank you ,i found a solution and i'm testing it to find if it will cause any kind of errors
Forms should NEVER know about other forms, nor the controls on them. Doing so ties them together so the code/form cannot be reused elsewhere.

Your second form needs to expose its own events that another form can subscribe to. These events would signal what's happening on the form and it's up to the subscribing form to determine how to respond to those events.

Forms should only be concerned with their own controls, not with the controls on other forms.
 
Share this answer
 
Comments
Mohamed Ali Khamis 30-Sep-17 4:38am    
thank you ,i found a solution and i'm testing it to find if it will cause any kind of errors
I managed to control the textboxes by adding this code to The First Form

public partial class Main : Form
   {
       private static Main frm;
       static void frm_FormClosed(object sender,FormClosedEventArgs e)
       {
           frm = null;
       }
       public static Main getMainForm
       {
           get
           {
               if(frm==null)
               {
                   frm = new Main();
                   frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);

               }
               return frm;
           }
       }


and by double clicking the datagridview in the the second form

private void dataGridView1_DoubleClick(object sender, EventArgs e)
       {

           Main.getMainForm.bSave.Enabled = true;
           Main.getMainForm.bSave.Enabled = true;
           Main.getMainForm.tClient.Enabled = true;
           Main.getMainForm.tType.Enabled = true;
           Main.getMainForm.tFirst.Enabled = true;
           Main.getMainForm.tSecond.Enabled = true;
           Main.getMainForm.tDriver.Enabled = true;
           Main.getMainForm.tCity.Enabled = true;
           Main.getMainForm.tNumber.Enabled = true;
           Main.getMainForm.tPrice.Enabled = true;

           this.Close();

       }
 
Share this answer
 
Comments
Dave Kreskowiak 30-Sep-17 9:19am    
Yeah ... that's not going to work. Your STILL creating a NEW instance of the main form, NOT using the existing instance of the form.

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