By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,140 Members | 1,880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,140 IT Pros & Developers. It's quick & easy.

How to get a usercontrol to automatically add code to the initialization code in a form...

P: n/a
Hope somebody can help!

I want to automatically be able to add code to the initialize routine on a Windows form when I add a custom control that I've written to a form.

Specifically, I'm trying to data bind to a normal class. So I've extended the standard text box to include a field for object name and property name. I want to be able to add a line such as :

controlname.DataBindings.Add("Text", objectName, "myPropertyName")
I've tried using google and looking at the documentation but can't seem to find this... I don't know if there is an alternative way of doing it in the startup code of the form (I seem to have a thought that reflection could be used to get the instance of the object for the data binding code above but I can't work out how!!).

I would be grateful for any pointers.

Many thanks in advance

Simon
Nov 20 '05 #1
Share this Question
Share on Google+
21 Replies


P: n/a
I assume this is a webform you are talking about. If so, each userconrol has its owen Page_load event. In this you should be able to bind and object in the parent container, dont have time to look up the properties now but in psuedo I imagine if would be something like.
Me.Parent.Control.DataBindings.Add( Text, me, Property)

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:OA**************@TK2MSFTNGP09.phx.gbl...
Hope somebody can help!

I want to automatically be able to add code to the initialize routine on a Windows form when I add a custom control that I've written to a form.

Specifically, I'm trying to data bind to a normal class. So I've extended the standard text box to include a field for object name and property name. I want to be able to add a line such as :

controlname.DataBindings.Add("Text", objectName, "myPropertyName")
I've tried using google and looking at the documentation but can't seem to find this... I don't know if there is an alternative way of doing it in the startup code of the form (I seem to have a thought that reflection could be used to get the instance of the object for the data binding code above but I can't work out how!!).

I would be grateful for any pointers.

Many thanks in advance

Simon
Nov 20 '05 #2

P: n/a
Terry,

No it's a Windows Form..

It refers to my question last week about data binding to a simple object. Which works fine... except that you need to manually add some code to databind the text boxes to the object in your main form...

I'm trying to get is so that somehow this step is automated.

I've created an inherited text box that allows me to enter the name of the object and the property to bind to. This is the easy bit.. I'm still working on some way of getting the actual binding to be "automatic" ie not require the binding code to be added manually. I'd therefore like the binding code to be added automatically in the control intialization code where it sets all the control properties.. or alternatively, find some way of doing the binding from the control itself, once the object and property names are set...

Does this make sense?

Regards
Simon
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:eF**************@TK2MSFTNGP11.phx.gbl...
I assume this is a webform you are talking about. If so, each userconrol has its owen Page_load event. In this you should be able to bind and object in the parent container, dont have time to look up the properties now but in psuedo I imagine if would be something like.
Me.Parent.Control.DataBindings.Add( Text, me, Property)

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:OA**************@TK2MSFTNGP09.phx.gbl...
Hope somebody can help!

I want to automatically be able to add code to the initialize routine on a Windows form when I add a custom control that I've written to a form.

Specifically, I'm trying to data bind to a normal class. So I've extended the standard text box to include a field for object name and property name. I want to be able to add a line such as :

controlname.DataBindings.Add("Text", objectName, "myPropertyName")
I've tried using google and looking at the documentation but can't seem to find this... I don't know if there is an alternative way of doing it in the startup code of the form (I seem to have a thought that reflection could be used to get the instance of the object for the data binding code above but I can't work out how!!).

I would be grateful for any pointers.

Many thanks in advance

Simon
Nov 20 '05 #3

P: n/a
* "One Handed Man \( OHM - Terry Burns \)" <news.microsoft.com> scripsit:
I assume this is a webform you are talking about.


The OP mentioned "Windows Form"...

:-)

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4

P: n/a
Hi Simon,

I might be entirely "off track" but if I understand your issue well, you could use a "IComponentChangeService".
I have created a Component which "Watches" all Components/Controls Added, Removed, Property Edited, ... on a Windows Form.
You could also use a "IComponentChangeService" directly in your Custom Control.

Having this done you could catch the "OnComponentAdded"-Event and put some code in it to set your bindings.

As I said: I'm not sure if I fully understood your problem, but if I did I think this might be a solution.

Regards,

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Terry,

No it's a Windows Form..

It refers to my question last week about data binding to a simple object. Which works fine... except that you need to manually add some code to databind the text boxes to the object in your main form...

I'm trying to get is so that somehow this step is automated.

I've created an inherited text box that allows me to enter the name of the object and the property to bind to. This is the easy bit.. I'm still working on some way of getting the actual binding to be "automatic" ie not require the binding code to be added manually. I'd therefore like the binding code to be added automatically in the control intialization code where it sets all the control properties.. or alternatively, find some way of doing the binding from the control itself, once the object and property names are set...

Does this make sense?

Regards
Simon
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:eF**************@TK2MSFTNGP11.phx.gbl...
I assume this is a webform you are talking about. If so, each userconrol has its owen Page_load event. In this you should be able to bind and object in the parent container, dont have time to look up the properties now but in psuedo I imagine if would be something like.
Me.Parent.Control.DataBindings.Add( Text, me, Property)

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:OA**************@TK2MSFTNGP09.phx.gbl...
Hope somebody can help!

I want to automatically be able to add code to the initialize routine on a Windows form when I add a custom control that I've written to a form.

Specifically, I'm trying to data bind to a normal class. So I've extended the standard text box to include a field for object name and property name. I want to be able to add a line such as :

controlname.DataBindings.Add("Text", objectName, "myPropertyName")
I've tried using google and looking at the documentation but can't seem to find this... I don't know if there is an alternative way of doing it in the startup code of the form (I seem to have a thought that reflection could be used to get the instance of the object for the data binding code above but I can't work out how!!).

I would be grateful for any pointers.

Many thanks in advance

Simon
Nov 20 '05 #5

P: n/a
Michael,

Thanks for the reply... I'm not 100% sure I'm following how I would use your advice!

I've done some more work on the subject, and ideally it looks like I need some way of dynamically "getting" the object that I want to bind to from within my control.

eg something like...

ObjectName="name_of_object_on_form"
PropertyName="name_of_property_to bind_to"
dim obj as object
obj=XXXXXX(objectname) <---- this is the line I need which retrieves the object from the parent form.
me.databinding.add("text",obj,PropertyName)

I think if I could work this out then I would solve the problem?

Regards
Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
Hi Simon,

I might be entirely "off track" but if I understand your issue well, you could use a "IComponentChangeService".
I have created a Component which "Watches" all Components/Controls Added, Removed, Property Edited, ... on a Windows Form.
You could also use a "IComponentChangeService" directly in your Custom Control.

Having this done you could catch the "OnComponentAdded"-Event and put some code in it to set your bindings.

As I said: I'm not sure if I fully understood your problem, but if I did I think this might be a solution.

Regards,

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Terry,

No it's a Windows Form..

It refers to my question last week about data binding to a simple object. Which works fine... except that you need to manually add some code to databind the text boxes to the object in your main form...

I'm trying to get is so that somehow this step is automated.

I've created an inherited text box that allows me to enter the name of the object and the property to bind to. This is the easy bit.. I'm still working on some way of getting the actual binding to be "automatic" ie not require the binding code to be added manually. I'd therefore like the binding code to be added automatically in the control intialization code where it sets all the control properties.. or alternatively, find some way of doing the binding from the control itself, once the object and property names are set...

Does this make sense?

