Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Still struggling: select control by name or _any_ workable alternative?

8 views
Skip to first unread message

confused_newbie

unread,
Nov 8, 2003, 2:46:52 PM11/8/03
to
Ok, I get that CF doesn't support control("name") to select and act on a
control.

I have text files that contain text I need to add to my comboboxes when the
program loads. The first line in each text file is the name of the combobox;
the rest of that text file contains the strings that need to be added
[.items.add] to that combobox.

I've tried a variety of suggestions posted in this ng, and have searched the
web and MS's FAQ as well- but I can't get anything to work.

If anyone has an easy suggestion, please let me know. Alternatively, I'm
thinking I have to cycle through the form and get the names of all the
comboboxes, and put the names of the comboboxes as indexes in a hashtable
along with the comboboxes themselves(?). However, I can't seem to find any
way to cycle through all the controls and get their name properties to start
with, e.g.

Sub GetNames
Dim MyCmb as Combobox
For Each MyCmb in Me.Controls
myHT.add(MyCmb.name, MyCmb) 'still relies on the name property-
doesn't work
Next
End Sub

another option I've tried is below, but that keeps throwing exceptions...
(watch for wrap)

Private function getcontrolbyname(byval name as string) as combobox
return directcast(me.gettype().getfield(name,bindingflags.nonpublic or
Bindingflags.public or bindingflags.instance or
bindingflags.ignorecase).getvalue(me),combobox)
end function

'paired with

Sub Testme
Dim tmpcontrol as combobox
tmpcontrol = getcontrolbyname("cmb01")
tmpcontrol.items.add ("testing")
end sub
'the sub & function above throw a managed nullreferenceexception at Frm1
getcontrolbyname+0x15

TIA !!

Jonathan Wells [msft]

unread,
Nov 8, 2003, 4:17:22 PM11/8/03
to
Hi there, it looks like you almost got it with your second approach. Note
that you need to prepend a "_" to your control's name in VB, as Alex points
out in this post:
http://groups.google.com/groups?q=compactframework+getcontrolbyname&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=%23ho0EZrkDHA.4008%40TK2MSFTNGP11.phx.gbl&rnum=1

Here is some code that I just tested:
'VB
Private Function ControlFromName(ByVal name As String) As Control
Dim o As Object
o = Me.GetType().GetField(name, Reflection.BindingFlags.NonPublic Or _
Reflection.BindingFlags.Instance Or _
Reflection.BindingFlags.IgnoreCase).GetValue(Me)

Return (CType(o, Control))
End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim c As ComboBox
c = CType(ControlFromName("_combobox1"), ComboBox)
c.Items.Add("1")
Me.GetControls()
End Sub

Private Sub GetControls()
Dim fis As System.Reflection.FieldInfo()

fis = Me.GetType().GetFields(Reflection.BindingFlags.NonPublic Or _
Reflection.BindingFlags.Instance Or _
Reflection.BindingFlags.IgnoreCase)

For Each fi As Reflection.FieldInfo In fis
If TypeOf (fi.GetValue(Me)) Is Control Then
MessageBox.Show(fi.Name)
End If
Next
End Sub

//C#
private void Form1_Load(object sender, System.EventArgs e) {
ComboBox c = (ComboBox)this.ControlFromName( "combobox1" );
c.Items.Add( "1" );
this.GetControls();
}

private Control ControlFromName( string name ) {
object o = this.GetType().GetField( name,
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance |

System.Reflection.BindingFlags.IgnoreCase ).GetValue( this );

return( (Control)o );
}

private void GetControls() {
System.Reflection.FieldInfo[] fis = this.GetType().GetFields(
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.IgnoreCase );

foreach( System.Reflection.FieldInfo fi in fis ) {
if( fi.GetValue(this) is Control ){
MessageBox.Show( fi.Name );
}
}
}

cheers jonathan

--
Jonathan Wells
Product Manager
.NET Compact Framework

Check out the .NET Compact Framework FAQ at:
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.aspx

This posting is provided "AS IS" with no warranties, and confers no rights.

"confused_newbie" <nos...@columbus.rr.com> wrote in message
news:%238v1rAj...@TK2MSFTNGP09.phx.gbl...

0 new messages