Hi,
I need to know on how to disable a certain button in another form.
An illustration is something like:
Form1 - Button1
I am using Form2 to access Button1 object and calling a form within my Form2.
---
Form1 frm1 = new Form1();
frm1.button1.enabled = false;
---
That snippet above is not working. The button1 is not disabled.
Thanks.
Loading

Kunal VaishyaRank #990 · 2K reputationPosted May 15, 2012, 1:24 AM EDT
Directly as per requirement
Form1.button1.enabled = false;
or if you show form you should set enable before show
sheen buhay8 reputationPosted May 15, 2012, 1:31 AM EDT
i appreciate your time looking into this however your response is the same as my original post. that code is not working.
thanks.
Kunal VaishyaRank #990 · 2K reputationPosted May 15, 2012, 4:01 AM EDT
frm1.Controls["button1"].Enabled = false;
Using this you can set property
Form2 Objf2 = new Form2();
Objf2.Controls["button1"].Visible = false;
Objf2.Show();
VulpesRank #55 · 29.2K reputationPosted May 15, 2012, 5:39 AM EDT
EhteshamRank #3,979 · 374 reputationPosted May 15, 2012, 6:40 AM EDT
Consider the below code
Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
new Form2(this).Show();
}
}
Form2.cs
public partial class Form2 : Form
{
Form1 FrmRef = null;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 frmref)
: this()
{
this.FrmRef = frmref;
this.checkBox1.Checked = true;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
this.FrmRef.button1.Enabled = this.checkBox1.Checked;
}
}
Note : At design time set the button1 Modifier property on Form1 to internal
hope you got an idea now
sheen buhay8 reputationPosted May 15, 2012, 7:01 PM EDT
I appreciate your time looking into this however I got an error "Object reference not set to an instance of an object."
Thanks.
EhteshamRank #3,979 · 374 reputationPosted May 15, 2012, 9:04 PM EDT
public Form2(Form1 frmref)
: this()
{
this.FrmRef = frmref;
this.checkBox1.Checked = true;
}
Use this.FrmRef.button1.Enabled=true;
The code snippet was tested and it should work
VulpesRank #55 · 29.2K reputationPosted May 16, 2012, 12:24 AM EDT
EhteshamRank #3,979 · 374 reputationPosted May 16, 2012, 12:29 AM EDT
new form2().show();//this will not work with respect to my example
you need to call using
new form2(this).show();
Kunal VaishyaRank #990 · 2K reputationPosted May 16, 2012, 1:35 AM EDT
frm1.Controls["button1"].Enabled = false;
Form2 Objf2 = new Form2();
Objf2.Controls["button1"].Enabled = false;
Objf2.Show();