Regards
Simon
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:eF**************@TK2MSFTNGP11.phx.gbl...
I assume this is a webform you are talking about. If so, each userconrol has its owen Page_load event. In this you should be able to bind and object in the parent container, dont have time to look up the properties now but in psuedo I imagine if would be something like.
Me.Parent.Control.DataBindings.Add( Text, me, Property)

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:OA**************@TK2MSFTNGP09.phx.gbl...
Hope somebody can help!

I want to automatically be able to add code to the initialize routine on a Windows form when I add a custom control that I've written to a form.

Specifically, I'm trying to data bind to a normal class. So I've extended the standard text box to include a field for object name and property name. I want to be able to add a line such as :

controlname.DataBindings.Add("Text", objectName, "myPropertyName")
I've tried using google and looking at the documentation but can't seem to find this... I don't know if there is an alternative way of doing it in the startup code of the form (I seem to have a thought that reflection could be used to get the instance of the object for the data binding code above but I can't work out how!!).

I would be grateful for any pointers.

Many thanks in advance

Simon
Nov 20 '05 #6

P: n/a
to review how I got to this question.. maybe I've missed something obvious.

I want to create an inherited text box that can be "bound" to a object (this
object not being a dataset or anything else that can be "automatically bound
to").

I found that I can do this in code using a normal text box using:

dim myobj as myclass
txtbox.databindings.add("text",myobj,"propertyname ").

So, I thought that if I created an inherited textbox with extra properties
call "ObjectName" and "PropertyName" I would be able to key in the object
and propertyname into the textbox within the designer (which works fine)..
However, I'm now struggling to find a way to get this inherited text box to
"bind" using the dynamic properties set in the designer.. My aim is to do
all this so that the text box ends up being "bound" without adding any code
at runtime....

Does this clarify??

Thanks in advance

Simon

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2k*************@uni-berlin.de...
* "One Handed Man \( OHM - Terry Burns \)" <news.microsoft.com> scripsit:
I assume this is a webform you are talking about.


The OP mentioned "Windows Form"...

:-)

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #7

P: n/a
Hi Simon.

Just a "wild guess":

If your Object is on the Form or in the ComponentsTray, then you can iterate through that collection until you've found your Object during Design-Time. This can be done by capturing the OnComponentAdded-Event of the IComponentChangeService (This service would be implemented in your custom control). You could also set a trigger in the OnComponentChanged-Event when the Property of "PropertyName" has changed.

This all of course can only work if your Object to which you want to bind is on the Form or ComponentTray (during design-time).
If this approach could help you, I would be glad to "get you on the road" with the 'IComponentChangeService' .

Regards,

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:uC**************@TK2MSFTNGP09.phx.gbl...
Michael,

Thanks for the reply... I'm not 100% sure I'm following how I would use your advice!

I've done some more work on the subject, and ideally it looks like I need some way of dynamically "getting" the object that I want to bind to from within my control.

eg something like...

ObjectName="name_of_object_on_form"
PropertyName="name_of_property_to bind_to"
dim obj as object
obj=XXXXXX(objectname) <---- this is the line I need which retrieves the object from the parent form.
me.databinding.add("text",obj,PropertyName)

I think if I could work this out then I would solve the problem?

Regards
Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
Hi Simon,

I might be entirely "off track" but if I understand your issue well, you could use a "IComponentChangeService".
I have created a Component which "Watches" all Components/Controls Added, Removed, Property Edited, ... on a Windows Form.
You could also use a "IComponentChangeService" directly in your Custom Control.

Having this done you could catch the "OnComponentAdded"-Event and put some code in it to set your bindings.

As I said: I'm not sure if I fully understood your problem, but if I did I think this might be a solution.

Regards,

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Terry,

No it's a Windows Form..

It refers to my question last week about data binding to a simple object. Which works fine... except that you need to manually add some code to databind the text boxes to the object in your main form...

I'm trying to get is so that somehow this step is automated.

I've created an inherited text box that allows me to enter the name of the object and the property to bind to. This is the easy bit.. I'm still working on some way of getting the actual binding to be "automatic" ie not require the binding code to be added manually. I'd therefore like the binding code to be added automatically in the control intialization code where it sets all the control properties.. or alternatively, find some way of doing the binding from the control itself, once the object and property names are set...

Does this make sense?

Regards
Simon
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:eF**************@TK2MSFTNGP11.phx.gbl...
I assume this is a webform you are talking about. If so, each userconrol has its owen Page_load event. In this you should be able to bind and object in the parent container, dont have time to look up the properties now but in psuedo I imagine if would be something like.
Me.Parent.Control.DataBindings.Add( Text, me, Property)

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:OA**************@TK2MSFTNGP09.phx.gbl...
Hope somebody can help!

I want to automatically be able to add code to the initialize routine on a Windows form when I add a custom control that I've written to a form.

Specifically, I'm trying to data bind to a normal class. So I've extended the standard text box to include a field for object name and property name. I want to be able to add a line such as :

controlname.DataBindings.Add("Text", objectName, "myPropertyName")
I've tried using google and looking at the documentation but can't seem to find this... I don't know if there is an alternative way of doing it in the startup code of the form (I seem to have a thought that reflection could be used to get the instance of the object for the data binding code above but I can't work out how!!).

I would be grateful for any pointers.

Many thanks in advance

Simon
Nov 20 '05 #8

P: n/a
So he did, sorry I muffed it up !

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2k*************@uni-berlin.de...
* "One Handed Man \( OHM - Terry Burns \)" <news.microsoft.com> scripsit:
I assume this is a webform you are talking about.


The OP mentioned "Windows Form"...

:-)

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #9

P: n/a
Michael,

If you have a code sample that you could post, I'd be eternally grateful ....

Regards
Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:eI**************@tk2msftngp13.phx.gbl...
Hi Simon.

Just a "wild guess":

If your Object is on the Form or in the ComponentsTray, then you can iterate through that collection until you've found your Object during Design-Time. This can be done by capturing the OnComponentAdded-Event of the IComponentChangeService (This service would be implemented in your custom control). You could also set a trigger in the OnComponentChanged-Event when the Property of "PropertyName" has changed.

This all of course can only work if your Object to which you want to bind is on the Form or ComponentTray (during design-time).
If this approach could help you, I would be glad to "get you on the road" with the 'IComponentChangeService' .

Regards,

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:uC**************@TK2MSFTNGP09.phx.gbl...
Michael,

Thanks for the reply... I'm not 100% sure I'm following how I would use your advice!

I've done some more work on the subject, and ideally it looks like I need some way of dynamically "getting" the object that I want to bind to from within my control.

eg something like...

ObjectName="name_of_object_on_form"
PropertyName="name_of_property_to bind_to"
dim obj as object
obj=XXXXXX(objectname) <---- this is the line I need which retrieves the object from the parent form.
me.databinding.add("text",obj,PropertyName)

I think if I could work this out then I would solve the problem?

Regards
Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
Hi Simon,

I might be entirely "off track" but if I understand your issue well, you could use a "IComponentChangeService".
I have created a Component which "Watches" all Components/Controls Added, Removed, Property Edited, ... on a Windows Form.
You could also use a "IComponentChangeService" directly in your Custom Control.

Having this done you could catch the "OnComponentAdded"-Event and put some code in it to set your bindings.

As I said: I'm not sure if I fully understood your problem, but if I did I think this might be a solution.

Regards,

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Terry,

No it's a Windows Form..

