Connecting Tech Pros Worldwide Forums | Help | Site Map

enable and disable control from different form

Member
 
Join Date: Mar 2008
Posts: 72
#1: Jul 3 '09
In form1 :

Expand|Select|Wrap|Line Numbers
  1.       private void Form1_Load(object sender, EventArgs e)
  2.         {
  3.  
  4.             textBox1.Enabled = false;
  5.          }

in form2:

Expand|Select|Wrap|Line Numbers
  1.         private void button1_Click(object sender, EventArgs e)
  2.         {
  3.  
  4.             Form1 form1 = new Form1();
  5.             form1.textBox1.Enabled =true;
  6.         }
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.

Expert
 
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 427
#2: Jul 3 '09

re: enable and disable control from different form


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:

Expand|Select|Wrap|Line Numbers
  1. private Form1 _frm1;
  2.  
  3. public Form2(Form1 frm1)
  4.         {
  5.             InitializeComponent();
  6.             _frm1 = frm1;
  7.         }
The control can then be accessed as below:

Expand|Select|Wrap|Line Numbers
  1. _frm1.Controls["Textbox1"].Enabled = true; //or whatever property you wish to set
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#3: Jul 3 '09

re: enable and disable control from different form


Please also read all the responses in this thread.
Member
 
Join Date: Mar 2008
Posts: 72
#4: Jul 3 '09

re: enable and disable control from different form


Quote:

Originally Posted by cloud255 View Post

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.

Expand|Select|Wrap|Line Numbers
  1. namespace WindowsFormsApplication1
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         private void Form1_Load(object sender, EventArgs e)
  11.         {
  12.             textBox1.Enabled = false;
  13.         }
  14.  
  15.         private void button1_Click(object sender, EventArgs e)
  16.         {
  17.             Form2 form2 = new Form2();
  18.             form2.Show();
  19.         }
  20.  
  21.  
  22.  
  23.     }
  24. }
Expand|Select|Wrap|Line Numbers
  1. namespace WindowsFormsApplication1
  2. {
  3.     public partial class Form2 : Form
  4.     {
  5.         //public Form2()
  6.         //{
  7.         //    InitializeComponent();
  8.         //}
  9.  
  10.  
  11.  
  12.     private Form1 _frm1;
  13.  
  14.     public Form2(Form1 frm1)
  15.            {
  16.                 InitializeComponent();
  17.                 _frm1 = frm1;
  18.             }
  19.  
  20.     private void button1_Click(object sender, EventArgs e)
  21.     {
  22.         _frm1.Controls["textBox1"].Enabled = true; //or whatever property you wish to set
  23.     }
  24.  
  25.  
  26.  
  27.     }
  28. }
  29.  
I got error (i.e. Form2 form2 = new Form2();) when execute the program.

Error 1 'WindowsFormsApplication1.Form2' does not contain a constructor that takes '0' arguments

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

Please help
Expert
 
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 427
#5: Jul 3 '09

re: enable and disable control from different form


Quote:

Originally Posted by MyMarlboro View Post

Expand|Select|Wrap|Line Numbers
  1. namespace WindowsFormsApplication1
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         private void Form1_Load(object sender, EventArgs e)
  11.         {
  12.             textBox1.Enabled = false;
  13.         }
  14.  
  15.         private void button1_Click(object sender, EventArgs e)
  16.         {
  17.             Form2 form2 = new Form2();
  18.             form2.Show();
  19.         }
  20.  
  21.  
  22.  
  23.     }
  24. }
Expand|Select|Wrap|Line Numbers
  1. namespace WindowsFormsApplication1
  2. {
  3.     public partial class Form2 : Form
  4.     {
  5.         //public Form2()
  6.         //{
  7.         //    InitializeComponent();
  8.         //}
  9.  
  10.  
  11.  
  12.     private Form1 _frm1;
  13.  
  14.     public Form2(Form1 frm1)
  15.            {
  16.                 InitializeComponent();
  17.                 _frm1 = frm1;
  18.             }
  19.  
  20.     private void button1_Click(object sender, EventArgs e)
  21.     {
  22.         _frm1.Controls["textBox1"].Enabled = true; //or whatever property you wish to set
  23.     }
  24.  
  25.  
  26.  
  27.     }
  28. }
  29.  
I got error (as bold in above code i.e. Form2 form2 = new Form2();) when execute the program.

Error 1 'WindowsFormsApplication1.Form2' 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:
Expand|Select|Wrap|Line Numbers
  1. Form2 frm2 = new Form2();
as you have commented out that constructor, you must pass an instance of Form1 as an argument to the constructor:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. Form2 frm2 = new Form2();
  2. 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:

Expand|Select|Wrap|Line Numbers
  1. if(_frm1 != null)
  2. {
  3.      //can access form 1
  4. }
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...
Member
 
Join Date: Mar 2008
Posts: 72
#6: Jul 3 '09

re: enable and disable control from different form


Quote:

Originally Posted by cloud255 View Post

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:

Expand|Select|Wrap|Line Numbers
  1. Form2 frm2 = new Form2();
