Controls Name/ID using reflection

G

Gary Hower

I recently read an article by an MVP Chris Tacke describing the use of
reflection to get the name/ID of a CF control on a windows form.

The Article: http://blog.opennetcf.org/ctacke/PermaLink.aspx/64c28b10-d3a8-4c6b-b11a-2ae2de4bdaf1

I have also read a few articles where people are describing the use of
the code segment Chris provided. There seems to be an issue with the
code example as it relates to Control placed on panels.

Would anyone have a good solution to this problem. It seems the
methods Chris wrote do not account for panels as the parent to other
controls.

Is anyone familiar with this problem or have another solution to
determine the Name/ID of a control thru code.
example:


Chris' method:
--------------

public static string GetControlName(object SourceControl)
{
FieldInfo[] fi = ((Control)SourceControl).Parent.GetType().GetFields(BindingFlags.NonPublic
| BindingFlags.Instance |BindingFlags.Public |
BindingFlags.IgnoreCase);


foreach(FieldInfo f in fi)
{
if(f.GetValue(((Control)SourceControl).Parent).Equals(SourceControl))
return f.Name;
}
return null;
}


What would be great would be if you could use the Object sender from
an event to determine the Name/ID of the control this would allow for
more flexible and smaller code when dealing with lots of Controls.

Any help with this matter would be greatly appreciated.

I would like to Correspond with an MVP on this matter if possible.
 
C

Chris Tacke, eMVP

what comes back for the fieldinfo collection of the Panel (this is what the
first line grabs - the Panel being the control's Parent)?
 
G

Gary Hower

Chris, First thanks for responding to my question and sorry if you
endup getting more than one request from me I was not sure what the
best method to reach you was.


so,

I ran this txtbox.Text = ControlEx.GetControlName(this.panel1);

Here is the data outputted to the imediates window. The above command
does get the panels name "Panel1" but the function does not see the
controls that are on the panel. Such as radiobuttons and textboxes.

System.Reflection.RuntimeFieldInfo}
[System.Reflection.RuntimeFieldInfo]:
{System.Reflection.RuntimeFieldInfo}
System.Reflection.MemberInfo: {System.Reflection.RuntimeFieldInfo}
Attributes: Private
FieldHandle: {System.RuntimeFieldHandle}
FieldType: {"System.Windows.Forms.Panel"}
IsAssembly: false
IsFamily: false
IsFamilyAndAssembly: false
IsFamilyOrAssembly: false
IsInitOnly: false
IsLiteral: false
IsNotSerialized: false
IsPinvokeImpl: false
IsPrivate: true
IsPublic: false
IsSpecialName: false
IsStatic: false
MemberType: Field


When i run this

foreach (Control item in this.Controls)
{
txtbox.Text += ControlEx.GetControlName(item) + ":";
}

I get all the control names except the one that are on the panel
control.



Chris Tacke said:
what comes back for the fieldinfo collection of the Panel (this is what the
first line grabs - the Panel being the control's Parent)?

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


Gary Hower said:
I recently read an article by an MVP Chris Tacke describing the use of
reflection to get the name/ID of a CF control on a windows form.

The Article: http://blog.opennetcf.org/ctacke/PermaLink.aspx/64c28b10-d3a8-4c6b-b11a-2ae2de4bdaf1

I have also read a few articles where people are describing the use of
the code segment Chris provided. There seems to be an issue with the
code example as it relates to Control placed on panels.

Would anyone have a good solution to this problem. It seems the
methods Chris wrote do not account for panels as the parent to other
controls.

Is anyone familiar with this problem or have another solution to
determine the Name/ID of a control thru code.
example:


Chris' method:
--------------

public static string GetControlName(object SourceControl)
{
FieldInfo[] fi = ((Control)SourceControl).Parent.GetType().GetFields(BindingFlags.NonPublic
| BindingFlags.Instance |BindingFlags.Public |
BindingFlags.IgnoreCase);


foreach(FieldInfo f in fi)
{
if(f.GetValue(((Control)SourceControl).Parent).Equals(SourceControl))
return f.Name;
} return null;
}


What would be great would be if you could use the Object sender from
an event to determine the Name/ID of the control this would allow for
more flexible and smaller code when dealing with lots of Controls.

Any help with this matter would be greatly appreciated.

I would like to Correspond with an MVP on this matter if possible.
 
C

Chris Tacke, eMVP

The loop probably needs to be modified to recursively see if the control is
a parent, and if it is, loop through it's children. So when it reaches a
Panel it would then run the same set of code again to get it's children.
The need for recursion is to allow for Panels in Panels and so on.

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


Gary Hower said:
Chris, First thanks for responding to my question and sorry if you
endup getting more than one request from me I was not sure what the
best method to reach you was.


so,

I ran this txtbox.Text = ControlEx.GetControlName(this.panel1);

Here is the data outputted to the imediates window. The above command
does get the panels name "Panel1" but the function does not see the
controls that are on the panel. Such as radiobuttons and textboxes.

System.Reflection.RuntimeFieldInfo}
[System.Reflection.RuntimeFieldInfo]:
{System.Reflection.RuntimeFieldInfo}
System.Reflection.MemberInfo: {System.Reflection.RuntimeFieldInfo}
Attributes: Private
FieldHandle: {System.RuntimeFieldHandle}
FieldType: {"System.Windows.Forms.Panel"}
IsAssembly: false
IsFamily: false
IsFamilyAndAssembly: false
IsFamilyOrAssembly: false
IsInitOnly: false
IsLiteral: false
IsNotSerialized: false
IsPinvokeImpl: false
IsPrivate: true
IsPublic: false
IsSpecialName: false
IsStatic: false
MemberType: Field