It refers to my question last week about data binding to a simple object. Which works fine... except that you need to manually add some code to databind the text boxes to the object in your main form...

I'm trying to get is so that somehow this step is automated.

I've created an inherited text box that allows me to enter the name of the object and the property to bind to. This is the easy bit.. I'm still working on some way of getting the actual binding to be "automatic" ie not require the binding code to be added manually. I'd therefore like the binding code to be added automatically in the control intialization code where it sets all the control properties.. or alternatively, find some way of doing the binding from the control itself, once the object and property names are set...

Does this make sense?

Regards
Simon
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:eF**************@TK2MSFTNGP11.phx.gbl...
I assume this is a webform you are talking about. If so, each userconrol has its owen Page_load event. In this you should be able to bind and object in the parent container, dont have time to look up the properties now but in psuedo I imagine if would be something like.
Me.Parent.Control.DataBindings.Add( Text, me, Property)

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:OA**************@TK2MSFTNGP09.phx.gbl...
Hope somebody can help!

I want to automatically be able to add code to the initialize routine on a Windows form when I add a custom control that I've written to a form.

Specifically, I'm trying to data bind to a normal class. So I've extended the standard text box to include a field for object name and property name. I want to be able to add a line such as :

controlname.DataBindings.Add("Text", objectName, "myPropertyName")
I've tried using google and looking at the documentation but can't seem to find this... I don't know if there is an alternative way of doing it in the startup code of the form (I seem to have a thought that reflection could be used to get the instance of the object for the data binding code above but I can't work out how!!).

I would be grateful for any pointers.

Many thanks in advance

Simon
Nov 20 '05 #10

P: n/a
OK Simon, heres one way to do it. ( in my case the bindings were to a checked property on the class and a checkbox on the usercontrol
Dim f As Form1 = Me.ParentForm

Me.CheckBox1.DataBindings.Add("Checked", f.aPerson, "Checked")
--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:eh*************@tk2msftngp13.phx.gbl...
Michael,

If you have a code sample that you could post, I'd be eternally grateful ...

Regards
Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:eI**************@tk2msftngp13.phx.gbl...
Hi Simon.

Just a "wild guess":

If your Object is on the Form or in the ComponentsTray, then you can iterate through that collection until you've found your Object during Design-Time. This can be done by capturing the OnComponentAdded-Event of the IComponentChangeService (This service would be implemented in your custom control). You could also set a trigger in the OnComponentChanged-Event when the Property of "PropertyName" has changed.

This all of course can only work if your Object to which you want to bind is on the Form or ComponentTray (during design-time).
If this approach could help you, I would be glad to "get you on the road" with the 'IComponentChangeService' .

Regards,

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:uC**************@TK2MSFTNGP09.phx.gbl...
Michael,

Thanks for the reply... I'm not 100% sure I'm following how I would use your advice!

I've done some more work on the subject, and ideally it looks like I need some way of dynamically "getting" the object that I want to bind to from within my control.

eg something like...

ObjectName="name_of_object_on_form"
PropertyName="name_of_property_to bind_to"
dim obj as object
obj=XXXXXX(objectname) <---- this is the line I need which retrieves the object from the parent form.
me.databinding.add("text",obj,PropertyName)

I think if I could work this out then I would solve the problem?

Regards
Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
Hi Simon,

I might be entirely "off track" but if I understand your issue well, you could use a "IComponentChangeService".
I have created a Component which "Watches" all Components/Controls Added, Removed, Property Edited, ... on a Windows Form.
You could also use a "IComponentChangeService" directly in your Custom Control.

Having this done you could catch the "OnComponentAdded"-Event and put some code in it to set your bindings.

As I said: I'm not sure if I fully understood your problem, but if I did I think this might be a solution.

Regards,

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Terry,

No it's a Windows Form..

It refers to my question last week about data binding to a simple object. Which works fine... except that you need to manually add some code to databind the text boxes to the object in your main form...

I'm trying to get is so that somehow this step is automated.

I've created an inherited text box that allows me to enter the name of the object and the property to bind to. This is the easy bit.. I'm still working on some way of getting the actual binding to be "automatic" ie not require the binding code to be added manually. I'd therefore like the binding code to be added automatically in the control intialization code where it sets all the control properties.. or alternatively, find some way of doing the binding from the control itself, once the object and property names are set...

Does this make sense?

Regards
Simon
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:eF**************@TK2MSFTNGP11.phx.gbl...
I assume this is a webform you are talking about. If so, each userconrol has its owen Page_load event. In this you should be able to bind and object in the parent container, dont have time to look up the properties now but in psuedo I imagine if would be something like.
Me.Parent.Control.DataBindings.Add( Text, me, Property)

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:OA**************@TK2MSFTNGP09.phx.gbl...
Hope somebody can help!

I want to automatically be able to add code to the initialize routine on a Windows form when I add a custom control that I've written to a form.

Specifically, I'm trying to data bind to a normal class. So I've extended the standard text box to include a field for object name and property name. I want to be able to add a line such as :

controlname.DataBindings.Add("Text", objectName, "myPropertyName")
I've tried using google and looking at the documentation but can't seem to find this... I don't know if there is an alternative way of doing it in the startup code of the form (I seem to have a thought that reflection could be used to get the instance of the object for the data binding code above but I can't work out how!!).

I would be grateful for any pointers.

Many thanks in advance

Simon
Nov 20 '05 #11

P: n/a
Hi Simon,

I will look into it tonight or tomorrow.

Until then!

J

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:eh*************@tk2msftngp13.phx.gbl...
Michael,

If you have a code sample that you could post, I'd be eternally grateful ...

Regards
Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:eI**************@tk2msftngp13.phx.gbl...
Hi Simon.

Just a "wild guess":

If your Object is on the Form or in the ComponentsTray, then you can iterate through that collection until you've found your Object during Design-Time. This can be done by capturing the OnComponentAdded-Event of the IComponentChangeService (This service would be implemented in your custom control). You could also set a trigger in the OnComponentChanged-Event when the Property of "PropertyName" has changed.

This all of course can only work if your Object to which you want to bind is on the Form or ComponentTray (during design-time).
If this approach could help you, I would be glad to "get you on the road" with the 'IComponentChangeService' .

Regards,

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:uC**************@TK2MSFTNGP09.phx.gbl...
Michael,

Thanks for the reply... I'm not 100% sure I'm following how I would use your advice!

I've done some more work on the subject, and ideally it looks like I need some way of dynamically "getting" the object that I want to bind to from within my control.

eg something like...

ObjectName="name_of_object_on_form"
PropertyName="name_of_property_to bind_to"
dim obj as object
obj=XXXXXX(objectname) <---- this is the line I need which retrieves the object from the parent form.
me.databinding.add("text",obj,PropertyName)

I think if I could work this out then I would solve the problem?

Regards
Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
Hi Simon,

I might be entirely "off track" but if I understand your issue well, you could use a "IComponentChangeService".
I have created a Component which "Watches" all Components/Controls Added, Removed, Property Edited, ... on a Windows Form.
You could also use a "IComponentChangeService" directly in your Custom Control.

Having this done you could catch the "OnComponentAdded"-Event and put some code in it to set your bindings.

As I said: I'm not sure if I fully understood your problem, but if I did I think this might be a solution.

Regards,

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Terry,

No it's a Windows Form..

It refers to my question last week about data binding to a simple object. Which works fine... except that you need to manually add some code to databind the text boxes to the object in your main form...

I'm trying to get is so that somehow this step is automated.

