My current project requires me to disable combo boxes from Form 2 when edit button is clicked in Form 1. What I would like to know is how I can disable a combo box that's in Form 2 from Form 1.
I tried
IDComboBox4.Enabled = false;
in Form 1 however I get and error that says
"The Name 'IDComboBox4' does not exist in the current context".
Update:
I attempted both answers however sadly neither worked. Now I get a syntax error
"An object reference is required for the non-static field, method, or property"
when trying code
dlgForm.ComboBox4Enabled = false;
in Form1 with code
public bool ComboBox4Enabled
{
get
{
return IDComboBox4.Enabled;
}
set
{
IDComboBox4.Enabled = value;
}
}
in Form2 which is also dlgForm. I feel like I'm overlooking something basic however just cant put my finger on it at the moment. If you would like some snippets of my code let me know. Any help is appreciated. Thanks again.
You could just make IDComboBox4 public and use form2.IDComboBox4.Enabled = false; if you like but I would STRONGLY suggest you create a property on form 2 that allows you to do it without accessing the internals of form2. Something like this:
public bool ComboBox4Enabled{
get{return IDComboBox4.Enabled;}
set{IDComboBox4.Enabled = value;}
}
Then use form2.ComboBox4Enabled = false; to set it.
Whilst you're at it - you really should rename your comboboxes etc. so that they make sense for the next developer who comes along. _cbbJobTitle or something.
Note : this is example for one button you can change this to combobox
Select your button in designer, go to it's properties and change "Modifiers" property from Private to Public.
alt text http://xmages.net/out.php/i170429_1.png
Then you can get access to it from another class, something like this:
public static class Test
{
public static void DisalbeMyButton()
{
var form = Form.ActiveForm as Form1;
if (form != null)
{
form.MyButton.Enabled = false;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With