Newbie: get object (combobox) from name string? (syntax help please)

K

K Ruck

I load a bunch of text files at form_load. The first line in each text file
is a string which is the name of the combobox control ("cmb1", "cmb2", etc)
I want to populate, and each subsequent string to the end of the file are
the strings I want to populate that specific combobox with.

I load all the files into a 2-dimensional array (below), from which I want
to populate the comboboxes.

cmb1 cmb2 cmb3
green apple sour
red pear sweet
yellow peach tart

I want to be able to reference the object by the string name, e.g. something
like:
Me.control("cmb1").additem

but if that isn't possible, I guess there must be a way to cycle/loop
through all the controls (yuck) and find the one with the right name. I
tried adapting the following, but it was not from a compact framework group,
and I have been unsuccessful at adapting it to my needs:

ttp://groups.google.de/groups?selm=%23kGYk5IgDHA.164%40tk2msftngp13.phx.gbl>
(note the last bracket '>' is required, but not part of the link for some
reason)

I also found:
http://www.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=u48XpTttCHA
..2492%40TK2MSFTNGP10&rnum=12&prev=/groups%3Fq%3D%2Bcontrol%2BOR%2Bcombobox%2
Bgroup:microsoft.public.dotnet%26start%3D10%26hl%3Den%26lr%3D%26ie%3DUTF-8%2
6oe%3DUTF-8%26selm%3Du48XpTttCHA.2492%2540TK2MSFTNGP10%26rnum%3D12
but the first response (which seemed the most relevant) is still not
decipherable to me

Any and all help would be greatly appreciated!
Keith
 
A

Alex Feinman [MVP]

I don't think the post to which you refer is relevant. Keep in mind that in
CF the control Name property does not exists, i.e. control's name is not
available in the runtime.

Imports System.Reflection
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)

End Function

GetControlByName("_TextBox1").Text = "42"

DirectCast(GetControlByName("_ComboBox1"), ComboBox).Items.Add("42")

Make sure to add the underscore to the control name when you address it (not
needed in C#)
 

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

Similar Threads


Top