I've created an inherited text box that allows me to enter the name of the object and the property to bind to. This is the easy bit.. I'm still working on some way of getting the actual binding to be "automatic" ie not require the binding code to be added manually. I'd therefore like the binding code to be added automatically in the control intialization code where it sets all the control properties.. or alternatively, find some way of doing the binding from the control itself, once the object and property names are set...

Does this make sense?

Regards
Simon
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:eF**************@TK2MSFTNGP11.phx.gbl...
I assume this is a webform you are talking about. If so, each userconrol has its owen Page_load event. In this you should be able to bind and object in the parent container, dont have time to look up the properties now but in psuedo I imagine if would be something like.
Me.Parent.Control.DataBindings.Add( Text, me, Property)

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:OA**************@TK2MSFTNGP09.phx.gbl...
Hope somebody can help!

I want to automatically be able to add code to the initialize routine on a Windows form when I add a custom control that I've written to a form.

Specifically, I'm trying to data bind to a normal class. So I've extended the standard text box to include a field for object name and property name. I want to be able to add a line such as :

controlname.DataBindings.Add("Text", objectName, "myPropertyName")
I've tried using google and looking at the documentation but can't seem to find this... I don't know if there is an alternative way of doing it in the startup code of the form (I seem to have a thought that reflection could be used to get the instance of the object for the data binding code above but I can't work out how!!).

I would be grateful for any pointers.

Many thanks in advance

Simon
Nov 20 '05 #12

P: n/a
Terry,

Thanks for that.

I presume that in your example, "aperson" is an object created within the form. My scenario is identical EXCEPT that I don't know at coding time the name of the object - it will be dynamically assigned at design time...

Regards
Simon
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
OK Simon, heres one way to do it. ( in my case the bindings were to a checked property on the class and a checkbox on the usercontrol
Dim f As Form1 = Me.ParentForm

Me.CheckBox1.DataBindings.Add("Checked", f.aPerson, "Checked")
--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:eh*************@tk2msftngp13.phx.gbl...
Michael,

If you have a code sample that you could post, I'd be eternally grateful ...

Regards
Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:eI**************@tk2msftngp13.phx.gbl...
Hi Simon.

Just a "wild guess":

If your Object is on the Form or in the ComponentsTray, then you can iterate through that collection until you've found your Object during Design-Time. This can be done by capturing the OnComponentAdded-Event of the IComponentChangeService (This service would be implemented in your custom control). You could also set a trigger in the OnComponentChanged-Event when the Property of "PropertyName" has changed.

This all of course can only work if your Object to which you want to bind is on the Form or ComponentTray (during design-time).
If this approach could help you, I would be glad to "get you on the road" with the 'IComponentChangeService' .

Regards,

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:uC**************@TK2MSFTNGP09.phx.gbl...
Michael,

Thanks for the reply... I'm not 100% sure I'm following how I would use your advice!

I've done some more work on the subject, and ideally it looks like I need some way of dynamically "getting" the object that I want to bind to from within my control.

eg something like...

ObjectName="name_of_object_on_form"
PropertyName="name_of_property_to bind_to"
dim obj as object
obj=XXXXXX(objectname) <---- this is the line I need which retrieves the object from the parent form.
me.databinding.add("text",obj,PropertyName)

I think if I could work this out then I would solve the problem?

Regards
Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
Hi Simon,

I might be entirely "off track" but if I understand your issue well, you could use a "IComponentChangeService".
I have created a Component which "Watches" all Components/Controls Added, Removed, Property Edited, ... on a Windows Form.
You could also use a "IComponentChangeService" directly in your Custom Control.

Having this done you could catch the "OnComponentAdded"-Event and put some code in it to set your bindings.

As I said: I'm not sure if I fully understood your problem, but if I did I think this might be a solution.

Regards,

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Terry,

No it's a Windows Form..

It refers to my question last week about data binding to a simple object. Which works fine... except that you need to manually add some code to databind the text boxes to the object in your main form...

I'm trying to get is so that somehow this step is automated.

I've created an inherited text box that allows me to enter the name of the object and the property to bind to. This is the easy bit.. I'm still working on some way of getting the actual binding to be "automatic" ie not require the binding code to be added manually. I'd therefore like the binding code to be added automatically in the control intialization code where it sets all the control properties.. or alternatively, find some way of doing the binding from the control itself, once the object and property names are set...

Does this make sense?

Regards
Simon
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:eF**************@TK2MSFTNGP11.phx.gbl...
I assume this is a webform you are talking about. If so, each userconrol has its owen Page_load event. In this you should be able to bind and object in the parent container, dont have time to look up the properties now but in psuedo I imagine if would be something like.
Me.Parent.Control.DataBindings.Add( Text, me, Property)

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:OA**************@TK2MSFTNGP09.phx.gbl...
Hope somebody can help!

I want to automatically be able to add code to the initialize routine on a Windows form when I add a custom control that I've written to a form.

Specifically, I'm trying to data bind to a normal class. So I've extended the standard text box to include a field for object name and property name. I want to be able to add a line such as :

controlname.DataBindings.Add("Text", objectName, "myPropertyName")
I've tried using google and looking at the documentation but can't seem to find this... I don't know if there is an alternative way of doing it in the startup code of the form (I seem to have a thought that reflection could be used to get the instance of the object for the data binding code above but I can't work out how!!).

I would be grateful for any pointers.

Many thanks in advance

Simon
Nov 20 '05 #13

P: n/a
OK Simon,

Here we go.......
Remember: this is a very Basic example without any error-handling, etc.... You should defenitly include that, else you will receive some errors at design-time.

If I understood your issue and you know how to set the Binding to your Objects, then I beleive this will defenitly help you.

Best regards,

Michael

Imports System.ComponentModel

Imports System.ComponentModel.Design

''' -----------------------------------------------------------------------------

''' Project : ObjectBoundTextBox

''' Class : ojbTextBox

'''

''' -----------------------------------------------------------------------------

''' <summary>

''' Just a 'Basic-Demonstration'

''' </summary>

''' <remarks>

''' </remarks>

''' <history>

''' [Michael.Maes] 28/06/2004 Created

''' </history>

''' -----------------------------------------------------------------------------

Public Class ojbTextBox

Inherits System.Windows.Forms.TextBox

#Region " Declarations "

Dim ChangeService As IComponentChangeService

#End Region

#Region " Properties "

Dim _ObjectName As String

<EditorBrowsable(EditorBrowsableState.Always), _

Browsable(True), _

Category("Data"), _

Description("The name of an object this TextBox must bind to.")> _

Public Property ObjectName() As String

Get

Return _ObjectName

End Get

Set(ByVal Value As String)

_ObjectName = Value

End Set

End Property

Dim _PropertyName As String

<EditorBrowsable(EditorBrowsableState.Always), _

Browsable(True), _

Category("Data"), _

Description("The name of the property this TextBox must bind to.")> _

Public Property PropertyName() As String

Get

Return _PropertyName

End Get

Set(ByVal Value As String)

_PropertyName = Value

End Set

End Property

Public Overrides Property Site() As ISite

Get

Return MyBase.Site

End Get

Set(ByVal Value As ISite)

ClearChangeNotifications()

MyBase.Site = Value

ChangeService = CType(GetService(GetType(IComponentChangeService)) , IComponentChangeService)

RegisterChangeNotifications()

End Set

End Property

#End Region

#Region " Event-Handlers "

Private Sub OnComponentChanged(ByVal sender As Object, ByVal ce As ComponentChangedEventArgs)

