Set Windows Forms Controls as Public

p-plater used Ask the Experts™
Is there anything wrong with setting windows forms controls to Public?

I am wanting to make a Project of "Search Forms" that I can simply import into different Projects that access the same Database design.
But I can't access the form controls from the other Projects unless the Controls are set to Public
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Karrtik IyerSoftware Architect

Hi @p-plater,

I suggest to make the search part which you want to reuse across multiple projects as user control and use it across all forms including Search-Forms.
By making control public, the form loses the control (does not know) when some property/state of a control is modified.  Also form may contain lot more things then just the control that you want to be reuse, in that case it is a overhead for every other project to import all the controls via form although they might be interested in only using one of the controls.

Top Expert 2015
The wrong thing about making your controls Public is that it opens a security hole in your application.

Anybody who as access to your assembly can then manipulate the form, and potentially use it for purposes that you would not like.

One of my students suffered problems with that in the past. The logo of the company in their standard About Form was replaced by a naked girl. Somebody else used such a form to enter data in the database, bypassing some validation and breaking the integrity of the database. In a search form, somebody who does not have access to the database might be able to go through and retrieve information to which it does not have access.

What I do in my own applications is the following.

- Put the form in a dll.

- In that dll, design the executables that need that form as Friend Assemblies of your dll.

- Use that dll with your application to be able to reference the form.

Assemblies that are designed as Friend are able to use anything that is designed as Friend in the dll. Because controls are Friend by default, then you will have access to it only from your own applications. Somebody who simply grabs your dll and try to use it won't be able to to more than display the form. You can event prevent that by declaring the Form itself as being Friend.
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Think of controls as private variables within a class--after all, a form is a class, right? If you're following best practices for class design, then you wouldn't expose your private variables to other classes because then you lose control over how the values are used. You would instead create public properties which provide external entities some mechanism for accessing your variables. IMO, you would do the same for controls. Controls are implementation details of your form. You don't want to expose implementation details to entities outside of the class. The implementation may change, and you don't want to have to update 20 different places that depend on your implementation.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Top Expert 2015

Kaufmed is right if you understand what he says and are at a level to implement these features.

I gave you the easy way to do what you wanted, judging your expertise by the questions that you asked in the past. I might be wrong in my judgement.

If you are up to it, he gave you the proper way to do it.


If I understand what Kaufmed is saying correctly then what I need to do is put the Search Forms in the BLL DLL?
So when the UI sends a Search to the BLL if it needs to display a search form then the BLL displays the form then it can return the selected values back to the UI.

Is this Correct?

The only drawback in doing it that way is it makes the BLL Windows Forms dependant. (If I need to make a WPF application it would need a Modified BLL)

Am I Understanding it correctly?
Top Expert 2015


But if you think to eventually go to WPF, although I am no expert on that subject, it seems that there are ways to use windows form with WPF applications.

You might thus use conditional compilation to automatically create 2 different versions of the same dll from the same source code. One that displays the Windows Form normally, and the other one that uses the tools in the System.Windows.Forms.Integration Namespace.


Many Thanks Yet Again.

Experts Exchange is a wealth of Knowledge.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial