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 VaishyaPosted May 16, 2012, 5:35 AM
frm1.Controls["button1"].Enabled = false;
Form2 Objf2 = new Form2();
Objf2.Controls["button1"].Enabled = false;
Objf2.Show();
EhteshamPosted May 16, 2012, 4:29 AM
new form2().show();//this will not work with respect to my example
you need to call using
new form2(this).show();
VulpesPosted May 16, 2012, 4:24 AM
EhteshamPosted May 16, 2012, 1:04 AM
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
sheen buhayPosted May 15, 2012, 11:01 PM
I appreciate your time looking into this however I got an error "Object reference not set to an instance of an object."
Thanks.
EhteshamPosted May 15, 2012, 10:40 AM
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
VulpesPosted May 15, 2012, 9:39 AM
Kunal VaishyaPosted May 15, 2012, 8:01 AM
frm1.Controls["button1"].Enabled = false;
Using this you can set property
Form2 Objf2 = new Form2();
Objf2.Controls["button1"].Visible = false;
Objf2.Show();
sheen buhayPosted May 15, 2012, 5:31 AM
i appreciate your time looking into this however your response is the same as my original post. that code is not working.
thanks.
Kunal VaishyaPosted May 15, 2012, 5:24 AM
Directly as per requirement
Form1.button1.enabled = false;
or if you show form you should set enable before show