When i run this

foreach (Control item in this.Controls)
{
txtbox.Text += ControlEx.GetControlName(item) + ":";
}

I get all the control names except the one that are on the panel
control.



"Chris Tacke, eMVP" <[email protected]> wrote in message
what comes back for the fieldinfo collection of the Panel (this is what the
first line grabs - the Panel being the control's Parent)?

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


Gary Hower said:
I recently read an article by an MVP Chris Tacke describing the use of
reflection to get the name/ID of a CF control on a windows form.

The Article:
http://blog.opennetcf.org/ctacke/PermaLink.aspx/64c28b10-d3a8-4c6b-b11a-2ae2de4bdaf1
I have also read a few articles where people are describing the use of
the code segment Chris provided. There seems to be an issue with the
code example as it relates to Control placed on panels.

Would anyone have a good solution to this problem. It seems the
methods Chris wrote do not account for panels as the parent to other
controls.

Is anyone familiar with this problem or have another solution to
determine the Name/ID of a control thru code.
example:


Chris' method:
--------------

public static string GetControlName(object SourceControl)
{
FieldInfo[] fi =
((Control)SourceControl).Parent.GetType().GetFields(BindingFlags.NonPublic
| BindingFlags.Instance |BindingFlags.Public |
BindingFlags.IgnoreCase);


foreach(FieldInfo f in fi)
{
if(f.GetValue(((Control)SourceControl).Parent).Equals(SourceControl))
return f.Name;
} return null;
}


What would be great would be if you could use the Object sender from
an event to determine the Name/ID of the control this would allow for
more flexible and smaller code when dealing with lots of Controls.

Any help with this matter would be greatly appreciated.

I would like to Correspond with an MVP on this matter if possible.
 
A

Alex Feinman [MVP]

Since pretty much all controls are memebers of the Form-derived class, why
don't you use this:

public static string GetControlName(object SourceControl)
{
FieldInfo[] fi =
((Control)SourceControl).TopLevelControl.GetType().GetFields(
BindingFlags.NonPublic | BindingFlags.Instance |
BindingFlags.Public | BindingFlags.IgnoreCase);

foreach(FieldInfo f in fi)
{

if(f.GetValue(((Control)SourceControl).Parent).Equals(SourceControl))
return f.Name;
}

return null;
}

The difference is in using TopLevelControl instead of Parent

--
Alex Feinman
---
Visit http://www.opennetcf.org
Gary Hower said:
Chris, First thanks for responding to my question and sorry if you
endup getting more than one request from me I was not sure what the
best method to reach you was.


so,

I ran this txtbox.Text = ControlEx.GetControlName(this.panel1);

Here is the data outputted to the imediates window. The above command
does get the panels name "Panel1" but the function does not see the
controls that are on the panel. Such as radiobuttons and textboxes.

System.Reflection.RuntimeFieldInfo}
[System.Reflection.RuntimeFieldInfo]:
{System.Reflection.RuntimeFieldInfo}
System.Reflection.MemberInfo: {System.Reflection.RuntimeFieldInfo}
Attributes: Private
FieldHandle: {System.RuntimeFieldHandle}
FieldType: {"System.Windows.Forms.Panel"}
IsAssembly: false
IsFamily: false
IsFamilyAndAssembly: false
IsFamilyOrAssembly: false
IsInitOnly: false
IsLiteral: false
IsNotSerialized: false
IsPinvokeImpl: false
IsPrivate: true
IsPublic: false
IsSpecialName: false
IsStatic: false
MemberType: Field


When i run this

foreach (Control item in this.Controls)
{
txtbox.Text += ControlEx.GetControlName(item) + ":";
}

I get all the control names except the one that are on the panel
control.