as you have commented out that constructor, you must pass an instance of Form1 as an argument to the constructor:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. Form2 frm2 = new Form2();
  2. 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:
Expand|Select|Wrap|Line Numbers
  1. if(_frm1 != null)
  2. {
  3.      //can access form 1
  4. }
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)


Expand|Select|Wrap|Line Numbers
  1. namespace WindowsFormsApplication1
  2. {
  3.     public partial class Form2 : Form
  4.     {
  5.         public Form2()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.  
  11.  
  12.     private Form1 _frm1;
  13.  
  14.     public Form2(Form1 frm1)
  15.            {
  16.                 InitializeComponent();
  17.                 _frm1 = frm1;
  18.             }
  19.  
  20.     private void button1_Click(object sender, EventArgs e)
  21.     {
  22.  
  23.  
  24.    if(_frm1 != null)
  25.     {
  26.        //can access form 1
  27.         _frm1.Controls["textBox1"].Enabled = true; //or whatever property you wish to set
  28.     }
  29.  
  30.  
  31.  
  32.  
  33.     }
  34.  
  35.  
  36.  
  37.     }
  38. }
  39.  

Please continue to guide me. thanks.
Expert
 
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 427
#7: Jul 3 '09

re: enable and disable control from different form


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.
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,695
#8: Jul 3 '09

re: enable and disable control from different form


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:
Expand|Select|Wrap|Line Numbers
  1. //in Form1
  2. public bool IsTextBox1Enabled
  3. {
  4.   get
  5.   {
  6.     return textBox1.Enabled;
  7.   }
  8.   set
  9.   {
  10.     textBox1.Enabled = value;
  11.   } 
  12. }
Then in your form2 you will be able to reference it like this:
Expand|Select|Wrap|Line Numbers
  1. _frm1.IsTextBox1Enabled = true;
Member
 
Join Date: Mar 2008
Posts: 72
#9: Jul 3 '09

re: enable and disable control from different form


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?
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#10: Jul 3 '09

re: enable and disable control from different form


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.
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,695
#11: Jul 3 '09

re: enable and disable control from different form


Quote:

Originally Posted by MyMarlboro View Post

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
Expand|Select|Wrap|Line Numbers
  1. //default constructor
  2. public Form2()
  3. {
  4.   InitializeComponent();
  5. }
  6.  
  7. private Form1 _frm1;
  8. //overloaded constructor
  9. public Form2(Form1 frm1)
  10. {
  11.   InitializeComponent();
  12.   _frm1 = frm1;
  13. }
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
Expand|Select|Wrap|Line Numbers
  1. //wherever you want to create and show Form2:
  2. Form2 frm2 = new Form2(this);
  3. 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.
Member
 
Join Date: Mar 2008
Posts: 72
#12: Jul 3 '09

re: enable and disable control from different form


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:
Expand|Select|Wrap|Line Numbers
  1. namespace WindowsFormsApplication1
  2. {
  3.     public partial class frmMain : Form
  4.     {
  5.         public frmMain()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.  
  11.         private void buttonA_Click(object sender, EventArgs e)
  12.         {
  13.             frmTrigger formtrigger = new frmTrigger();
  14.             formtrigger.Show();
  15.  
  16.         }
  17.  
  18.  
  19.         private void frmMain_Load(object sender, EventArgs e)
  20.         {
  21.             textBox.Enabled = false;
  22.         }
  23.  
  24.  
  25.     }
  26. }

frmTrigger:

Expand|Select|Wrap|Line Numbers
  1. namespace WindowsFormsApplication1
  2. {
  3.     public partial class frmTrigger : Form
  4.     {
  5.         public frmTrigger()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.  
  11.         private frmMain _frm1;
  12.  
  13.         //overloaded constructor
  14.         public frmTrigger(frmMain frm1)
  15.         {
  16.             InitializeComponent();
  17.             _frm1 = frm1;
  18.         }
  19.  
  20.  
  21.         private void buttonB_Click(object sender, EventArgs e)
  22.         {
  23.             if (_frm1 != null)
  24.             {
  25.  
  26.                 _frm1.Controls["textBox"].Enabled = true;
  27.  
  28.             }
  29.         }
  30.  
  31.     }
  32. }
Member
 
Join Date: Mar 2008
Posts: 72
#13: Jul 4 '09

re: enable and disable control from different form


Anyone please help me? where is my mistake?
:)
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,695
#14: Jul 4 '09

re: enable and disable control from different form


Try doing what I suggest in this post:
http://bytes.com/topic/c-sharp/answe...rm#post3497713
Expert
 
Join Date: Jun 2008
Location: Pretoria, South Africa
Posts: 427
#15: Jul 6 '09

re: enable and disable control from different form


Hi

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

Expand|Select|Wrap|Line Numbers
  1.         public void Example()
  2.         {
  3.  
  4.         }
  5.  
  6.         public void Example(string name)
  7.         {
  8.  
  9.         }
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:

Expand|Select|Wrap|Line Numbers
  1. in frmTrigger Class
  2.  
  3.  public frmTrigger()
  4.          {
  5.              InitializeComponent();
  6.          }
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(frmMain 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.
Reply