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

Using Reflection to get a Field Value from Control

P: n/a
I am writing a simulation program, the main classes inherit from a base class
which itself inherits from Control. I am using reflection to serialize these
objects including the fields from control. This works perfectly apart from
the field text in Control which always comes back null. Although the IDE
always shows it having the correct value. Since the IDE presumably uses
reflection to show the value, I am at a loss to know why this one field is a
problem. Interestingly, the command window also comes back with a null, when
doing the same operation, but if you do the operation on the original object
just after creation it is correct. It is worth noting the objects are placed
in a Collection before serialization. So it is using FieldInfo.GetValue on
the items in the Collection which is failing on the string field.
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Tony,

Are you reflecting on the field, or on the property? I don't think that
most controls store the text themselves. Rather, when you call the Text
property, it calls SendMessage with a WM_GETTEXT message to get the text
contents of the control, which is why the text field would be blank.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"tony lock" <to******@discussions.microsoft.com> wrote in message
news:E0**********************************@microsof t.com...
I am writing a simulation program, the main classes inherit from a base
class
which itself inherits from Control. I am using reflection to serialize
these
objects including the fields from control. This works perfectly apart from
the field text in Control which always comes back null. Although the IDE
always shows it having the correct value. Since the IDE presumably uses
reflection to show the value, I am at a loss to know why this one field is
a
problem. Interestingly, the command window also comes back with a null,
when
doing the same operation, but if you do the operation on the original
object
just after creation it is correct. It is worth noting the objects are
placed
in a Collection before serialization. So it is using FieldInfo.GetValue on
the items in the Collection which is failing on the string field.

Nov 17 '05 #2

P: n/a
I am reflecting on the field text which provides the property Text, this
works when done at the form level but not during serialization. In fact it is
more complicated than that. I set up a test program, that inherited a very
simple class from control and found that it picked up the field properly,
using exactly the same class for serialization as I am using in the main
program. The field is certainly there in the main program but comes up as
null, in the test program it comes up properly. All of which leaves me at a
loss as to what is going on.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Tony,

Are you reflecting on the field, or on the property? I don't think that
most controls store the text themselves. Rather, when you call the Text
property, it calls SendMessage with a WM_GETTEXT message to get the text
contents of the control, which is why the text field would be blank.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"tony lock" <to******@discussions.microsoft.com> wrote in message
news:E0**********************************@microsof t.com...
I am writing a simulation program, the main classes inherit from a base
class
which itself inherits from Control. I am using reflection to serialize
these
objects including the fields from control. This works perfectly apart from
the field text in Control which always comes back null. Although the IDE
always shows it having the correct value. Since the IDE presumably uses
reflection to show the value, I am at a loss to know why this one field is
a
problem. Interestingly, the command window also comes back with a null,
when
doing the same operation, but if you do the operation on the original
object
just after creation it is correct. It is worth noting the objects are
placed
in a Collection before serialization. So it is using FieldInfo.GetValue on
the items in the Collection which is failing on the string field.


Nov 17 '05 #3

P: n/a
You can never assume that the field text is properly set. Therefore you
should use the property Text which should always be set correctly.
"tony lock" <to******@discussions.microsoft.com> schrieb im Newsbeitrag
news:18**********************************@microsof t.com...
I am reflecting on the field text which provides the property Text, this
works when done at the form level but not during serialization. In fact it is more complicated than that. I set up a test program, that inherited a very
simple class from control and found that it picked up the field properly,
using exactly the same class for serialization as I am using in the main
program. The field is certainly there in the main program but comes up as
null, in the test program it comes up properly. All of which leaves me at a loss as to what is going on.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Tony,

Are you reflecting on the field, or on the property? I don't think that most controls store the text themselves. Rather, when you call the Text
property, it calls SendMessage with a WM_GETTEXT message to get the text
contents of the control, which is why the text field would be blank.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"tony lock" <to******@discussions.microsoft.com> wrote in message
news:E0**********************************@microsof t.com...
I am writing a simulation program, the main classes inherit from a base
class
which itself inherits from Control. I am using reflection to serialize
these
objects including the fields from control. This works perfectly apart from the field text in Control which always comes back null. Although the IDE always shows it having the correct value. Since the IDE presumably uses reflection to show the value, I am at a loss to know why this one field is a
problem. Interestingly, the command window also comes back with a null, when
doing the same operation, but if you do the operation on the original
object
just after creation it is correct. It is worth noting the objects are
placed
in a Collection before serialization. So it is using FieldInfo.GetValue on the items in the Collection which is failing on the string field.


Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.