Here I am creating the elements:

ListBox1->Items->Add(ned1->Text); 

I give them handlers

 for(int i(0);i<ListBox1->Count;i++) { ListBox1->ItemByIndex(i)->OnClick = ListBoxItem1Click; } 

And also created a handler.

 void __fastcall TTabbedForm::TabItemClick(TObject *Sender) { ListBox2->Items->SaveToFile("/sdcard/"+(ListBox1->Items->Strings[ListBox1->ItemIndex])+".txt"); } 

The program works like this: when you first click on any of the created elements (TListBox), an error (photo) crashes. enter image description here

All other clicks on the same or any other elements occur what is in the handler (i.e., it works properly). what is the problem. It seems to me that I use the index of the selected item in its own processor. If so, how to make it work?

  • and what should come running in sender? line? - KoVadim
  • To be honest, I do not know what these arguments mean. - Max
  • 2
    Sender is a pointer to the object that triggered the event. Since, logically, this event should call a line in the list, it should be there. But the problem is that the lines in the list are just lines, not objects. But I think this code will help you. void __fastcall TForm1::ListBox1Click(TObject *Sender) { Edit1->Text = ListBox1->Items->Strings[ListBox1->ItemIndex]; } void __fastcall TForm1::ListBox1Click(TObject *Sender) { Edit1->Text = ListBox1->Items->Strings[ListBox1->ItemIndex]; } - KoVadim

1 answer 1

I understand that you are creating a Multi-Device Application based on a Tabbed template. In this case, the function of the form TTabbedForm::TabItemClick(TObject *Sender) assigned to handle the OnClick event for a specific tab:

tabbed

To write a universal handler for all tabs, you need:

  1. Assign the OnClick event OnClick to the mentioned function for all tabs:

    Onclick

  2. In the function, the Sender argument is processed as a pointer to a TTabItem . For example:

     TTabItem* item = static_cast<TTabItem*>(Sender); 

    item in this case will point to a copy of the tab that was clicked. Actions with it will affect only the selected tab.

ListBox1 mentioned by you is not relevant. And if you need to handle a click on the elements of a TListBox on the form, then the OnItemClick handler OnItemClick have a completely different signature:

 TTabbedForm::ListBox1ItemClick(TCustomListBox * const Sender, TListBoxItem * const Item) 

There is already a pointer to an element of the list is passed explicitly.

  • Here I need exactly the processing of TListBox elements. I did like this. for (int i (0); i <ListBox1-> Count; i ++) {ListBox1-> ItemByIndex (i) -> OnClick = ListBoxItem1Click; } And here is the handler. void __fastcall TTabbedForm :: ListBoxItem1Click (TObject * Sender) {istBox2-> Items-> SaveToFile ("/ sdcard /" + (ListBox1-> Items-> Strings [ListBox1-> ItemIndex]) + ". txt"); } But as I understand it, you need to write something else in the arguments - Max.
  • @Max add the actual information to the question by editing it. Comments are not intended for this. - 伪位蔚蠂慰位蠀蟿
  • OK, I'll do it now - Max