If Not (ce.Component Is Nothing) And Not (ce.Member Is Nothing) Then

Select Case ce.Member.Name

Case "ObjectName", "PropertyName"

If ce.Component.ReferenceEquals(ce.Component, Me) Then

' We have the right Instance of our ojbTextBox, so continue...

' Here you have to put your Handling code to Bind to your Object.

DirectCast(ce.Component, ojbTextBox).Text = ce.NewValue.ToString ' This is just a "Dummy Sample"

End If

End Select

End If

End Sub

Private Sub OnComponentChanging(ByVal sender As Object, ByVal ce As ComponentChangingEventArgs)
End Sub

Private Sub OnComponentAdded(ByVal sender As Object, ByVal ce As ComponentEventArgs)
End Sub

Private Sub OnComponentAdding(ByVal sender As Object, ByVal ce As ComponentEventArgs)
End Sub

Private Sub OnComponentRemoved(ByVal sender As Object, ByVal ce As ComponentEventArgs)
End Sub

Private Sub OnComponentRemoving(ByVal sender As Object, ByVal ce As ComponentEventArgs)
End Sub

Private Sub OnComponentRename(ByVal sender As Object, ByVal ce As ComponentRenameEventArgs)
End Sub

#End Region

#Region " ChangeNotifications "

Private Sub ClearChangeNotifications()

ChangeService = CType(GetService(GetType(IComponentChangeService)) , IComponentChangeService)

If Not (ChangeService Is Nothing) Then

RemoveHandler ChangeService.ComponentChanged, AddressOf OnComponentChanged

RemoveHandler ChangeService.ComponentChanging, AddressOf OnComponentChanging

RemoveHandler ChangeService.ComponentAdded, AddressOf OnComponentAdded

RemoveHandler ChangeService.ComponentAdding, AddressOf OnComponentAdding

RemoveHandler ChangeService.ComponentRemoved, AddressOf OnComponentRemoved

RemoveHandler ChangeService.ComponentRemoving, AddressOf OnComponentRemoving

RemoveHandler ChangeService.ComponentRename, AddressOf OnComponentRename

End If

End Sub

Private Sub RegisterChangeNotifications()

If Not (ChangeService Is Nothing) Then

AddHandler ChangeService.ComponentChanged, AddressOf OnComponentChanged

AddHandler ChangeService.ComponentChanging, AddressOf OnComponentChanging

AddHandler ChangeService.ComponentAdded, AddressOf OnComponentAdded

AddHandler ChangeService.ComponentAdding, AddressOf OnComponentAdding

AddHandler ChangeService.ComponentRemoved, AddressOf OnComponentRemoved

AddHandler ChangeService.ComponentRemoving, AddressOf OnComponentRemoving

AddHandler ChangeService.ComponentRename, AddressOf OnComponentRename

End If

End Sub

#End Region

End Class

Nov 20 '05 #14

P: n/a
Michael,

Thanks very much for digging out the code.

Regards
Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:u$**************@TK2MSFTNGP11.phx.gbl...
OK Simon,

Here we go.......
Remember: this is a very Basic example without any error-handling, etc.... You should defenitly include that, else you will receive some errors at design-time.

If I understood your issue and you know how to set the Binding to your Objects, then I beleive this will defenitly help you.

Best regards,

Michael

Imports System.ComponentModel

Imports System.ComponentModel.Design

''' -----------------------------------------------------------------------------

''' Project : ObjectBoundTextBox

''' Class : ojbTextBox

'''

''' -----------------------------------------------------------------------------

''' <summary>

''' Just a 'Basic-Demonstration'

''' </summary>

''' <remarks>

''' </remarks>

''' <history>

''' [Michael.Maes] 28/06/2004 Created

''' </history>

''' -----------------------------------------------------------------------------

Public Class ojbTextBox

Inherits System.Windows.Forms.TextBox

#Region " Declarations "

Dim ChangeService As IComponentChangeService

#End Region

#Region " Properties "

Dim _ObjectName As String

<EditorBrowsable(EditorBrowsableState.Always), _

Browsable(True), _

Category("Data"), _

Description("The name of an object this TextBox must bind to.")> _

Public Property ObjectName() As String

Get

Return _ObjectName

End Get

Set(ByVal Value As String)

_ObjectName = Value

End Set

End Property

Dim _PropertyName As String

<EditorBrowsable(EditorBrowsableState.Always), _

Browsable(True), _

Category("Data"), _

Description("The name of the property this TextBox must bind to.")> _

Public Property PropertyName() As String

Get

Return _PropertyName

End Get

Set(ByVal Value As String)

_PropertyName = Value

End Set

End Property

Public Overrides Property Site() As ISite

Get

Return MyBase.Site

End Get

Set(ByVal Value As ISite)

ClearChangeNotifications()

MyBase.Site = Value

ChangeService = CType(GetService(GetType(IComponentChangeService)) , IComponentChangeService)

RegisterChangeNotifications()

End Set

End Property

#End Region

#Region " Event-Handlers "

Private Sub OnComponentChanged(ByVal sender As Object, ByVal ce As ComponentChangedEventArgs)

If Not (ce.Component Is Nothing) And Not (ce.Member Is Nothing) Then

Select Case ce.Member.Name

Case "ObjectName", "PropertyName"

If ce.Component.ReferenceEquals(ce.Component, Me) Then

' We have the right Instance of our ojbTextBox, so continue...

' Here you have to put your Handling code to Bind to your Object.

DirectCast(ce.Component, ojbTextBox).Text = ce.NewValue.ToString ' This is just a "Dummy Sample"

End If

End Select

End If

End Sub

Private Sub OnComponentChanging(ByVal sender As Object, ByVal ce As ComponentChangingEventArgs)
End Sub

Private Sub OnComponentAdded(ByVal sender As Object, ByVal ce As ComponentEventArgs)
End Sub

Private Sub OnComponentAdding(ByVal sender As Object, ByVal ce As ComponentEventArgs)
End Sub

Private Sub OnComponentRemoved(ByVal sender As Object, ByVal ce As ComponentEventArgs)
End Sub

Private Sub OnComponentRemoving(ByVal sender As Object, ByVal ce As ComponentEventArgs)
End Sub

Private Sub OnComponentRename(ByVal sender As Object, ByVal ce As ComponentRenameEventArgs)
End Sub

#End Region

#Region " ChangeNotifications "

Private Sub ClearChangeNotifications()

ChangeService = CType(GetService(GetType(IComponentChangeService)) , IComponentChangeService)

If Not (ChangeService Is Nothing) Then

RemoveHandler ChangeService.ComponentChanged, AddressOf OnComponentChanged

RemoveHandler ChangeService.ComponentChanging, AddressOf OnComponentChanging

RemoveHandler ChangeService.ComponentAdded, AddressOf OnComponentAdded

RemoveHandler ChangeService.ComponentAdding, AddressOf OnComponentAdding

RemoveHandler ChangeService.ComponentRemoved, AddressOf OnComponentRemoved

RemoveHandler ChangeService.ComponentRemoving, AddressOf OnComponentRemoving

RemoveHandler ChangeService.ComponentRename, AddressOf OnComponentRename

End If

End Sub

Private Sub RegisterChangeNotifications()

If Not (ChangeService Is Nothing) Then

AddHandler ChangeService.ComponentChanged, AddressOf OnComponentChanged

AddHandler ChangeService.ComponentChanging, AddressOf OnComponentChanging

