string to object name

Hi all,
I recently started a new job and have been thrown in the deep with a project. It was all well and good until it was decided that I would develop the windows software on top of everything else. Long story short, I know a little bit about C++.net but not heaps.

I want to be able to address an object using a variable so I can manipulate many text boxes with a for loop.

For example:
System::String^ AString;
int i = 1;
AString = "textBox" + i; // This bit works fine...

Now AString contains the name of the object I want to manipulate but I don't know how to call it...
this->?AString?->Text = "Hello";

What do I put in place of ?AString? or what do I use to achieve the same goal with different code?

Many thanks in advance.

It depends on your GUI system.

For MFC, I don't know, but there should be an associative array somewhere where you can turn a String into a Component^ or Control^ or something.

For example, Borland's VCL defines a components[] property that you can access to get a pointer to the named object.
 
TLabel* label = (TLabel*)(form->components[ "Label1" ]);

(Granted, I usually do this in Delphi, not C++, but the above snippit demonstrates the idea...)

Look through the component properties of your main form (via your documentation).

Good luck!
Last edited on
if you are talking about mfc..

assosiate CString with a text control
for more manipulations on text box control use CEdit.
to assosiate it with text box do like this:

declare a CEdit/CString type object in .h file,
use
DDX_Control
to assosiate the variable with the text box.

1
2
3
4
5
.h file
CEdit m_textbox;

.cpp file:
DDX_Control(pDX,EDT_1,m_textbox); //EDT_1 is the id of the text box. 


hope this is what you are looking for.
I'll give them a go but I'm using CLR not MFC. Sorry I didn't indicate this earlier but I didn't realise that MFC supports the .net library. If you know how it's definitely done in CLR it would help. I should probably have said I'm using Visual Studio 2005 as well.

Thanks for your help so far.
oh.. no idea regarding CLR..
actually i also misread you.. sorry.. :(
closed account (z05DSL3A)
For CLR you could try somthing like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public ref class Form1 : public System:: Windows:: Forms::Form
{
    // blah blah blah

private: System:: Void someFunc() 
         {
            System::String^ AString;
            int i = 1;
            AString = "textBox" + i;
            Control^ ctrlPtr = FindControl(AString);
            if(ctrlPtr != nullptr)
            {
                ctrlPtr->Text = "SomeText";
            }

         }

private: Control^ FindControl(String^ strControlName)
        {
            if (strControlName->Length == 0 || this->Controls->Find(strControlName, true)->Length == 0)
                return nullptr;

            return this->Controls->Find(strControlName, true)[0];
        }
    
}
I tried the code this morning and it worked and absolute treat. To me that seemed like a lot of code, but that's because I spend most of my time programming microcontrollers and such where efficiency is essential. But windows software design will have none of that.

Thanks a bunch for everyone's help, it made my day today.
Topic archived. No new replies allowed.