new property for control type Form

M

MP

Hello

I want to have a public property (and Brows able) in one control, I use this
code:

[Browsable(true)]
public System.Windows.Forms.Form recordForm

get { return _recordForm;}
set {_recordForm = value;}
}

But when I tried to use it at the properties window I get a combobox and
only give me options for (none), and the same Form the control is contained.

I need to put the name of a different Form, is the a way for me to get this
done?
Is this the better approach?

TIA

MP
 
J

John Wood

Where do you expect it to get the list of forms from? All the instances in
your application? How would it know about that at design time anyway?
 
M

MP

Is another form in the same project and some time from a reference project.



I don't wave any way for now, or any preferences.



The problem I have is that at some point I want to know if this property has
a value, and if it's not empty or null, show the corresponding form.



TIA



MP


John Wood said:
Where do you expect it to get the list of forms from? All the instances in
your application? How would it know about that at design time anyway?

MP said:
Hello

I want to have a public property (and Brows able) in one control, I use this
code:

[Browsable(true)]
public System.Windows.Forms.Form recordForm

get { return _recordForm;}
set {_recordForm = value;}
}

But when I tried to use it at the properties window I get a combobox and
only give me options for (none), and the same Form the control is contained.

I need to put the name of a different Form, is the a way for me to get this
done?
Is this the better approach?

TIA

MP
 
J

Jay B. Harlow [MVP - Outlook]

MP,
The default implementation for the drop down created is to list all
'components' on the current designer that matches the type given. Hence on
your current form, you only have one form type. If you drag & drop other
forms to this form, they will show up in the drop down. As they are now
instance variables on the current form.

Alternatively you can create a custom TypeConverter for this property, that
will list all the forms in this project. Do you expect the property to
create an instance of the form?

This TypeConverter can override the GetStandardValuesSupported,
GetStandardValuesExclusive, & GetStandardValues methods to provide a list of
forms to show in the drop down. You will also need to override the
CanConvertTo, ConvertTo, CanConvertFrom, and ConvertFrom methods.

The trick is going to be do you want an instance of another form to be
passed?
Or do you want the property to create an instance of another form, in which
case I would think you want the property to be a type or type name (string)
and use Activator.CreateInstance.

See System.ComponentModel.TypeConverter class and its GetStandardValues
method.

Hope this helps
Jay
 
M

MP

Or do you want the property to create an instance of another form, in
which
case I would think you want the property to be a type or type name (string)
and use Activator.CreateInstance.

Yes this is my case, I use this code, is working, is correct, or there is a
better way?

Type myType = Type.GetType(newRecFormName);
ParentF0 f = (ParentF0) Activator.CreateInstance(myType);
f....
If you drag & drop other
forms to this form, they will show up in the drop down. As they are now
instance variables on the current form.

I tried to drag and drop another form over my first form but the designer
doesn't allow me to do that. (The icon change to the NOT symbol) How I do
that?

Thanks, very useful...
MP

Jay B. Harlow said:
MP,
The default implementation for the drop down created is to list all
'components' on the current designer that matches the type given. Hence on
your current form, you only have one form type. If you drag & drop other
forms to this form, they will show up in the drop down. As they are now
instance variables on the current form.

Alternatively you can create a custom TypeConverter for this property, that
will list all the forms in this project. Do you expect the property to
create an instance of the form?

This TypeConverter can override the GetStandardValuesSupported,
GetStandardValuesExclusive, & GetStandardValues methods to provide a list of
forms to show in the drop down. You will also need to override the
CanConvertTo, ConvertTo, CanConvertFrom, and ConvertFrom methods.

The trick is going to be do you want an instance of another form to be
passed?
Or do you want the property to create an instance of another form, in which
case I would think you want the property to be a type or type name (string)
and use Activator.CreateInstance.

See System.ComponentModel.TypeConverter class and its GetStandardValues
method.

Hope this helps
Jay

MP said:
Hello

I want to have a public property (and Brows able) in one control, I use this
code:

[Browsable(true)]
public System.Windows.Forms.Form recordForm

get { return _recordForm;}
set {_recordForm = value;}
}

But when I tried to use it at the properties window I get a combobox and
only give me options for (none), and the same Form the control is contained.

I need to put the name of a different Form, is the a way for me to get this
done?
Is this the better approach?

TIA

MP
 
Y

Ying-Shen Yu

Hi MP,
Glad to hear from you!
First I want to make clear that When do you want to create instance of
that form, run-time or design-time?
If the answer is run-time, maybe here is an solution, you may create it
in you control using the the
Appomain.CurrentDomain.CreateInstanceAndUnWrap. like this:
string AssemblyName = "TestBench";
string TypeName = AssemblyName + "." + "Form1";
Form form =
AppDomain.CurrentDomain.CreateInstanceAndUnwrap(AssemblyName,TypeName) as
Form;
if(form != null)
form.ShowDialog();
I've sent you a mail with a simple demo using that solution. But it's not
very convenient, in order to identify a the form type, you must provide
both the assembly name and the full type name.

Although the assembly name is often part of the contained name but
there are exceptions, such as mscorlib. In Addition, you may not able to
get assembly name from the full type name, for your control didn't know its
type( call Type.GetType("Form1") in control returns null because it didn't
reference the coressponding assembly, sounds like a circle problem?).
You may ask users inputing the assembly name and full type name manually.

If you want to create that form in design-time or validate the property
by try to create an instance, well, as far as I know there are no
documented way to do this. So I'm afraid the validation in design-time is
not an easy task, for now, the work around maybe throw exception in
run-time and keep the state of your control consistent in exception.

