Newsgroup: it.comp.lang.visual-basic
Titolo: Using text as a control name --- Help, Please!!!

Help!
I need to know how to use text as a control name.
Here's the example:
    I have a about 50 pictureboxes on a form that make up a configurable
control panel.  These are on a control array called Panel1.
    I also have off to the side of the form, each of the modules that can be
dropped into the panel.  Each of these has a unique control name that just
happens to be the manufacturer's model number of the module (but that
doesn't really matter here)
    In each of the model numbers (with unique names) and each of the control
array indexes there is a TAG that is the same as the model number controls.
Here is my problem.  I want the user to be able to save the configuration of
the control panel GUI I have made.  I have in place a system that uses an
.ini file to go through all the controls on the form then save the pertinent
data to the .ini file.  It may not be the most efficient way, but it works
and the application is too big to change all the other forms over to
something new now.  Here lies my problem.  For the control array, I would
simply record the TAG information to the .ini file.  That's easy.  Reading
it back and changing all the control arrays is not.
What I need to so is read the INI file line for the control index, let's say
it's  Panel1(100).  Let's then say the text in the .ini file is   XYZ123.
There is always another control on the form that will match the TAG
information originally saved to the INI file, in this case   XYZ123.
How do I say to Visual Basic:    "Take the value of this text item (XYZ123),
then access the .picture property of the control of the same name?"
dBASE III used to have a function called Macro Substitution which did just
this.  You could take any kind of text and by putting an "&" in front of it,
it would treat it like you were referring to a control.
Can I do this in VB6?  I really need it.
Thanks for the help and sorry for the long winded explanation.
If you'd like to converse privately, please e-mail to  (be sure to take
everything out in brackets to prevent spam):
rothwell[removethis]s@[removethis]
attbi.com

 I need to know how to use text as a control name.
...snip...
I don't think of a way to do it directly in VB as you want just now.  A
workaround if the possible controls are a static collection is to select
via a Case or other logic--ugly, but it should work (I think)...if they
somehow aren't static controls, then you have a real problem,
methinks...



Sorry but I'm really confused at your question(s)... :-<
If each PictureBox has a unique name then you can use the .Name property to compare what is saved in the .INI file.
(bad programming practice doing it this way because if a Model is changed/added/deleted you have to up date the program)
If how ever the PictureBox is a control array then all you'd need to do is save the Index + "What Ever Else" to the .INI file...
Sort of like this:
sSaveData = CStr(Index) & "~" & "What Ever Else"... I used the tilde "~" here because its not used to much...
Then when reading in the .INI file do something like:
Dim sPointer As String
'Read INI line into sReadData here
sPointer = Split(sReadData,"~")
Now sPointer(0) holds the saved Index and sPointer(1) holds "What Ever Else"
Then you can do like:::
PictureBox(CLng(sPointer(0)).Tag = sPointer(1).... 
blah... blah... blah...
Kind of a hack but it's just food for thought, more or less...
Have a good day...
Don

 wrote in message
news:r2kuevgnrt8713tdn...@4ax.com...




Ok, I think I understand what you mean now... 
I take it that it's to late in the developement cycle to change the FIXED controls into a control array.
One alternative might be something like the following air code:
Dim cntTest as Control
For Each cntTest In Form.Controls
If cntTest.Name = sNameStringFromINIFile then
   PictureBox(CLng(IndexFromINIFile)).Picture = cntTestName.Image
   Exit For
End If
Next cntTest
hmmmmm.... What ya thunk?? LOL


 wrote in message
news:gvquev8jtrl60mpqr...@4ax.com...


As noted before, you're trying to simulate the equivalent to a pointer
derefernce--VB doesn't do that.  I think the only possible way w/o
translating the static controls to an array is to have a logic test
where the static control names are explicitly contained in the
structure--ugly, and if (as noted previously) they aren't truly static,
you're hosed...


 wrote:
Ok. Here is a little display (project) that will show you how this works.
Start a new project
Add 5 to 10 labels
1 TextBox
1 Command Button
Run the project 
Type in a label name, ie: label3
Click the command buttton and that labels backcolor will change to red.
Option Explicit
Private Sub Command1_Click()
Dim cntTest As Control
    On Error Resume Next 'Just incase a control has no BackColor property
    For Each cntTest In Me.Controls 'Loop through ALL controls on the form
        If UCase(cntTest.Name) = UCase(Text1.Text) Then 'Is this the one???
'Text1.Text is a subsitute for the Control Name Saved to the INI File
            cntTest.BackColor = vbRed 'If a matched name then change color
'You would use YourPictureBox(IndexFromINIFILE).Picture = cntTest.Image here
            Exit For 'Get out while the getting is good
        End If
    Next cntTest
End Sub
<zaaaaapppp>


 wrote in message
news:ut4vevo1vgcg2nngv...@4ax.com...


Pointers in VB would be entirely contrary to the basic (so to speak :) )
nature of BASIC.  The purpose of Basic was to separate such low level
manipulation from the programmer.  Unfortunately, that does preclude
some forms of implementation, but that's why there are different
programming languages--


 I need to know how to use text as a control name.
...
I got a bit lost in your description -- at one point I thought you were
saying that you had two controls with the same Tag.  But what I *think* you
meant is that you have a String (a model number) that exists in a Tag of
some control and is also the actual control name of another control.  You
want to be able to manipulate the control using the text name -- not an
actual reference to the control.
This can be done easily thanks to the fact that VB forms are COM objects
that implement IDispatch ("automatable COM objects").
VB contains a function called CallByName().  All this really does is invoke
IDispatch for you.
For a demo, create a Form with two PictureBoxes.  Put a bitmap of some sort
(your favorite JPEG) as the Picture for Picture1.  Leave Picture2 empty.
Create a command button and put the following code in its click handler:
    Picture2.Picture = CallByName(Me, "Picture1", VbGet).Picture
Run the demo.  Press the button.  Yay, the picture ends up in the second
picture box.
Let's take a quick look at the code (a long look would be difficult since
it's just one line)...
Here's the call to CallByName:
CallByName(Me, "Picture1", VbGet)
What it's doing is accessing the "Picture1" property on the object Me (in
this case, that's Form1 -- where Picture1 is).  VbGet tells CallByName that
Picture1 is a property and we want to GET the value (this could also be Let
or Set for property assignments).  This returns a Variant holding the
Picture1 object, which we then get the Picture property of and assign to
Picture2's Picture property.
It's just that easy.
As for VB supporting pointers in the future -- well, no.  You have to
remember that Visual Basic is completely dead, having been replaced with
Visual Basic.Net (or, as I like to call it, C#--), which is an entirely
different animal.
Murphy
www.constantthought.com