AddHandler ChangeService.ComponentAdded, AddressOf OnComponentAdded

AddHandler ChangeService.ComponentAdding, AddressOf OnComponentAdding

AddHandler ChangeService.ComponentRemoved, AddressOf OnComponentRemoved

AddHandler ChangeService.ComponentRemoving, AddressOf OnComponentRemoving

AddHandler ChangeService.ComponentRename, AddressOf OnComponentRename

End If

End Sub

#End Region

End Class

Nov 20 '05 #15

P: n/a
Hey Simon,

Does this fully answer your questions, because I came to the conclusion I didn't point out how to actually set your binding.

On the other hand, I was thinking if your DataObjects are on the Form or ComponentTray, you could set the Binding in Design-time through the Property-Window. Sure your DataObjects must all inherit from the same BaseClass, so you could do something like:
Dim _DataSource As YourBaseClass

<EditorBrowsable(EditorBrowsableState.Always), _

Category("Data"), _

Browsable(True), _

Bindable(True), _

Description("The DataObject to bind the TextBox to."), _

TypeConverter("System.Windows.Forms.Design.DataSou rceConverter, System.Design"), _

RefreshProperties(RefreshProperties.All)> _

Public Overloads Property DataSource() As Object

Get

Return _DataSource

End Get

Set(ByVal Value As Object)

_DataSource = Value

End Set

End Property

Should you use this approach, then you have to fine-tune this property, because it's for Complex-DataBinding and as you know, the TextBox-Class has Simple DataBinding. Doing this you would get a dropdown-list with all possible DataObjects your TextBox can bind to.

Anyway: should you furthermore want to make use of the 'IComponentChangeService' then you would defently need Reflection to Get/Set some values of Components/Controls which you don't know of in Design-Time. Something like:
Private Sub OnComponentRename(ByVal sender As Object, ByVal ce As ComponentRenameEventArgs)

If IsNothing(ce) Or IsNothing(ce.Component) Then Exit Sub

Dim ot As Object = ce.Component

Dim s As String = ot.GetType.ToString

Dim p As PropertyInfo

' Set a Boolean-Property
p = ot.GetType.GetProperty("MyDesiredPropertyName")

p.SetValue(ot, False, Nothing)

' Set a String-Property

p = ot.GetType.GetProperty("MyOtherDesiredPropertyName ")

p.SetValue(ot, ce.NewName, Nothing)

' Continue other logic

.................................................. ........

.................................................. ........

End Sub

Also keep in mind with the 'IComponentChangeService' that you address the right INSTANCE, because if you have two or more controls of the same type on a form, then it will loop through all those controls (even if this service is called from within a single control)!!

I wish you the best, and let me know if you could manage your goal.

Kind regards,

Michael

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:uA**************@tk2msftngp13.phx.gbl...
Michael,

Thanks very much for digging out the code.

Regards
Simon
Nov 20 '05 #16

P: n/a
Yes aPerson is an object created ( like in the previous example from Jay ) in the main form. If you dont know the name of this object, then use reflection to establish this.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
Terry,

Thanks for that.

I presume that in your example, "aperson" is an object created within the form. My scenario is identical EXCEPT that I don't know at coding time the name of the object - it will be dynamically assigned at design time...

Regards
Simon
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
OK Simon, heres one way to do it. ( in my case the bindings were to a checked property on the class and a checkbox on the usercontrol
Dim f As Form1 = Me.ParentForm

Me.CheckBox1.DataBindings.Add("Checked", f.aPerson, "Checked")
--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:eh*************@tk2msftngp13.phx.gbl...
Michael,

If you have a code sample that you could post, I'd be eternally grateful ...

Regards
Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:eI**************@tk2msftngp13.phx.gbl...
Hi Simon.

Just a "wild guess":

If your Object is on the Form or in the ComponentsTray, then you can iterate through that collection until you've found your Object during Design-Time. This can be done by capturing the OnComponentAdded-Event of the IComponentChangeService (This service would be implemented in your custom control). You could also set a trigger in the OnComponentChanged-Event when the Property of "PropertyName" has changed.

This all of course can only work if your Object to which you want to bind is on the Form or ComponentTray (during design-time).
If this approach could help you, I would be glad to "get you on the road" with the 'IComponentChangeService' .

Regards,

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:uC**************@TK2MSFTNGP09.phx.gbl...
Michael,

Thanks for the reply... I'm not 100% sure I'm following how I would use your advice!

I've done some more work on the subject, and ideally it looks like I need some way of dynamically "getting" the object that I want to bind to from within my control.

eg something like...

ObjectName="name_of_object_on_form"
PropertyName="name_of_property_to bind_to"
dim obj as object
obj=XXXXXX(objectname) <---- this is the line I need which retrieves the object from the parent form.
me.databinding.add("text",obj,PropertyName)

I think if I could work this out then I would solve the problem?

Regards
Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
Hi Simon,

I might be entirely "off track" but if I understand your issue well, you could use a "IComponentChangeService".
I have created a Component which "Watches" all Components/Controls Added, Removed, Property Edited, ... on a Windows Form.
You could also use a "IComponentChangeService" directly in your Custom Control.

Having this done you could catch the "OnComponentAdded"-Event and put some code in it to set your bindings.

As I said: I'm not sure if I fully understood your problem, but if I did I think this might be a solution.

Regards,

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Terry,

No it's a Windows Form..

It refers to my question last week about data binding to a simple object. Which works fine... except that you need to manually add some code to databind the text boxes to the object in your main form...

I'm trying to get is so that somehow this step is automated.

I've created an inherited text box that allows me to enter the name of the object and the property to bind to. This is the easy bit.. I'm still working on some way of getting the actual binding to be "automatic" ie not require the binding code to be added manually. I'd therefore like the binding code to be added automatically in the control intialization code where it sets all the control properties.. or alternatively, find some way of doing the binding from the control itself, once the object and property names are set...

Does this make sense?

Regards
Simon
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:eF**************@TK2MSFTNGP11.phx.gbl...
I assume this is a webform you are talking about. If so, each userconrol has its owen Page_load event. In this you should be able to bind and object in the parent container, dont have time to look up the properties now but in psuedo I imagine if would be something like.
Me.Parent.Control.DataBindings.Add( Text, me, Property)

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:OA**************@TK2MSFTNGP09.phx.gbl...
Hope somebody can help!

I want to automatically be able to add code to the initialize routine on a Windows form when I add a custom control that I've written to a form.

Specifically, I'm trying to data bind to a normal class. So I've extended the standard text box to include a field for object name and property name. I want to be able to add a line such as :

controlname.DataBindings.Add("Text", objectName, "myPropertyName")
I've tried using google and looking at the documentation but can't seem to find this... I don't know if there is an alternative way of doing it in the startup code of the form (I seem to have a thought that reflection could be used to get the instance of the object for the data binding code above but I can't work out how!!).

I would be grateful for any pointers.

Many thanks in advance

Simon
Nov 20 '05 #17

P: n/a
that was my problem I think!! I'm not sure how to code using reflection to get this!! <G>

regards
Simon
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:OX**************@TK2MSFTNGP11.phx.gbl...
Yes aPerson is an object created ( like in the previous example from Jay ) in the main form. If you dont know the name of this object, then use reflection to establish this.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:%2****************@TK2MSFTNGP12.phx.gbl...
Terry,

Thanks for that.

I presume that in your example, "aperson" is an object created within the form. My scenario is identical EXCEPT that I don't know at coding time the name of the object - it will be dynamically assigned at design time...

