Help - Missing control.Name property in compact framework ?

M

Mobile Boy 36

Hello,

I was writing a piece of test-code to translate controls on a form...
It seems that the control.name property is missing.
To confirm my supposition, I tried my code in a normal Net framework windows
application.
There you've got the name property.
Does someone know a workaround or a solution ?

This is my code ( the problem line is 'If ctl.Name = "label1" Then')

Public Shared Function TransLateForm(ByVal LanguageId As String, ByVal
FormName As System.Windows.Forms.Form, ByVal Text As String)

Dim ctl As System.Windows.Forms.Control

Dim ctlType As String

For Each ctl In FormName.Controls

TranslateControl(ctl, LanguageId, Text)

Next

End Function

Private Shared Sub TranslateControl(ByVal ctl As
System.Windows.Forms.Control, ByVal LanguageId As String, ByVal Text As
String)

Dim subctl As System.Windows.Forms.Control

Dim ctlType As String



ctlType = ctl.GetType.ToString



If ctl.Name = "label1" Then

CType(ctl, System.Windows.Forms.Label).Text = "Test"

End If

For Each subctl In ctl.Controls

TranslateControl(subctl, LanguageId, Text)

Next

End Sub
 
A

Alex Yakhnin [MVP]

If all what you're trying to do is assing some value to
Label's text on your form, instead of verifying a name,
you can check if a control is a Label type.
 
B

Brian Garcia

Still, this is a problem I have run into too and there's got to be a way
around it. I have a series of 12+ controls that I'm trying to fill from
a DataReader. Here's what I'm trying to do:

Dim i as integer
For i = 1 to 6
GetControlByName("txtResult" & i).text = dr.item(0)
GetControlByName("txtUnits" & i).text = dr.item(1)
...lot's other similar code not worth posting
next

..and I'm using a function I found posted somewhere that I don't fully
understand because I'm new to .NET. That and the fact that it's not
working doesn't help:

Private Function GetControlByName(ByVal name As String) As Control
Return DirectCast(Me.GetType().GetField(name,
BindingFlags.NonPublic Or BindingFlags.Public Or BindingFlags.Instance
Or BindingFlags.IgnoreCase).GetValue(Me), Control)

The only thing I know about this function is that it requires the import
of System.Reflection

When I call this, it returns a NullReferenceException. Any help is
GREATLY appreciated, including deciphering the above return statement.
 
C

Chris Tacke, eMVP

This code uses reflection, but it assumes that the Name field exists. I'm
not sure it exists, even as a hidden field, so it makes sense that you'd get
a null reference exception. Is the snippet from a CF project, or desktop?
 
B

Brian Garcia

Scratch that. After searching through some of the other posts, I came
to the realization that the only thing I was missing was the "_"
preceding the control name in my function calls (should have been
GetControlByName("_cmbResult1"). For some reason I was under the
impression that the underscore was only necessary in C#. It works fine
now.

BTW...it is a CF project but it doesn't use the name propert, it just
passes the name as a string to the function. From there, I'm not sure
how the whole reflection thing works but as long as it works, I'm
satisfied.
 

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