Click here to Skip to main content
15,919,749 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
//I have created button control
C#
public class myButton : System.Windows.Forms.Button
    {
       public myButton()
        {
          //.............
        }
    }


but when I use this button, in property modifiers=private (default)
i want as modifiers=public (default)
Posted
Comments
Philippe Mori 23-Jul-11 14:20pm    
Hum... bad idea to do that.
And not really a good question anyway as it can easily be done in the designer (Modifiers in the Design section of the property grid).
Sergey Alexandrovich Kryukov 24-Jul-11 2:11am    
Agree.
--SA

As a general rule, don't make any component public (or protected). Thiks will lead to badly designed application.

There are almost no valid reason to give access to a control in a form from the outside of the form.

Add events and properties to your form for the few properties you need to get or set and the few events you need to handle.

If you give public access to a component like a button, what do you think will happen if you rename the button or decide to change the button for a link.

By the way, it is not that hard to change that as the designer (at least in C#) allows you to change the access modifier. In rarely make sense to do so.
 
Share this answer
 
I agree, the idea of public access modifier for a control is quite bad. You would actually need internal instead of public, but it's also a bad idea.

The right solution will come if you think why would you need more access. The problem is reduced to the problem of form collaboration. The most robust approach is implementing appropriate interface in form class.

For more detail, see my past solution: How to copy all the items between listboxes in two forms[^]. Read all other suggestions and the discussion.

—SA
 
Share this answer
 
Visual Studio by default makes all controls within a form private (and for good reasons). AFAIK, you cannot change this behavior in your control code. It is not a good idea to make it public. If at all you need to access it from outside the form, then expose it through a public property with only a get accessor.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Jul-11 2:16am    
You are right it terms of method of development; but changing modifier is possible if the control is added not it designer. There are certain cases when this makes sense (generation of UI by non-designed code makes sense, not "public", which is indeed a very bad idea). My 4.
--SA
Sergey Alexandrovich Kryukov 24-Jul-11 2:21am    
As to the solution, I suggest one, please see.
--SA
In InitializeComponent(),this button is declared

like
private System.Windows.Forms.Button myButton;


Make it public :)
 
Share this answer
 
Comments
Philippe Mori 23-Jul-11 14:22pm    
Normally, you should not modify the code inside InitializeComponent. As the file is regenerated when changes are made through the designer, some hand-made changes can be lost. In that particular case, probably not... But whay not change it through the designer?
Sergey Alexandrovich Kryukov 24-Jul-11 2:13am    
I would say doing it in designer code is useless, but a button can be declared and set up not using designer at all; in some cases it makes sense.
--SA
Sergey Alexandrovich Kryukov 24-Jul-11 2:21am    
As to the solution, I suggest one, please see.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900