Regards
Simon
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
OK Simon, heres one way to do it. ( in my case the bindings were to a checked property on the class and a checkbox on the usercontrol
Dim f As Form1 = Me.ParentForm

Me.CheckBox1.DataBindings.Add("Checked", f.aPerson, "Checked")
--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:eh*************@tk2msftngp13.phx.gbl...
Michael,

If you have a code sample that you could post, I'd be eternally grateful ...

Regards
Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:eI**************@tk2msftngp13.phx.gbl...
Hi Simon.

Just a "wild guess":

If your Object is on the Form or in the ComponentsTray, then you can iterate through that collection until you've found your Object during Design-Time. This can be done by capturing the OnComponentAdded-Event of the IComponentChangeService (This service would be implemented in your custom control). You could also set a trigger in the OnComponentChanged-Event when the Property of "PropertyName" has changed.

This all of course can only work if your Object to which you want to bind is on the Form or ComponentTray (during design-time).
If this approach could help you, I would be glad to "get you on the road" with the 'IComponentChangeService' .

Regards,

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:uC**************@TK2MSFTNGP09.phx.gbl...
Michael,

Thanks for the reply... I'm not 100% sure I'm following how I would use your advice!

I've done some more work on the subject, and ideally it looks like I need some way of dynamically "getting" the object that I want to bind to from within my control.

eg something like...

ObjectName="name_of_object_on_form"
PropertyName="name_of_property_to bind_to"
dim obj as object
obj=XXXXXX(objectname) <---- this is the line I need which retrieves the object from the parent form.
me.databinding.add("text",obj,PropertyName)

I think if I could work this out then I would solve the problem?

Regards
Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
Hi Simon,

I might be entirely "off track" but if I understand your issue well, you could use a "IComponentChangeService".
I have created a Component which "Watches" all Components/Controls Added, Removed, Property Edited, ... on a Windows Form.
You could also use a "IComponentChangeService" directly in your Custom Control.

Having this done you could catch the "OnComponentAdded"-Event and put some code in it to set your bindings.

As I said: I'm not sure if I fully understood your problem, but if I did I think this might be a solution.

Regards,

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Terry,

No it's a Windows Form..

It refers to my question last week about data binding to a simple object. Which works fine... except that you need to manually add some code to databind the text boxes to the object in your main form...

I'm trying to get is so that somehow this step is automated.

I've created an inherited text box that allows me to enter the name of the object and the property to bind to. This is the easy bit.. I'm still working on some way of getting the actual binding to be "automatic" ie not require the binding code to be added manually. I'd therefore like the binding code to be added automatically in the control intialization code where it sets all the control properties.. or alternatively, find some way of doing the binding from the control itself, once the object and property names are set...

Does this make sense?

Regards
Simon
"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message news:eF**************@TK2MSFTNGP11.phx.gbl...
I assume this is a webform you are talking about. If so, each userconrol has its owen Page_load event. In this you should be able to bind and object in the parent container, dont have time to look up the properties now but in psuedo I imagine if would be something like.
Me.Parent.Control.DataBindings.Add( Text, me, Property)

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:OA**************@TK2MSFTNGP09.phx.gbl...
Hope somebody can help!

I want to automatically be able to add code to the initialize routine on a Windows form when I add a custom control that I've written to a form.

Specifically, I'm trying to data bind to a normal class. So I've extended the standard text box to include a field for object name and property name. I want to be able to add a line such as :

controlname.DataBindings.Add("Text", objectName, "myPropertyName")
I've tried using google and looking at the documentation but can't seem to find this... I don't know if there is an alternative way of doing it in the startup code of the form (I seem to have a thought that reflection could be used to get the instance of the object for the data binding code above but I can't work out how!!).

I would be grateful for any pointers.

Many thanks in advance

Simon
Nov 20 '05 #18

P: n/a
Thanks Michael,

I think I'll sit down when I have a quiet few hours and play with this... I think I have to get to grip with the concepts that you have outlined.

I'll let you know how I get on!

Regards
Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Hey Simon,

Does this fully answer your questions, because I came to the conclusion I didn't point out how to actually set your binding.

On the other hand, I was thinking if your DataObjects are on the Form or ComponentTray, you could set the Binding in Design-time through the Property-Window. Sure your DataObjects must all inherit from the same BaseClass, so you could do something like:
Dim _DataSource As YourBaseClass

<EditorBrowsable(EditorBrowsableState.Always), _

Category("Data"), _

Browsable(True), _

Bindable(True), _

Description("The DataObject to bind the TextBox to."), _

TypeConverter("System.Windows.Forms.Design.DataSou rceConverter, System.Design"), _

RefreshProperties(RefreshProperties.All)> _

Public Overloads Property DataSource() As Object

Get

Return _DataSource

End Get

Set(ByVal Value As Object)

_DataSource = Value

End Set

End Property

Should you use this approach, then you have to fine-tune this property, because it's for Complex-DataBinding and as you know, the TextBox-Class has Simple DataBinding. Doing this you would get a dropdown-list with all possible DataObjects your TextBox can bind to.

Anyway: should you furthermore want to make use of the 'IComponentChangeService' then you would defently need Reflection to Get/Set some values of Components/Controls which you don't know of in Design-Time. Something like:
Private Sub OnComponentRename(ByVal sender As Object, ByVal ce As ComponentRenameEventArgs)

If IsNothing(ce) Or IsNothing(ce.Component) Then Exit Sub

Dim ot As Object = ce.Component

Dim s As String = ot.GetType.ToString

Dim p As PropertyInfo

' Set a Boolean-Property
p = ot.GetType.GetProperty("MyDesiredPropertyName")

p.SetValue(ot, False, Nothing)

' Set a String-Property

p = ot.GetType.GetProperty("MyOtherDesiredPropertyName ")

p.SetValue(ot, ce.NewName, Nothing)

' Continue other logic

.................................................. .......

.................................................. .......

End Sub

Also keep in mind with the 'IComponentChangeService' that you address the right INSTANCE, because if you have two or more controls of the same type on a form, then it will loop through all those controls (even if this service is called from within a single control)!!

I wish you the best, and let me know if you could manage your goal.

Kind regards,

Michael

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:uA**************@tk2msftngp13.phx.gbl...
Michael,

Thanks very much for digging out the code.

Regards
Simon
Nov 20 '05 #19

P: n/a
Dont forget to sleep J

All the best and "play a lot" with the 'IComponentChangeService'. It's powerfull to use (but a hell to Debug...Since it's IDE)

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Thanks Michael,

I think I'll sit down when I have a quiet few hours and play with this... I think I have to get to grip with the concepts that you have outlined.

I'll let you know how I get on!

Regards
Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Hey Simon,

Does this fully answer your questions, because I came to the conclusion I didn't point out how to actually set your binding.

On the other hand, I was thinking if your DataObjects are on the Form or ComponentTray, you could set the Binding in Design-time through the Property-Window. Sure your DataObjects must all inherit from the same BaseClass, so you could do something like:
Dim _DataSource As YourBaseClass

<EditorBrowsable(EditorBrowsableState.Always), _

Category("Data"), _

Browsable(True), _

Bindable(True), _

Description("The DataObject to bind the TextBox to."), _

TypeConverter("System.Windows.Forms.Design.DataSou rceConverter, System.Design"), _

RefreshProperties(RefreshProperties.All)> _

Public Overloads Property DataSource() As Object

Get

