Results 1 to 18 of 18

Thread: [RESOLVED] How to modify default control property values, or put custom controls in toolbox

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2010
    Posts
    39

    Resolved [RESOLVED] How to modify default control property values, or put custom controls in toolbox

    Is there a way to change the default property values of the native VB.Net controls? I know I can create my own control in code that inherits from one of the native controls and set the properties any way I want in the constructor, but that doesn't help me when I want to drag a control from the toolbox onto a Form. I'm thinking specifically of the Label control, which has its AutoSize property annoyingly set to True by default. How can I modify the default values of the native controls, or make my customized controls appear in the toolbox?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,504

    Re: How to modify default control property values, or put custom controls in toolbox

    Code:
    Public Class newButton
        Inherits Button
    
        Public Property yourCustomProperty As String
    
    End Class
    Add the class to your project, rebuild your project, then in your designer, you’ll find the newButton control at the top of your toolbox

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,504

    Re: How to modify default control property values, or put custom controls in toolbox

    In my previous example, include this to change the text of all newButton controls added to your project…

    Code:
    Public Sub New()
        MyBase.Text = “Whatever”
    End Sub

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,893

    Re: How to modify default control property values, or put custom controls in toolbox

    Quote Originally Posted by silverblatt View Post
    How can I modify the default values of the native controls
    You can't. The default values you see for properties when you add a control to a form are either part of the code of that class or baked into the WinForms designer itself.
    Quote Originally Posted by silverblatt View Post
    or make my customized controls appear in the toolbox?
    What .paul. has suggested will work on a project-by-project basis. If you want permanent replacements for the standard controls then you need to create a library project, add your custom controls to it, build that project and then add new items to the Toolbox from that library via the Toolbox itself. You can right-click the Toolbox and choose to select items, then navigate to the compiled DLL from your library project and choose custom controls to add permanently to the Toolbox. You would then need to deploy your DLL with any application that uses those custom controls.

  5. #5

    Thread Starter
    Member
    Join Date
    Apr 2010
    Posts
    39

    Re: How to modify default control property values, or put custom controls in toolbox

    Thanks, .paul and jmcilhinney. I added the following code, successfully rebuilt the entire solution and then closed and re-opened Visual Studio, but NewLabel still does not appear in my Toolbox. Is there some other step I need to take?
    Code:
        Public Class NewLabel
            Inherits Label
            Public Sub New()
                MyBase.AutoSize = False
            End Sub
        End Class
    Last edited by silverblatt; Dec 3rd, 2021 at 12:37 PM.

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2010
    Posts
    39

    Re: How to modify default control property values, or put custom controls in toolbox

    Solved it. I had only added the NewLabel class code to an existing code module, instead of creating a separate control item in Solution Explorer. Doing that, and moving the class code to the control item, caused the new control to appear in Toolbox after a rebuild.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,893

    Re: [RESOLVED] How to modify default control property values, or put custom controls

    Pretty much all types should be declared in their own dedicated code file. The one exception I make is that I declare all Enums in a single file named Enumerations.vb.

    You should pretty much never declare one type inside another. If the inner type is only ever used within the outer type then it's justified but pretty much never otherwise. Microsoft used to nest classes that were specifically related, e.g. ListViewSubItem is declared within ListViewItem, but they don't do that any more.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,504

    Re: [RESOLVED] How to modify default control property values, or put custom controls

    @jm. I think silverblatt has now created a usercontrol. That wasn’t what I suggested, but, if it works…

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,893

    Re: [RESOLVED] How to modify default control property values, or put custom controls

    Quote Originally Posted by .paul. View Post
    @jm. I think silverblatt has now created a usercontrol. That wasn’t what I suggested, but, if it works…
    Sounds more like a custom control. My interpretation was that the code from post #5, which is pretty much exactly like your post #2 but for Label rather than Button, was simply moved to its own code file from its original bad location.

  10. #10

    Thread Starter
    Member
    Join Date
    Apr 2010
    Posts
    39

    Re: [RESOLVED] How to modify default control property values, or put custom controls

    I'm a bit confused about the distinction between user controls and custom controls. What I did in the end was add a new User Control item in Solution Explorer, move the code into it from an existing code module, and rebuild the solution.

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,893

    Re: [RESOLVED] How to modify default control property values, or put custom controls

    Quote Originally Posted by silverblatt View Post
    I'm a bit confused about the distinction between user controls and custom controls. What I did in the end was add a new User Control item in Solution Explorer, move the code into it from an existing code module, and rebuild the solution.
    People do use the terms interchangeably sometimes but, in WinForms, a user control is something specific, i.e. a class that inherits System.Windows.Forms.UserControl. A user control is basically a container for multiple child controls. You design it like a form, adding Buttons, Labels, TextBoxes and/or whatever else you want, add code like you would a form and then, when you build your project, it gets added to the Toolbox and you can use it like any other form. That's not what you wanted so you should not have created a user control. You would have had a designer code file that you don't need or want. You should delete that item in the Solution Explorer and start again.

    A custom control is a class that either inherits System.Windows.Forms.Control directly and adds all the functionality or inherits an existing control and adds just the new functionality. The latter is what you're doing. In that case, what you should have done is added a new Class item to your project and named it appropriately, then added the:
    vb.net Code:
    1. Inherits Label
    line and any other code you needed. Again, once you build, the new custom control will be added to the Toolbox.

    You should also not name it NewLabel. If the is no appropriate specific descriptive name, a convention that I've seen elsewhere and that I follow is to add a "Ex" suffix to the existing control name, indicating "Extended". A name that actually describes the class is generally better though, e.g. NonAutoSizingLabel.

  12. #12

    Thread Starter
    Member
    Join Date
    Apr 2010
    Posts
    39

    Re: [RESOLVED] How to modify default control property values, or put custom controls

    Given that what I did (added a new User Control) seems to work fine, what would be the advantage to your approach (adding a new class)?

  13. #13
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,893

    Re: [RESOLVED] How to modify default control property values, or put custom controls

    If you added a user control then you have presumably have a designer code file that you don't need. Complexity for no reason is a possible breaking point. Given that it would take a matter of seconds to copy the code from the user code file, delete the user control, add a new class and paste the code in, there seems no good reason not to do it.

  14. #14

    Thread Starter
    Member
    Join Date
    Apr 2010
    Posts
    39

    Re: [RESOLVED] How to modify default control property values, or put custom controls

    I see your point, so I ripped everything out and re-did it as a class, with the following code:
    Code:
    Public Class ExLabel
        Inherits Label
        Public Sub New()
            MyBase.AutoSize = False
        End Sub
    End Class
    After re-building the solution, ExLabel does indeed appear in the Toolbox, but doesn't work properly. When I click it and then drag in a form designer to indicate its position and shape, the AutoSize property is still set to True despite the code, thus defeating the entire purpose of the class. Any idea what's happening here?

  15. #15
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,893

    Re: [RESOLVED] How to modify default control property values, or put custom controls

    In my testing, the AutoSize property is getting set to False there but it is already False anyway. I handled the AutoSizeChanged event and it was raised after the constructor completed, but there was no indication of where the property was being set from. Presumably it's in a base class somewhere but not sure where.

  16. #16
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,504

    Re: [RESOLVED] How to modify default control property values, or put custom controls

    This will work...

    Code:
    Public Class NonAutoSizeLabel
        Inherits Label
    
        Public Overrides Property AutoSize() As Boolean
            Get
                Return MyBase.AutoSize
            End Get
            Set(ByVal value As Boolean)
                MyBase.AutoSize = False
            End Set
        End Property
    End Class

  17. #17
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,893

    Re: [RESOLVED] How to modify default control property values, or put custom controls

    Quote Originally Posted by .paul. View Post
    This will work...

    Code:
    Public Class NonAutoSizeLabel
        Inherits Label
    
        Public Overrides Property AutoSize() As Boolean
            Get
                Return MyBase.AutoSize
            End Get
            Set(ByVal value As Boolean)
                MyBase.AutoSize = False
            End Set
        End Property
    End Class
    That's a bit of a sledgehammer, as it will prevent it ever being set to True, but I guess that's probably not an issue in this particular case. If you ever do want it to auto-size, just use a regular Label instead.

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    24,504

    Re: [RESOLVED] How to modify default control property values, or put custom controls

    To prevent AutoSize, and remove it from your designer propertygrid...

    Code:
    Imports System.ComponentModel
    
    Public Class NonAutoSizeLabel
        Inherits Label
    
        <Browsable(False)> _
        Public Overrides Property AutoSize() As Boolean
            Get
                Return MyBase.AutoSize
            End Get
            Set(ByVal value As Boolean)
                MyBase.AutoSize = False
            End Set
        End Property
    End Class

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width