Default Properties for Controls

  • Thread starter Thread starter XNoDE
  • Start date Start date
X

XNoDE

Hi all,

VS.Net 2002.

Is there a way to set default properties for controls in the Toolbox?

There a some properties that I implement as standard in my apps and would
like to get around setting these same basic things each time a use a
control.

TIA,

XNoDE
 
Default values by and large are not allowed in VB.NET unless it is a
parameterized property, like with a collection:

collection.item(x) could be written as: collection(x)

In classic VB you could have default properties (such as .text with a
textbox) because of the difference between objects and data types

x = txtUser was clearly accessing the default .text property of the textbox
and

Set x = txtUser was clearly making x an object reference to the textbox
itself

In .NET, since everything is an object, the Set statement is irrelevant and
no longer supported. However, this created a problem:

Does x = txtUser mean that you want x to take on the .text property value
of the txtUser textbox or does it mean that you want x to become an object
reference to the textbox itself?

To clear up this ambiguity, MS did away with all default properties unless
that are properties that take parameters.

x = collection(x) clearly implies that you are talking about the .item
property of collection because there is a parameter present (x).

x = collection clearly implies that you want x to be an object reference to
the collection object.

Hope this helps.

Scott
 
So, I'm reading your post again and realizing that, you don't want to set
default PROPERTIES on the toolbox controls (as you've written), you perhaps
want to set a default VALUE for some various properties of the toolbox
controls. (Sorry for the mis-read)

The controls on the toolbox represent base classes in the .NET Framework
class libraries, so the short answer is no. However, you could make a new
control that inherits from the standard one, set whatever property values
you want and use that control instead.

Public Class MyTextBox
Inherits System.Windows.Forms.Textbox

Public Sub New()
me.Text = "Some default value."
End Sub
End Class

HTH!

Scott
 
Thanks Scott,

Sorry for the mistype, I guess I wasn't really sure how to ask for what I
wanted. Your second post is correct. If for example I would like all of my
buttons to have the default values as:

FlatStyle = Flat
Size = 96,20

etc..

Is there a way I can have these values preset to my liking so that I only
have to build the form and code? I know it seems a trivial thing, but it's
a preference of mine.

If so, how. If I create the new class that you mention, how do I save it as
a control so that I can add it to my forms at leisure? I am familiar with
creating the class files but I've never coded extensions to a control.

Thanks again,

Xnode
 
I haven't done this myself, but look into building a custom server control.
These can be added to your toolbox so that you can place them on forms just
like any other control.
 
Back
Top