-
Dec 2nd, 2021, 07:43 PM
#1
Thread Starter
Lively Member
[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?
-
Dec 2nd, 2021, 07:50 PM
#2
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 2nd, 2021, 07:55 PM
#3
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 2nd, 2021, 08:23 PM
#4
Re: How to modify default control property values, or put custom controls in toolbox
Originally Posted by silverblatt
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.
Originally Posted by silverblatt
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.
-
Dec 3rd, 2021, 12:33 PM
#5
Thread Starter
Lively Member
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.
-
Dec 3rd, 2021, 04:45 PM
#6
Thread Starter
Lively Member
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.
-
Dec 3rd, 2021, 09:16 PM
#7
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.
-
Dec 3rd, 2021, 09:20 PM
#8
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…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 3rd, 2021, 09:57 PM
#9
Re: [RESOLVED] How to modify default control property values, or put custom controls
Originally Posted by .paul.
@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.
-
Dec 3rd, 2021, 10:33 PM
#10
Thread Starter
Lively Member
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.
-
Dec 4th, 2021, 02:00 AM
#11
Re: [RESOLVED] How to modify default control property values, or put custom controls
Originally Posted by silverblatt
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:
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.
-
Dec 6th, 2021, 12:45 PM
#12
Thread Starter
Lively Member
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)?
-
Dec 6th, 2021, 07:11 PM
#13
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.
-
Dec 6th, 2021, 11:25 PM
#14
Thread Starter
Lively Member
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?
-
Dec 7th, 2021, 12:41 AM
#15
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.
-
Dec 7th, 2021, 02:55 AM
#16
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 7th, 2021, 04:16 AM
#17
Re: [RESOLVED] How to modify default control property values, or put custom controls
Originally Posted by .paul.
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.
-
Dec 7th, 2021, 03:18 PM
#18
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|