Initializing .NET Control

  • Thread starter Thread starter Scott Kilbourn
  • Start date Start date
S

Scott Kilbourn

Hi,

In VB6, when you wanted to set a property on an ActiveX control to a default
value when the control is first created, you use the InitProperties event.
What is the equivalent for this property in .NET? I need to initialize some
properties to certain values only when the control is first added to a form.

Thanks
 
Use the DefaultValue Attribute

Example:

<DefaultValue(False)> _
Public Property MyProperty() As Boolean
...
End Property
 
Scott Kilbourn said:
In VB6, when you wanted to set a property on an ActiveX control to a
default value when the control is first created, you use the
InitProperties event. What is the equivalent for this property in
.NET? I need to initialize some properties to certain values only
when the control is first added to a form.

What does "first" added mean? How could you add the same control a second
time?
 
I wasn't saying you could. You read that a bit to literally.

I meant that when I add a control to a form, I need the control to have
certain default values. So far, I've been unable to come up with a way to
make that happen.
 
Scott Kilbourn said:
I wasn't saying you could. You read that a bit to literally.

Maybe. :-)

You wrote "...only when the control is first added to a form.". It
implicitly meant to me: "...not, when it is added the second time". Now I
understand, and I saw you've gotten a solution already.
 
It's amazing how the exact same language can mean completely different
things to two different people. :)
 
Scott Kilbourn said:
It's amazing how the exact same language can mean completely
different things to two different people. :)

If you had only written "...when it is added to a form" it would have been
clear. ;-)
 
Hi Armin and Scott,
You wrote "...only when the control is first added to a form.". It
implicitly meant to me: "...not, when it is added the second time". Now I
understand, and I saw you've gotten a solution already.

I had the same conclusion as Armin.
And that did make the question very difficult.

Cor
 
H
I am trying this and when I drag the control onto the form it is not setting the property value to the default value. Have I misunderstood what this DefaultValue is or am I omitting something silly

For example

<DefaultValue(100), Description("This is my property")> Property MyProperty() As Intege
Ge
Return mnMyPropert
End Ge
Set(ByVal Value As Integer
mnMyProperty = Valu
End Se
End Propert

When I place a new version of the control on a form MyProperty is set to 0 and not 100
Any help appreciated and apologoes if I am being totally daft!
Thank
Siobhan
 
* "=?Utf-8?B?U2lvYmhhbg==?= said:
I am trying this and when I drag the control onto the form it is not
setting the property value to the default value. Have I misunderstood
what this DefaultValue is or am I omitting something silly?

Default value will only be used, if you select the property in the
properties window and choose "Reset" from its context menu.
 
Hi
Thanks for that, but is there anyway to get the property value set when you place the control on the form
Cheer
Siobha

----- Herfried K. Wagner [MVP] wrote: ----

* "=?Utf-8?B?U2lvYmhhbg==?= said:
I am trying this and when I drag the control onto the form it is no
setting the property value to the default value. Have I misunderstoo
what this DefaultValue is or am I omitting something silly

Default value will only be used, if you select the property in th
properties window and choose "Reset" from its context menu
 
It would be pretty diffucult...

controls are instantiated by using the default constructor and the dte does
some work with it...

Now, you could do this in your InitializeComponent() method and say

if (me.DesignMode) then
'set your property value
end if

However, how do you know that this is a newly added component and not just a
form being loaded in the DTE?

Exactly...

So, thats why it only works on reset.

Long winded and boring, but sometiems you need to to get the point across.
=)

There is one other "trick" that could work. If its a Read/Write property
you *could* do your validation in the property setter itself by calling a
self reference

i.e

MyPropertyValue = MyPropertyValue

Which calls the get and then the set... So if it was zero you could set it
to the default value... however, do you know for a fact that no one ever
wants it to be zero or you yourself don't.

HTH,
CJ
Siobhan said:
Hi
Thanks for that, but is there anyway to get the property value set when
you place the control on the form?
 
Not dumb at all.

AFAIK, there isn't a way to do it unless you create your own PropertyBag
like ActiveX controls did. Or you could use your resource file to store
data instead. But nothing to do it easily.

or...

now that I think of it.. you could use your property again, but this time,
use Object to make it happen...

Lets say you have a property called MyDecimalProperty, obviously of type
decimal.
You could use an object behind the scenes to deal with reference.

i.e

private _myDecimalObject as Object

public property MyDecimalProperty as Decimal
Get
if (_myDecimalObject is Nothing) then
'set default value
end if
return ctype(_myDecimalObject, Decimal)
end get
set
...
end set
end property

I don't knowi f it will work, but you could try...



Siobhan said:
I think I understand, but again forgive me if I am being dumb - does all
this mean that there is no equivalent way to perform the actions of vb6
InitProperties?
 
Back
Top