473,321 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

enable and disable control from different form

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.
Jul 3 '09 #1
14 10681
cloud255
427 Expert 256MB
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
Jul 3 '09 #2
r035198x
13,262 8TB
Please also read all the responses in this thread.
Jul 3 '09 #3
@cloud255
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
Jul 3 '09 #4
cloud255
427 Expert 256MB
@MyMarlboro
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...
Jul 3 '09 #5
@cloud255
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.
Jul 3 '09 #6
cloud255
427 Expert 256MB
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.
Jul 3 '09 #7
Curtis Rutland
3,256 Expert 2GB
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;
Jul 3 '09 #8
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?
Jul 3 '09 #9
r035198x
13,262 8TB
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.
Jul 3 '09 #10
Curtis Rutland
3,256 Expert 2GB
@MyMarlboro
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.
Jul 3 '09 #11
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. }
Jul 3 '09 #12
Anyone please help me? where is my mistake?
:)
Jul 4 '09 #13
Curtis Rutland
3,256 Expert 2GB
Try doing what I suggest in this post:
http://bytes.com/topic/c-sharp/answe...rm#post3497713
Jul 4 '09 #14
cloud255
427 Expert 256MB
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.
Jul 6 '09 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Bob Bedford | last post by:
I've this code: function checkdate(FormSubmit){ alert(document.getElementById('Mois').value); if(eval(document.getElementById('Mois'))>0 && eval(document.getElementById('Annee'))>0){...
1
by: hortoristic | last post by:
We are using JavaScript to Enable/Disable certain fields on web pages based on business rules. A simple example is if when using an option type tag, and the two options are Yes and No. If YES...
3
by: DBQueen | last post by:
I have a form with lines of controls. On some of the lines there are 3 controls (call them A,B,C); other lines have only control A. The controls have been numbered sequentially (Q20, Q21....Q76)...
2
by: buran | last post by:
Dear ASP.NET Programmers, I have a web user control (a search menu) which has 2 validation controls (one for input and another for the search criterion). I am including this search user control...
6
by: Jonathan | last post by:
Hi, I have 2 forms "Form1" & "Form2" In Form1, there is a button to click to open Form2 and set Form1.Enabled = False However, when i finish using Form2, i cannot Enable Form1? In Form2, i...
7
by: Phil | last post by:
on my form I have a checkbox and a tab control when the checkbox is true I wish to enable the controls on a single page of the tab control, is this possible or do I need to enable each control on...
4
by: RedHair | last post by:
Development: Windows 2003 Production :Windows 2003 ASP.NET 2.0 + C# I want to disable the cache feature of all pages in dev but enable it in production environments. What's the solution? If...
3
AccessIdiot
by: AccessIdiot | last post by:
I was successful with help from another thread (this one ) in enabling and disabling a form/subform with a button. Essentially you press a button on the form and the form controls are disabled but...
1
by: Birky | last post by:
I have a form (Custom_Code) that has a sub form (Support_Groups_Form) associated to it. I have a need to enable and disable the fields depending on user selection but I am having trouble controlling...
2
by: Naushad | last post by:
Hi all, I am using the countinous form. I want to Enable/Disable the some fields for perticular records as per the following condition when open the form. I have written this code in "On Current...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.