enable and disable control from different form | Member | | Join Date: Mar 2008
Posts: 72
| |
In form1 : - private void Form1_Load(object sender, EventArgs e)
-
{
-
-
textBox1.Enabled = false;
-
}
in form2: - 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.
| | Expert | | Join Date: Jun 2008 Location: Pretoria, South Africa
Posts: 427
| | | 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: - private Form1 _frm1;
-
-
public Form2(Form1 frm1)
-
{
-
InitializeComponent();
-
_frm1 = frm1;
-
}
The control can then be accessed as below: - _frm1.Controls["Textbox1"].Enabled = true; //or whatever property you wish to set
| | Lives Here | | Join Date: Sep 2006
Posts: 12,070
| | | re: enable and disable control from different form
Please also read all the responses in this thread.
| | Member | | Join Date: Mar 2008
Posts: 72
| | | re: enable and disable control from different form Quote:
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.
- 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 = new Form2();
-
form2.Show();
-
}
-
-
-
-
}
-
}
- 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 '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
| | | re: enable and disable control from different form Quote:
Originally Posted by MyMarlboro - 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 = new Form2();
-
form2.Show();
-
}
-
-
-
-
}
-
}
- 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 '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: -
Form2 frm2 = new Form2();
as you have commented out that constructor, you must pass an instance of Form1 as an argument to the constructor: - 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: -
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: - 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...
| | Member | | Join Date: Mar 2008
Posts: 72
| | | re: enable and disable control from different form Quote:
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: -
Form2 frm2 = new Form2();
as you have commented out that constructor, you must pass an instance of Form1 as an argument to the constructor: - 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: -
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: - 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) - 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.
| | Expert | | Join Date: Jun 2008 Location: Pretoria, South Africa
Posts: 427
| | | 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.
|  | Forum Leader | | Join Date: Apr 2008 Location: San Antonio, TX (USA)
Posts: 2,695
| | | 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: - //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: - _frm1.IsTextBox1Enabled = true;
| | Member | | Join Date: Mar 2008
Posts: 72
| | | 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
| | | 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.
|  | Forum Leader | | Join Date: Apr 2008 Location: San Antonio, TX (USA)
Posts: 2,695
| | | re: enable and disable control from different form Quote:
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 -
//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 - //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.
| | Member | | Join Date: Mar 2008
Posts: 72
| | | 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: - 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: - 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;
-
-
}
-
}
-
-
}
-
}
| | Member | | Join Date: Mar 2008
Posts: 72
| | | re: enable and disable control from different form
Anyone please help me? where is my mistake?
:)
|  | Forum Leader | | Join Date: Apr 2008 Location: San Antonio, TX (USA)
Posts: 2,695
| | | re: enable and disable control from different form | | Expert | | Join Date: Jun 2008 Location: Pretoria, South Africa
Posts: 427
| | | 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: - 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: -
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(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.
|  | Similar C# / C Sharp bytes | | | Forums
Visit our community forums for general discussions and latest on Bytes
/bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 229,155 network members.
|