"Chris Tacke, eMVP" <[email protected]> wrote in message
what comes back for the fieldinfo collection of the Panel (this is what the
first line grabs - the Panel being the control's Parent)?

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


Gary Hower said:
I recently read an article by an MVP Chris Tacke describing the use of
reflection to get the name/ID of a CF control on a windows form.

The Article:
http://blog.opennetcf.org/ctacke/PermaLink.aspx/64c28b10-d3a8-4c6b-b11a-2ae2de4bdaf1
I have also read a few articles where people are describing the use of
the code segment Chris provided. There seems to be an issue with the
code example as it relates to Control placed on panels.

Would anyone have a good solution to this problem. It seems the
methods Chris wrote do not account for panels as the parent to other
controls.

Is anyone familiar with this problem or have another solution to
determine the Name/ID of a control thru code.
example:


Chris' method:
--------------

public static string GetControlName(object SourceControl)
{
FieldInfo[] fi =
((Control)SourceControl).Parent.GetType().GetFields(BindingFlags.NonPublic
| BindingFlags.Instance |BindingFlags.Public |
BindingFlags.IgnoreCase);


foreach(FieldInfo f in fi)
{
if(f.GetValue(((Control)SourceControl).Parent).Equals(SourceControl))
return f.Name;
} return null;
}


What would be great would be if you could use the Object sender from
an event to determine the Name/ID of the control this would allow for
more flexible and smaller code when dealing with lots of Controls.

Any help with this matter would be greatly appreciated.

I would like to Correspond with an MVP on this matter if possible.
 
C

Chris Tacke, eMVP

You could do that too, if you wanted to go the easy route. :)

-Chris


Alex Feinman said:
Since pretty much all controls are memebers of the Form-derived class, why
don't you use this:

public static string GetControlName(object SourceControl)
{
FieldInfo[] fi =
((Control)SourceControl).TopLevelControl.GetType().GetFields(
BindingFlags.NonPublic | BindingFlags.Instance |
BindingFlags.Public | BindingFlags.IgnoreCase);

foreach(FieldInfo f in fi)
{

if(f.GetValue(((Control)SourceControl).Parent).Equals(SourceControl))
return f.Name;
}

return null;
}

The difference is in using TopLevelControl instead of Parent

--
Alex Feinman
---
Visit http://www.opennetcf.org
Gary Hower said:
Chris, First thanks for responding to my question and sorry if you
endup getting more than one request from me I was not sure what the
best method to reach you was.


so,

I ran this txtbox.Text = ControlEx.GetControlName(this.panel1);

Here is the data outputted to the imediates window. The above command
does get the panels name "Panel1" but the function does not see the
controls that are on the panel. Such as radiobuttons and textboxes.

System.Reflection.RuntimeFieldInfo}
[System.Reflection.RuntimeFieldInfo]:
{System.Reflection.RuntimeFieldInfo}
System.Reflection.MemberInfo: {System.Reflection.RuntimeFieldInfo}
Attributes: Private
FieldHandle: {System.RuntimeFieldHandle}
FieldType: {"System.Windows.Forms.Panel"}
IsAssembly: false
IsFamily: false
IsFamilyAndAssembly: false
IsFamilyOrAssembly: false
IsInitOnly: false
IsLiteral: false
IsNotSerialized: false
IsPinvokeImpl: false
IsPrivate: true
IsPublic: false
IsSpecialName: false
IsStatic: false
MemberType: Field


When i run this

foreach (Control item in this.Controls)
{
txtbox.Text += ControlEx.GetControlName(item) + ":";
}

I get all the control names except the one that are on the panel
control.



"Chris Tacke, eMVP" <[email protected]> wrote in message
what comes back for the fieldinfo collection of the Panel (this is
what
http://blog.opennetcf.org/ctacke/PermaLink.aspx/64c28b10-d3a8-4c6b-b11a-2ae2de4bdaf1
I have also read a few articles where people are describing the use of
the code segment Chris provided. There seems to be an issue with the
code example as it relates to Control placed on panels.

Would anyone have a good solution to this problem. It seems the
methods Chris wrote do not account for panels as the parent to other
controls.

Is anyone familiar with this problem or have another solution to
determine the Name/ID of a control thru code.
example:


Chris' method:
--------------

public static string GetControlName(object SourceControl)
{
FieldInfo[] fi =
((Control)SourceControl).Parent.GetType().GetFields(BindingFlags.NonPublic
| BindingFlags.Instance |BindingFlags.Public |
BindingFlags.IgnoreCase);


foreach(FieldInfo f in fi)
{
if(f.GetValue(((Control)SourceControl).Parent).Equals(SourceControl))
return f.Name;
}
return null;
}


What would be great would be if you could use the Object sender from
an event to determine the Name/ID of the control this would allow for
more flexible and smaller code when dealing with lots of Controls.

Any help with this matter would be greatly appreciated.

I would like to Correspond with an MVP on this matter if possible.
 

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