Return _DataSource

End Get

Set(ByVal Value As Object)

_DataSource = Value

End Set

End Property

Should you use this approach, then you have to fine-tune this property, because it's for Complex-DataBinding and as you know, the TextBox-Class has Simple DataBinding. Doing this you would get a dropdown-list with all possible DataObjects your TextBox can bind to.

Anyway: should you furthermore want to make use of the 'IComponentChangeService' then you would defently need Reflection to Get/Set some values of Components/Controls which you don't know of in Design-Time. Something like:
Private Sub OnComponentRename(ByVal sender As Object, ByVal ce As ComponentRenameEventArgs)

If IsNothing(ce) Or IsNothing(ce.Component) Then Exit Sub

Dim ot As Object = ce.Component

Dim s As String = ot.GetType.ToString

Dim p As PropertyInfo

' Set a Boolean-Property
p = ot.GetType.GetProperty("MyDesiredPropertyName")

p.SetValue(ot, False, Nothing)

' Set a String-Property

p = ot.GetType.GetProperty("MyOtherDesiredPropertyName ")

p.SetValue(ot, ce.NewName, Nothing)

' Continue other logic

.................................................. .......

.................................................. .......

End Sub

Also keep in mind with the 'IComponentChangeService' that you address the right INSTANCE, because if you have two or more controls of the same type on a form, then it will loop through all those controls (even if this service is called from within a single control)!!

I wish you the best, and let me know if you could manage your goal.

Kind regards,

Michael

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:uA**************@tk2msftngp13.phx.gbl...
Michael,

Thanks very much for digging out the code.

Regards
Simon
Nov 20 '05 #20

P: n/a
sleep?? whats that????

Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:OA**************@TK2MSFTNGP11.phx.gbl...
Dont forget to sleep J

All the best and "play a lot" with the 'IComponentChangeService'. It's powerfull to use (but a hell to Debug...Since it's IDE)

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Thanks Michael,

I think I'll sit down when I have a quiet few hours and play with this... I think I have to get to grip with the concepts that you have outlined.

I'll let you know how I get on!

Regards
Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Hey Simon,

Does this fully answer your questions, because I came to the conclusion I didn't point out how to actually set your binding.

On the other hand, I was thinking if your DataObjects are on the Form or ComponentTray, you could set the Binding in Design-time through the Property-Window. Sure your DataObjects must all inherit from the same BaseClass, so you could do something like:
Dim _DataSource As YourBaseClass

<EditorBrowsable(EditorBrowsableState.Always), _

Category("Data"), _

Browsable(True), _

Bindable(True), _

Description("The DataObject to bind the TextBox to."), _

TypeConverter("System.Windows.Forms.Design.DataSou rceConverter, System.Design"), _

RefreshProperties(RefreshProperties.All)> _

Public Overloads Property DataSource() As Object

Get

Return _DataSource

End Get

Set(ByVal Value As Object)

_DataSource = Value

End Set

End Property

Should you use this approach, then you have to fine-tune this property, because it's for Complex-DataBinding and as you know, the TextBox-Class has Simple DataBinding. Doing this you would get a dropdown-list with all possible DataObjects your TextBox can bind to.

Anyway: should you furthermore want to make use of the 'IComponentChangeService' then you would defently need Reflection to Get/Set some values of Components/Controls which you don't know of in Design-Time. Something like:
Private Sub OnComponentRename(ByVal sender As Object, ByVal ce As ComponentRenameEventArgs)

If IsNothing(ce) Or IsNothing(ce.Component) Then Exit Sub

Dim ot As Object = ce.Component

Dim s As String = ot.GetType.ToString

Dim p As PropertyInfo

' Set a Boolean-Property
p = ot.GetType.GetProperty("MyDesiredPropertyName")

p.SetValue(ot, False, Nothing)

' Set a String-Property

p = ot.GetType.GetProperty("MyOtherDesiredPropertyName ")

p.SetValue(ot, ce.NewName, Nothing)

' Continue other logic

.................................................. .......

.................................................. .......

End Sub

Also keep in mind with the 'IComponentChangeService' that you address the right INSTANCE, because if you have two or more controls of the same type on a form, then it will loop through all those controls (even if this service is called from within a single control)!!

I wish you the best, and let me know if you could manage your goal.

Kind regards,

Michael

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:uA**************@tk2msftngp13.phx.gbl...
Michael,

Thanks very much for digging out the code.

Regards
Simon
Nov 20 '05 #21

P: n/a
J
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:On**************@TK2MSFTNGP10.phx.gbl...
sleep?? whats that????

Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:OA**************@TK2MSFTNGP11.phx.gbl...
Dont forget to sleep J

All the best and "play a lot" with the 'IComponentChangeService'. It's powerfull to use (but a hell to Debug...Since it's IDE)

Michael
"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Thanks Michael,

I think I'll sit down when I have a quiet few hours and play with this... I think I have to get to grip with the concepts that you have outlined.

I'll let you know how I get on!

Regards
Simon
"Michael Maes" <mi*****@merlot.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
Hey Simon,

Does this fully answer your questions, because I came to the conclusion I didn't point out how to actually set your binding.

On the other hand, I was thinking if your DataObjects are on the Form or ComponentTray, you could set the Binding in Design-time through the Property-Window. Sure your DataObjects must all inherit from the same BaseClass, so you could do something like:
Dim _DataSource As YourBaseClass

<EditorBrowsable(EditorBrowsableState.Always), _

Category("Data"), _

Browsable(True), _

Bindable(True), _

Description("The DataObject to bind the TextBox to."), _

TypeConverter("System.Windows.Forms.Design.DataSou rceConverter, System.Design"), _

RefreshProperties(RefreshProperties.All)> _

Public Overloads Property DataSource() As Object

Get

Return _DataSource

End Get

Set(ByVal Value As Object)

_DataSource = Value

End Set

End Property

Should you use this approach, then you have to fine-tune this property, because it's for Complex-DataBinding and as you know, the TextBox-Class has Simple DataBinding. Doing this you would get a dropdown-list with all possible DataObjects your TextBox can bind to.

Anyway: should you furthermore want to make use of the 'IComponentChangeService' then you would defently need Reflection to Get/Set some values of Components/Controls which you don't know of in Design-Time. Something like:
Private Sub OnComponentRename(ByVal sender As Object, ByVal ce As ComponentRenameEventArgs)

If IsNothing(ce) Or IsNothing(ce.Component) Then Exit Sub

Dim ot As Object = ce.Component

Dim s As String = ot.GetType.ToString

Dim p As PropertyInfo

' Set a Boolean-Property
p = ot.GetType.GetProperty("MyDesiredPropertyName")

p.SetValue(ot, False, Nothing)

' Set a String-Property

p = ot.GetType.GetProperty("MyOtherDesiredPropertyName ")

p.SetValue(ot, ce.NewName, Nothing)

' Continue other logic

.................................................. .......

.................................................. .......

End Sub

Also keep in mind with the 'IComponentChangeService' that you address the right INSTANCE, because if you have two or more controls of the same type on a form, then it will loop through all those controls (even if this service is called from within a single control)!!

I wish you the best, and let me know if you could manage your goal.

Kind regards,

Michael

"Simon Verona" <ne**@aphroditeuk.com> wrote in message news:uA**************@tk2msftngp13.phx.gbl...
Michael,

Thanks very much for digging out the code.

Regards
Simon
Nov 20 '05 #22

This discussion thread is closed

Replies have been disabled for this discussion.