3

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.

1
  • 1
    Why has somebody down voted this question? It's perfectly reasonable. At least state what your issue is! Commented Dec 14, 2011 at 9:19

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It's really the only correct way of doing it. I suspect there might well be other operations required when disabling that combobox anyway which could also be done in the property. Setting a number of things false at the same time perhaps, or changing a label... whatever is required.
3

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;
        }
    }
}

3 Comments

And the change would then be visible in the Form.Designer.cs file.
@Otiel it should be visible in designer.cs file
That's my point exactly. Was just commenting to precise that :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.