enable and disable control from different form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyMarlboro
    New Member
    • Mar 2008
    • 71

    enable and disable control from different form

    In form1 :

    Code:
          private void Form1_Load(object sender, EventArgs e)
            {
    
                textBox1.Enabled = false;
             }

    in form2:

    Code:
            private void button1_Click(object sender, EventArgs e)
            {
              
                Form1 form1 = new Form1();
                form1.textBox1.Enabled =true;
            }
    The enable function doesnt work ... and i realized that it caused by the completely new instance of my form1 class.
    So my question is how to get to a reference to that specific form instance?
    Thanks.
  • cloud255
    Recognized Expert Contributor
    • Jun 2008
    • 427

    #2
    Hi

    One way around this is to add a reference to form1 in form2's constructor, you can then place that in a variable inside the form2 class.

    In the form2 class:

    Code:
    private Form1 _frm1;
    
    public Form2(Form1 frm1)
            {
                InitializeComponent();
                _frm1 = frm1;
            }
    The control can then be accessed as below:

    Code:
    _frm1.Controls["Textbox1"].Enabled = true; //or whatever property you wish to set

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Please also read all the responses in this thread.

      Comment

      • MyMarlboro
        New Member
        • Mar 2008
        • 71

        #4
        Originally posted by cloud255
        Hi

        One way around this is to add a reference to form1 in form2's constructor, you can then place that in a variable inside the form2 class.
        Code:
        namespace WindowsFormsApplication1
        {
            public partial class Form1 : Form
            {
                public Form1()
                {
                    InitializeComponent();
                }
        
                private void Form1_Load(object sender, EventArgs e)
                {
                    textBox1.Enabled = false;
                }
        
                private void button1_Click(object sender, EventArgs e)
                {
                    Form2 form2 = [B]new Form2();[/B]
                    form2.Show();
                }
        
        
        
            }
        }
        Code:
        namespace WindowsFormsApplication1
        {
            public partial class Form2 : Form
            {
                //public Form2()
                //{
                //    InitializeComponent();
                //}
        
                
        
            private Form1 _frm1;
             
            public Form2(Form1 frm1)
                   {
                        InitializeComponent();
                        _frm1 = frm1;
                    }
        
            private void button1_Click(object sender, EventArgs e)
            {
                _frm1.Controls["textBox1"].Enabled = true; //or whatever property you wish to set
            }
        
                
        
            }
        }
        I got error (i.e. Form2 form2 = new Form2();) when execute the program.

        Error 1 'WindowsFormsAp plication1.Form 2' does not contain a constructor that takes '0' arguments

        SO how to call the Form2 from Form1 ??
        I'm getting confused

        Please help

        Comment

        • cloud255
          Recognized Expert Contributor
          • Jun 2008
          • 427

          #5
          Originally posted by MyMarlboro
          Code:
          namespace WindowsFormsApplication1
          {
              public partial class Form1 : Form
              {
                  public Form1()
                  {
                      InitializeComponent();
                  }
          
                  private void Form1_Load(object sender, EventArgs e)
                  {
                      textBox1.Enabled = false;
                  }
          
                  private void button1_Click(object sender, EventArgs e)
                  {
                      Form2 form2 = [B]new Form2();[/B]
                      form2.Show();
                  }
          
          
          
              }
          }
          Code:
          namespace WindowsFormsApplication1
          {
              public partial class Form2 : Form
              {
                  //public Form2()
                  //{
                  //    InitializeComponent();
                  //}
          
                  
          
              private Form1 _frm1;
               
              public Form2(Form1 frm1)
                     {
                          InitializeComponent();
                          _frm1 = frm1;
                      }
          
              private void button1_Click(object sender, EventArgs e)
              {
                  _frm1.Controls["textBox1"].Enabled = true; //or whatever property you wish to set
              }
          
                  
          
              }
          }
          I got error (as bold in above code i.e. Form2 form2 = new Form2();) when execute the program.

          Error 1 'WindowsFormsAp plication1.Form 2' does not contain a constructor that takes '0' arguments

          SO how to call the Form2 from Form1 ??
          I'm getting confused

          Please help
          Hi,

          Ok lets look at this bit by bit.
          In form2, you have commetted out lines 5-8, the default constructor and created a different constructor in lines 14-18, this new constructor has one paramater namely Form1 frm1.

          So now when you want to create a new instance of Form2 you cannot do:
          Code:
          Form2 frm2 = new Form2();
          as you have commented out that constructor, you must pass an instance of Form1 as an argument to the constructor:

          Code:
          Form2 frm2 = new Form2(this);
          I suggest that you uncomment lines 5-8 of the Form1 class, thus overloading the classes constructor in that way you can use both of the below calls:

          Code:
          Form2 frm2 = new Form2();
          Form2 frm2 = new Form2(this);
          One thing you will have to do with this method is ensure that your private reference to form1 _frm1 is not null before you try to access it, so each time before you try to do anything with _frm1 do the following check:

          Code:
          if(_frm1 != null)
          {
               //can access form 1
          }
          if you dont do that, you will get null reference exceptions.

          Apologies for the long explanation, but feel free to post if anything is still unclear...

          Comment

          • MyMarlboro
            New Member
            • Mar 2008
            • 71

            #6
            Originally posted by cloud255
            Hi,

            Ok lets look at this bit by bit.
            In form2, you have commetted out lines 5-8, the default constructor and created a different constructor in lines 14-18, this new constructor has one paramater namely Form1 frm1.

            So now when you want to create a new instance of Form2 you cannot do:
            Code:
            Form2 frm2 = new Form2();
            as you have commented out that constructor, you must pass an instance of Form1 as an argument to the constructor:

            Code:
            Form2 frm2 = new Form2(this);
            I suggest that you uncomment lines 5-8 of the Form1 class, thus overloading the classes constructor in that way you can use both of the below calls:

            Code:
            Form2 frm2 = new Form2();
            Form2 frm2 = new Form2(this);
            One thing you will have to do with this method is ensure that your private reference to form1 _frm1 is not null before you try to access it, so each time before you try to do anything with _frm1 do the following check:


            I have updated the Form2 as below:
            Code:
            if(_frm1 != null)
            {
                 //can access form 1
            }
            if you dont do that, you will get null reference exceptions.

            Apologies for the long explanation, but feel free to post if anything is still unclear...
            i do appreciate your explanation.
            but i stil facing the problem. The textBox1 cannot be enabled.
            i think the culprit is here if(_frm1 != null)


            Code:
            namespace WindowsFormsApplication1
            {
                public partial class Form2 : Form
                {
                    public Form2()
                    {
                        InitializeComponent();
                    }
            
                    
            
                private Form1 _frm1;
                 
                public Form2(Form1 frm1)
                       {
                            InitializeComponent();
                            _frm1 = frm1;
                        }
            
                private void button1_Click(object sender, EventArgs e)
                {
                    
            
               if(_frm1 != null)
                {
                   //can access form 1
                    _frm1.Controls["textBox1"].Enabled = true; //or whatever property you wish to set
                }
            
            
                    
                   
                }
            
                    
            
                }
            }

            Please continue to guide me. thanks.

            Comment

            • cloud255
              Recognized Expert Contributor
              • Jun 2008
              • 427

              #7
              That should work, are you calling the Form2(Form1 frm1) constructor in Form1?
              You need to call this constructor or _frm1 will always be null.

              Comment

              • Curtis Rutland
                Recognized Expert Specialist
                • Apr 2008
                • 3264

                #8
                In addition to cloud's excellent advice, you will need to make sure that the textbox is either declared as public, or, a better solution, expose a public property on Form1, that will handle setting the textbox's enabled flag.

                Something along these lines:
                Code:
                //in Form1
                public bool IsTextBox1Enabled
                {
                  get
                  {
                    return textBox1.Enabled;
                  }
                  set
                  {
                    textBox1.Enabled = value;
                  } 
                }
                Then in your form2 you will be able to reference it like this:
                Code:
                _frm1.IsTextBox1Enabled = true;

                Comment

                • MyMarlboro
                  New Member
                  • Mar 2008
                  • 71

                  #9
                  Thanks cloud255 and insertAlias.
                  it still doesnt work. i had set the textBox1 to public.
                  still.. it's because of if (_frm1 != null) statement.
                  Anything that i missed?

                  calling the Form2(Form1 frm1) constructor in Form1? what it means?

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    I want to help too but those variable names in your code keep driving me away.
                    What is Form1 and Form2 trying to model? They should be named after what they are really trying to model.

                    Comment

                    • Curtis Rutland
                      Recognized Expert Specialist
                      • Apr 2008
                      • 3264

                      #11
                      Originally posted by MyMarlboro
                      calling the Form2(Form1 frm1) constructor in Form1? what it means?
                      OK, I'll explain what he means here. This is a code snippet from your Form2.CS
                      Code:
                      //default constructor
                      public Form2()
                      {
                        InitializeComponent();
                      }
                       
                      private Form1 _frm1;
                      //overloaded constructor
                      public Form2(Form1 frm1)
                      {
                        InitializeComponent();
                        _frm1 = frm1;
                      }
                      Notice the one I commented called "Overloaded constructor." When you create an instance of Form2 from Form1, you should use the overloaded constructor so you have a reference back to Form1.

                      For example, in Form1.CS
                      Code:
                      //wherever you want to create and show Form2:
                      Form2 frm2 = new Form2(this);
                      frm2.Show();
                      this refers to the current instance of Form1. You pass that reference to the overloaded constructor of Form2. That way, you have a link back to the Form1 instance so you can get and set it's properties.

                      Also, r0 is very much correct. You need to use more descriptive names than Form1 and Form2 so that you can easily remember what each one does.

                      Comment

                      • MyMarlboro
                        New Member
                        • Mar 2008
                        • 71

                        #12
                        Noted.

                        i've changed my name of the form.
                        Basicaly in frmMain i have 2 controls which are textBox and buttonA; initially textBox is set to disable.
                        The buttonA is used to trigger frmTrigger's textBox; in this form i have 1 control which is buttonB that is used to enable the textBox in frmMain.

                        frmMain:
                        Code:
                        namespace WindowsFormsApplication1
                        {
                            public partial class frmMain : Form
                            {
                                public frmMain()
                                {
                                    InitializeComponent();
                                }
                        
                          
                                private void buttonA_Click(object sender, EventArgs e)
                                {
                                    frmTrigger formtrigger = new frmTrigger();
                                    formtrigger.Show();
                                    
                                }
                        
                        
                                private void frmMain_Load(object sender, EventArgs e)
                                {
                                    textBox.Enabled = false;
                                }
                        
                                     
                            }
                        }

                        frmTrigger:

                        Code:
                        namespace WindowsFormsApplication1
                        {
                            public partial class frmTrigger : Form
                            {
                                public frmTrigger()
                                {
                                    InitializeComponent();
                                }
                        
                                
                                private frmMain _frm1;
                        
                                //overloaded constructor
                                public frmTrigger(frmMain frm1)
                                {
                                    InitializeComponent();
                                    _frm1 = frm1;
                                }
                        
                        
                                private void buttonB_Click(object sender, EventArgs e)
                                {
                                    if (_frm1 != null)
                                    {
                        
                                        _frm1.Controls["textBox"].Enabled = true;
                        
                                    }
                                }
                        
                            }
                        }

                        Comment

                        • MyMarlboro
                          New Member
                          • Mar 2008
                          • 71

                          #13
                          Anyone please help me? where is my mistake?
                          :)

                          Comment

                          • Curtis Rutland
                            Recognized Expert Specialist
                            • Apr 2008
                            • 3264

                            #14
                            Try doing what I suggest in this post:

                            Comment

                            • cloud255
                              Recognized Expert Contributor
                              • Jun 2008
                              • 427

                              #15
                              Hi

                              frmTrigger still has no reference to frmMain because in frmMain you call the default constructor for frmTirgger. Look at this example below:

                              Code:
                                      public void Example()
                                      {
                              
                                      }
                              
                                      public void Example(string name)
                                      {
                              
                                      }
                              The above functions both have the same name, but different parameters, the first function accepts 0 arguments (like a default constructor) while the second function accepts one argument.

                              Example(string name) can thus have a value passed to it and you can reference that value within the function.

                              You always call frmTrigger formtrigger = new frmTrigger(); see there is no parameter in this constructor, thus the code it calls is:

                              Code:
                              in frmTrigger Class
                              
                               public frmTrigger()
                                       {
                                           InitializeComponent();
                                       }
                              In the above constructor, there is no reference to frmMain, so you never execute _frm1 = frm1; thus _frm1 is always null and your if statement will always return false.

                              To assign a value to _frm1 we must call the public frmTrigger(frmM ain frm1) constructor,

                              Remember that overloaded functions (functions with the same name but different arguments) are different sections of code that behave in a unique manner.

                              Without passing a reference to frmMain, frmTrigger cannot access it. In this scenario frmMain is just like a variable that must be passed from one function to another.

                              I hope this clarifies what is actually going wrong here. If you are new to development, i suggest you really try this until you understand what is happening as passing parameters is a common and very good programming technique.

                              Comment

                              Working...