In run-time, you can use the property AppDomain.CurrentDomain to get
global meta data of the whole project. But the property doesn't take effect
in design-time, beacause your control runs in the AppDomain of vS.NET in
design-time. I'm doing some research on this issue and until now, I haven't
found a way to access the assembly information of the project in
design-time, please let me know if you have a good idea.

Be free to let me know if you have any questions on this issue, thanks!


Kind regards,

Ying-Shen Yu [MSFT]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.

From: "MP" <[email protected]>
Subject: Re: new property for control type Form
Date: Mon, 11 Aug 2003 10:56:59 -0400
Newsgroups: microsoft.public.dotnet.framework.windowsforms

Hello again and thanks for your help,

I understand your answer at run time, but how can I put the value at design
time for:
MyFormProperty

I think I need some type of "form set" for group at design time the form
contained in my control and another one.

If that way is complicated, I can change my property for string, and create
an instance of the form corresponding to that string

String optionalForm = "FormOpt";

Now my new question is:
How to create a form instance for the string (I searching for this now)

This isn't the better way, but for now it may work for me.

If you have another ideas let me know please.

Thanks for your help

MP

Ying-Shen Yu said:
Hello MP,
Do you mean you that when you pull-down the property combo-box, you get
the form instance created in your project(e.g. form2, form3....),and then
you select one, or you enter the name of the form variable and then
validate it. Unfortunately, neither methods
will work in current .NET framework, because there is no documented
interfaces or methods to enumerate the added forms, the only instance of
form you can get is the container of your control. To work around this
limitation, you may set and check your property in run-time. If you want to
know if this property has a valid reference , you can check if it is equals
to null,like the following code:
if (UserControl.MyFormProperty != null)
{
.....
UserControl.MyFormProperty.Show();
.....
}

Please Let me know if you still have questions on this issue, Thanks!



Kind regards,

Ying-Shen Yu [MSFT]
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2001 Microsoft Corporation. All rights
reserved.



From: "MP" <[email protected]>
Subject: Re: new property for control type Form
Date: Sun, 10 Aug 2003 16:21:44 -0400
Newsgroups:
microsoft.public.dotnet.framework.windowsforms,microsoft.public.dotnet.framework.windowsforms.controls,microsoft.public.dotnet.framework.windowsforms.de
signtime,microsoft.public.dotnet.languages.csharp

Is another form in the same project and some time from a reference project.



I don't wave any way for now, or any preferences.



The problem I have is that at some point I want to know if this property has
a value, and if it's not empty or null, show the corresponding form.



TIA



MP


John Wood said:
Where do you expect it to get the list of forms from? All the instances in
your application? How would it know about that at design time anyway?

MP said:
Hello

I want to have a public property (and Brows able) in one control, I
use
this
code:

[Browsable(true)]
public System.Windows.Forms.Form recordForm

get { return _recordForm;}
set {_recordForm = value;}
}

But when I tried to use it at the properties window I get a combobox and
only give me options for (none), and the same Form the control is contained.

I need to put the name of a different Form, is the a way for me to get this
done?
Is this the better approach?

TIA

MP
 
J

Jay B. Harlow [MVP - Outlook]

MP,
As Ying-Shen Yu, demonstrated there are about 5 ways to create objects from
a string at run-time.

To drag & drop a form onto another form the form you are dragging would need
to be on the toolbox. Unfortunately I do not see you can actually put a form
on the toolbox.

I should have stated "If you COULD drag & drop other forms to this form,
they will show up in the drop down."

Hope this helps
Jay


MP said:
Or do you want the property to create an instance of another form, in which
case I would think you want the property to be a type or type name (string)
and use Activator.CreateInstance.

Yes this is my case, I use this code, is working, is correct, or there is a
better way?

Type myType = Type.GetType(newRecFormName);
ParentF0 f = (ParentF0) Activator.CreateInstance(myType);
f....
If you drag & drop other
forms to this form, they will show up in the drop down. As they are now
instance variables on the current form.

I tried to drag and drop another form over my first form but the designer
doesn't allow me to do that. (The icon change to the NOT symbol) How I do
that?

Thanks, very useful...
MP

MP,
The default implementation for the drop down created is to list all
'components' on the current designer that matches the type given. Hence on
your current form, you only have one form type. If you drag & drop other
forms to this form, they will show up in the drop down. As they are now
instance variables on the current form.

Alternatively you can create a custom TypeConverter for this property, that
will list all the forms in this project. Do you expect the property to
create an instance of the form?

This TypeConverter can override the GetStandardValuesSupported,
GetStandardValuesExclusive, & GetStandardValues methods to provide a
list
of
forms to show in the drop down. You will also need to override the
CanConvertTo, ConvertTo, CanConvertFrom, and ConvertFrom methods.

The trick is going to be do you want an instance of another form to be
passed?
Or do you want the property to create an instance of another form, in which
case I would think you want the property to be a type or type name (string)
and use Activator.CreateInstance.

See System.ComponentModel.TypeConverter class and its GetStandardValues
method.

Hope this helps
Jay

MP said:
Hello

I want to have a public property (and Brows able) in one control, I
use
this
code:

[Browsable(true)]
public System.Windows.Forms.Form recordForm

get { return _recordForm;}
set {_recordForm = value;}
}

But when I tried to use it at the properties window I get a combobox and
only give me options for (none), and the same Form the control is contained.

I need to put the name of a different Form, is the a way for me to get this
done?
Is this the better approach?

TIA

MP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top