From: Family Tree Mike on
On 4/25/2010 5:15 PM, Mr. X. wrote:
> Well, thanks.
> Your design pattern example of using Activator, is exactly what I need too.
>
> I have tried this for some other simpler controls, such as button.
>
> Some new problems :
> --------------------
> 1. I want to make a class that is inherited from a form,
> and use it, add it to project, etc. at the same way I add new windows'
> form.
> How can I do that?

Here is a decent writeup on the topic. Look about half way through the
artical and see a section called "Exporting our class to a new template".


> 2. Also, I need to exposed a property, but I should need a type.
> When I do :
> public property newForm() as Type
> ...
>
> on the property I cannot choose a type,
> but also I want that I can choose only types that are inherits the
> MyNewForm.
>
> How can I do that ?
>
>
> Thanks :)

Interesting. I've not tried that before, but I see where the property
is grayed out in the properties list. Aside from that, your question
about limiting the types is going to be a problem for a property. I'm
not sure how you could do that.

It sounds more and more like you could potentially use "generics". The
following class shows how to create a form using generics where the
parameter type is constrained to classes derived from type MyNewForm,
and that can be called to create a new instance.

Public Class MySpecialForm(Of T As {MyNewForm, New})
Inherits System.Windows.Forms.Form

Public Property NewForm As T
End Class


So now when you create an instance of this, you would do the following:

Dim x as new MySpecialForm(of Class1)

This assumes Class1 inherits from MyNewForm. You now can only set the
property x.NewForm to an object of type Class1 (or something derived
from class1). Within MySpecialForm, you can create an instance of the
sent in type (class1 in the example), as follows:

Sub foo()
Dim x As New T
x.ShowDialog()
End Sub

--
Mike
From: Family Tree Mike on
On 4/25/2010 9:20 PM, Family Tree Mike wrote:
> On 4/25/2010 5:15 PM, Mr. X. wrote:
>> Well, thanks.
>> Your design pattern example of using Activator, is exactly what I need
>> too.
>>
>> I have tried this for some other simpler controls, such as button.
>>
>> Some new problems :
>> --------------------
>> 1. I want to make a class that is inherited from a form,
>> and use it, add it to project, etc. at the same way I add new windows'
>> form.
>> How can I do that?
>
> Here is a decent writeup on the topic. Look about half way through the
> artical and see a section called "Exporting our class to a new template".
>

Sorry, forgot the link:
http://www.vbdotnetheaven.com/UploadFile/progalex/AdsCreateExportClasses01242006114527AM/AdsCreateExportClasses.aspx

--
Mike
From: Branco Medeiros on
Mr. X. wrote:
<snip>
> 1. I want to make a class that is inherited from a form,
> and use it, add it to project, etc. at the same way I add new windows' form.
> How can I do that?

In VB Express 2008 (didn't try in 2010, but it's probably the same)
you would have something like this:

a) Create the base form and customize it to your needs, placing the
controls that you want to appear in the descendant forms (the
descendant forms will also inherit the properties you set in the base
form as well).

b) Add a new class and inherit from your base form.

c) Build the application (from the build menu)

d) double click your new class name in the Solution Explorer to
customize it. The class will open in design mode, and you will see
that the controls that existed in the base for were automatically
added to the derived one (they even have some overlay marks indicating
that they are inherited).

e) rinse and repeat for any number of customized forms you want.
Notice that if you change anything in the base form(s), the changes
will only appear in the derived ones after you rebuild the
application.

> 2. Also, I need to exposed a property, but I should need a type.
> When I do :
> public property newForm() as Type
> ...
>
> on the property I cannot choose a type,
> but also I want that I can choose only types that are inherits the
> MyNewForm.
> How can I do that ?
<snip>

I'm not really sure of what you want here. Do you want a property that
will return a *new* object? Maybe this isn't the way properties are
supposed to work. A function would be more apropriate, I guess.

Best regards,

Branco.
From: Mr. X. on
Thanks - Work !!!
One little problem :
How can I create an objects that new has some parameters.
I have tried :
x = Activator.CreateInstance(MyControlType, controlArgs)
but this doesn't work.
Also
x = Activator.CreateInstance(MyControlType)(controlArgs)
doesn't work either.

MyControlType can be a form
controlArgs :
if I have constructor :
new(byRef t as dataTable).
....
dim x as MyControlType
....

Thanks :)
From: Mr. X. on
Solved.
What I did :
A constructor on the inherited class.

I should check out using template to insert the code behind.

Thanks :)

"Mr. X." <nospam(a)nospam_please.com> wrote in message
news:ejOXyU#5KHA.3656(a)TK2MSFTNGP06.phx.gbl...
> Thanks - Work !!!
> One little problem :
> How can I create an objects that new has some parameters.
> I have tried :
> x = Activator.CreateInstance(MyControlType, controlArgs)
> but this doesn't work.
> Also x = Activator.CreateInstance(MyControlType)(controlArgs)
> doesn't work either.
>
> MyControlType can be a form
> controlArgs :
> if I have constructor :
> new(byRef t as dataTable).
> ...
> dim x as MyControlType
> ...
>
> Thanks :)