a while ago Remy posted me a sample how to create 20 dynamic TEdit's..
my problem is that i've got a ComboBox, that gives the user the option to "feed" the dynamicaly created TEdit's with several values...BUT how do i access the TEdit's ?? Their names are created in the constructor.. and i need to access them from a TButton->OnClick(...) and here they are unknown, so how is it possible?
Oren Halvani wrote: > my problem is that i've got a ComboBox, that gives the user the option > to "feed" the dynamicaly created TEdit's with several values...BUT how > do i access the TEdit's ?? Their names are created in the constructor.. > and i need to access them from a TButton->OnClick(...) and here they > are unknown, so how is it possible?
You pushed all your TEdit's in -which you forgot to mention-:
std::Vector<TEdit*> Edits;
So simply loop through Edits looking for the wanted Name.
> You need to pay more attention to the code samples I give you. I already > showed you how to access them.
> Gambit
OK, i could do it like that:
void ins(TEdit *one) { one->Text = "ä";
}
// But..
void __fastcall TfrmMulti_StringReplace::cmdInsert_SampleClick(TObject *Sender) { std::for_each(Edits.begin(), Edits.end(), ins); // I DO NOT NEED ALL EDIT'S..only 7
First off, you can just do a vector.at( someint ) and get just the one you need.
Also, you might want to consider a map if you need to access certain "random" TEdits. Like mapping an identifier that you make up like some integer to keep track of them.
map< int, TEdit* > someMap;
Then when you put them in your map simliar to this
someMap[ 1 ] = thisTEdit;
You can always get back top that specific one.
Hope that helps..
cheers,
tim
p.s. using the overloaded index [] operator inserts a new item at that position if it doesn't exist. If that's not the behavior you are seeking, use find instead.
> void __fastcall TfrmMulti_StringReplace::cmdInsert_SampleClick(TObject *Sender) > { > std::for_each(Edits.begin(), Edits.end(), ins); // I DO NOT NEED ALL > EDIT'S..only 7
That is not what I was referring to. I already showed you how to access both Left and Right edits given a particular TEdit pointer. How you get that pointer to begin with is up to you.
> std::for_each(Edits.begin(), Edits.end(), ins); // I DO > NOT NEED ALL EDIT'S..only 7
Then you need to explain - IN DETAIL - exactly what you really want to do.
> > OK, i could do it like that: > That is not what I was referring to. I already showed you how to access > both Left and Right edits given a particular TEdit pointer. How you get > that pointer to begin with is up to you.
but i cannot use pointers.. i need to access them by their names for example:
> > std::for_each(Edits.begin(), Edits.end(), ins); // I DO > > NOT NEED ALL EDIT'S..only 7 > Then you need to explain - IN DETAIL - exactly what you really want to do.
dear Remy,
i thought i've done this in the first post...anyway..i've got a ComboBox, that gives the user the option to "feed" the dynamicaly created TEdit's with several values...
BUT how do i access the TEdit's ? Their names are created in the CONSTRUCTOR.. and i need to access them from a TButton->OnClick(...) Event..and of course here they are unknown...the names that i'm creating here:
__fastcall Form1::Form1(TComponent* Owner) : TForm(Owner) { const int b = 20; const int width = 110; const int height = 21;
Edits.clear(); int anzahl = 20;
for(int x = 0; x < anzahl; ++x) { TEdit *LeftEdit = new TEdit(this); LeftEdit->Parent = this; LeftEdit->Name = "txtLeft" + IntToStr(x); // NOT GLOBAL... //.....
are NOT global, so i cannot access them from Button1->Click(...)
Yes, you can. Simply look up the pointer that has the name you are looking for. You need the pointer in order to actually update the Text. A pointer is a pointer regardless of where you get it from.
> i need to access them by their names for example:
You are thinking of the auto-generated pointers that the IDE creates for you. You are limiting your thinking too much.
Try this code:
std::vector<TEdit*> Edits;
TEdit* __fastcall FindEditByName(const AnsiString &Name) { std::vector<TEdit*>::iterator iter = Edits.begin(); while( iter != Edits.end() ) { if( (*iter)->Name == Name ) return *iter; } return NULL; }