Set Windows Forms Controls as Public

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
p-platerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karrtik IyerSoftware ArchitectCommented:
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.
https://msdn.microsoft.com/en-us/library/aa302342.aspx
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.

Thanks,
Karrtik
Jacques Bourgeois (James Burger)PresidentCommented:
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.
kaufmed   ( ⚆ _ ⚆ )I asked the operating system what I could do to become a better programmer. It said, "Let me give you some pointers."Commented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Jacques Bourgeois (James Burger)PresidentCommented:
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.
p-platerAuthor Commented:
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?
Jacques Bourgeois (James Burger)PresidentCommented:
Yes.

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.
p-platerAuthor Commented:
Many Thanks Yet Again.

Experts Exchange is a wealth of Knowledge.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.