开发者

C# disable listbox from parent form

I have an mdi child form as a dockable content in my application and I want to disable/enable a listbox in it from the parent form depending on a certain event. I thought this would be simple as:

开发者_如何学运维
_child.listBox1.Enabled = false;

But it doesn't seem to disable it. _child is an object reference of the mdi child form btw. Why does it not work and how can I fix this?


_child probably refers to a different instance of the child form.

Make sure that _child refers to the same instance that you called Show() on.


Can't you create a function on your MDI child which would disable the listbox, you could call from the MDI parent?

I guess that here listBox1 is private (which is the default if you have constructed your form using VS designer)


Although it works, exposing the control of a form as a public property is considered a bad design practice.

Suppose that at some point in the future, you will have to change the internal ListBox into some other type, such as ListView, in order to add some functionality.

In this scenario, if you create a method called DisableList on the form, you will only have to change one place in code, to update the way the list should be disabled.

But if you choose the method of writing code such as _client.listbox1.Enabled = false;, you will have to go through all the pieces of code that touch the ListBox, and update them.

A very important principal in design is to avoid exposing the internal implementation details of class to those that have to use it. In this case, you will benefit if the parent form won't have to know that the list is implemented as a ListBox.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