Issue handling TComponent objects (TEdits, TLabels, …) from a TForm object, saved in an unordered_map (member of my own class)

huangapple go评论119阅读模式
英文:

Issue handling TComponent objects (TEdits, TLabels, ...) from a TForm object, saved in an unordered_map (member of my own class)

问题

我正在尝试从保存在std::unordered_map中的TForm对象中处理TComponent对象(例如TEdit、TLabel等)。但是当我尝试处理它们时,一些奇怪的事情发生了。

Test.h

  1. //---------------------------------------------------------------------------
  2. #ifndef TestH
  3. #define TestH
  4. //---------------------------------------------------------------------------
  5. #include <System.Classes.hpp>
  6. #include <Vcl.Controls.hpp>
  7. #include <Vcl.StdCtrls.hpp>
  8. #include <Vcl.Forms.hpp>
  9. //---------------------------------------------------------------------------
  10. #include "Container.h"
  11. #include "Handler.h"
  12. //---------------------------------------------------------------------------
  13. class TUMap_Test : public TForm
  14. {
  15. __published: // IDE-managed Components
  16. TEdit *Edit1;
  17. TEdit *Edit2;
  18. TEdit *Edit3;
  19. TEdit *Edit4;
  20. TLabel *Label1;
  21. TLabel *Label2;
  22. TLabel *Label3;
  23. TLabel *Label4;
  24. TButton *Button1;
  25. void __fastcall Button1Click(TObject *Sender);
  26. private: // User declarations
  27. Container Cont;
  28. public: // User declarations
  29. __fastcall TUMap_Test(TComponent* Owner);
  30. };
  31. //---------------------------------------------------------------------------
  32. extern PACKAGE TUMap_Test *UMap_Test;
  33. //---------------------------------------------------------------------------
  34. #endif

我创建了一个名为Container的类来打包这些TComponent对象,如下所示:

Container.h

  1. #ifndef CONTAINERH
  2. #define CONTAINERH
  3. #include <vcl.h>
  4. #include <unordered_map>
  5. #include <map>
  6. class Container{
  7. private:
  8. TForm * m_Form;
  9. std::unordered_map<String, TEdit*> m_Edits;
  10. std::unordered_map<String, TLabel*> m_Labels;
  11. void packEdits();
  12. void packLabels();
  13. public:
  14. Container(){}
  15. void packComponents(TForm * Form);
  16. std::unordered_map<String, TEdit*> * getEdits();
  17. std::unordered_map<String, TLabel*> * getLabels();
  18. };
  19. #endif

Container.cpp

  1. #include "Container.h"
  2. void Container::packComponents(TForm * Form){
  3. m_Form = Form;
  4. packEdits();
  5. packLabels();
  6. }
  7. void Container::packEdits(){
  8. String Edits[4] = {"Edit1", "Edit2", "Edit3", "Edit4"};
  9. for (int i = 0; i < 4; i++) {
  10. m_Edits.insert(std::make_pair(Edits[i], dynamic_cast<TEdit*>(m_Form->FindComponent(Edits[i]))));
  11. }
  12. }
  13. void Container::packLabels(){
  14. String Labels[4] = {"Edit1", "Edit2", "Edit3", "Edit4"};
  15. for (int i = 0; i < 4; i++) {
  16. m_Labels.insert(std::make_pair(Labels[i], dynamic_cast<TLabel*>(m_Form->FindComponent(Labels[i]))));
  17. }
  18. }
  19. std::unordered_map<String, TEdit*> * Container::getEdits(){
  20. return &m_Edits;
  21. }
  22. std::unordered_map<String, TLabel*> * Container::getLabels(){
  23. return &m_Labels;
  24. }

然后,我在我的TForm对象中创建了一个名为Cont的Container对象。为了以后打包这些TComponent对象,我必须使用TForm对象作为packComponents()方法的参数(packComponents(TForm * Form))。然后,我使用Form->FindComponent()将我的组件插入std::unordered_map。

我创建了一个处理我的打包在std::unordered_map中的TComponent对象的处理函数:

Handler.h

  1. #pragma once
  2. #include "Container.h"
  3. #include <vcl.h>
  4. void TextEditsToLabels(Container * Cont);

Handler.cpp

  1. #include "Handler.h"
  2. void TextEditsToLabels(Container * Cont){
  3. //将标签的标题更改为编辑框的文本
  4. (*Cont->getLabels())["Label1"]->Caption = (*Cont->getEdits())["Edit1"]->Text;
  5. (*Cont->getLabels())["Label2"]->Caption = (*Cont->getEdits())["Edit2"]->Text;
  6. (*Cont->getLabels())["Label3"]->Caption = (*Cont->getEdits())["Edit3"]->Text;
  7. (*Cont->getLabels())["Label4"]->Caption = (*Cont->getEdits())["Edit4"]->Text;
  8. }

最后,在Test.cpp中,我调用了Cont的packer()函数,然后在Button1上创建了一个事件函数来调用TextEditsToLabels()函数。

Test.cpp

  1. #include <vcl.h>
  2. #pragma hdrstop
  3. #include "Test.h"
  4. #pragma package(smart_init)
  5. #pragma resource "*.dfm"
  6. TUMap_Test *UMap_Test;
  7. fastcall TUMap_Test::TUMap_Test(TComponent* Owner): TForm(Owner){
  8. Cont.packComponents(this);
  9. }
  10. void __fastcall TUMap_Test::Button1Click(TObject *Sender){
  11. TextEditsToLabels(&Cont);
  12. }

问题是,当我点击Button1时,什么都没有发生。所以我在TextEditsToLabels()函数中设置了断点,以查看发生了什么,并创建了四个变量来获取这些TEdit的Text。这些新变量显示了NULL值。

  1. #include "Handler.h"
  2. void TextEditsToLabels(Container * Cont){
  3. String Text1 = (*Cont->getEdits())["Edit1"]->Text;
  4. String Text2 = (*Cont->getEdits())["Edit2"]->Text;
  5. String Text3 = (*Cont->getEdits())["Edit3"]->Text;
  6. String Text4 = (*Cont->getEdits())["Edit4"]->Text;
  7. //BREAKPOINT
  8. //将标签的标题更改为编辑框的文本
  9. //(*Cont->getLabels())["Label1"]->Caption = (*Cont->getEdits())["Edit1"]->Text;
  10. //(*Cont->getLabels())["Label2"]->Caption = (*Cont->getEdits())["Edit2"]->Text;
  11. //(*Cont->getLabels())["Label3"]->Caption = (*Cont->getEdits())["Edit3"]->Text;
  12. //(*Cont->getLabels())["Label4"]->Caption = (*Cont->getEdits())["Edit4"]->Text;
  13. }

注意:在点击Button1之前,我在我的TEdit中放入了一些随机值,期望在我的四个新变量中看到它们。

我尝试将容器类型从std::unordered_map更改为std::map,然后问题部分解决了:

  1. std::map<String, TEdit*> m_Edits;

我说部分解决了,因为我需要使用std::unordered_map以获得更好的性能,因为我以后会打包大量TComponent,如果使用std::map,性能将更差。

英文:

I'm trying to handle TComponent objects (TEdit, TLabel, ...) from my TForm object which are saved in an std::unordered_map. But something weird happens to the pair values when I try to handle them.

Test.h

  1. //---------------------------------------------------------------------------
  2. #ifndef TestH
  3. #define TestH
  4. //---------------------------------------------------------------------------
  5. #include &lt;System.Classes.hpp&gt;
  6. #include &lt;Vcl.Controls.hpp&gt;
  7. #include &lt;Vcl.StdCtrls.hpp&gt;
  8. #include &lt;Vcl.Forms.hpp&gt;
  9. //---------------------------------------------------------------------------
  10. #include &quot;Container.h&quot;
  11. #include &quot;Handler.h&quot;
  12. //---------------------------------------------------------------------------
  13. class TUMap_Test : public TForm
  14. {
  15. __published: // IDE-managed Components
  16. TEdit *Edit1;
  17. TEdit *Edit2;
  18. TEdit *Edit3;
  19. TEdit *Edit4;
  20. TLabel *Label1;
  21. TLabel *Label2;
  22. TLabel *Label3;
  23. TLabel *Label4;
  24. TButton *Button1;
  25. void __fastcall Button1Click(TObject *Sender);
  26. private: // User declarations
  27. Container Cont;
  28. public: // User declarations
  29. __fastcall TUMap_Test(TComponent* Owner);
  30. };
  31. //---------------------------------------------------------------------------
  32. extern PACKAGE TUMap_Test *UMap_Test;
  33. //---------------------------------------------------------------------------
  34. #endif

I made a Container class to pack those TComponent objects as follows:

Container.h

  1. #ifndef CONTAINERH
  2. #define CONTAINERH
  3. #include &lt;vcl.h&gt;
  4. #include &lt;unordered_map&gt;
  5. #include &lt;map&gt;
  6. class Container{
  7. private:
  8. TForm * m_Form;
  9. std::unordered_map&lt;String, TEdit*&gt; m_Edits;
  10. std::unordered_map&lt;String, TLabel*&gt; m_Labels;
  11. void packEdits();
  12. void packLabels();
  13. public:
  14. Container(){}
  15. void packComponents(TForm * Form);
  16. std::unordered_map&lt;String, TEdit*&gt; * getEdits();
  17. std::unordered_map&lt;String, TLabel*&gt; * getLabels();
  18. };
  19. #endif

Container.cpp

  1. #include &quot;Container.h&quot;
  2. void Container::packComponents(TForm * Form){
  3. m_Form = Form;
  4. packEdits();
  5. packLabels();
  6. }
  7. void Container::packEdits(){
  8. String Edits[4] = {&quot;Edit1&quot;, &quot;Edit2&quot;, &quot;Edit3&quot;, &quot;Edit4&quot;};
  9. for (int i = 0; i &lt; 4; i++) {
  10. m_Edits.insert(std::make_pair(Edits[i], dynamic_cast&lt;TEdit*&gt;(m_Form-&gt;FindComponent(Edits[i]))));
  11. }
  12. }
  13. void Container:: packLabels(){
  14. String Labels[4] = {&quot;Edit1&quot;, &quot;Edit2&quot;, &quot;Edit3&quot;, &quot;Edit4&quot;};
  15. for (int i = 0; i &lt; 4; i++) {
  16. m_Labels.insert(std::make_pair(Labels[i], dynamic_cast&lt;TLabel*&gt;(m_Form-&gt;FindComponent(Labels[i]))));
  17. }
  18. }
  19. std::unordered_map&lt;String, TEdit*&gt; * Container::getEdits(){
  20. return &amp;m_Edits;
  21. }
  22. std::unordered_map&lt;String, TLabel*&gt; * Container::getLabels(){
  23. return &amp;m_Labels;
  24. }

Then I create a Container object named Cont in my TForm object. In order to pack those TComponent objects later, I had to use the TForm object as an argument of the packComponents() method (packComponents(TForm * Form)). Then I used Form-&gt;FindComponent() to insert my components into a std::unordered_map.

I made a handler function to handle my TComponent objects that are packed in a std::unordered_map:

Handler.h

  1. #pragma once
  2. #include &quot;Container.h&quot;
  3. #include &lt;vcl.h&gt;
  4. void TextEditsToLabels(Container * Cont);

Handler.cpp

  1. #include &quot;Handler.h&quot;
  2. void TextEditsToLabels(Container * Cont){
  3. //Change the Caption of Labels to Edits Text
  4. (*Cont-&gt;getLabels())[&quot;Label1&quot;]-&gt;Caption = (*Cont-&gt;getEdits())[&quot;Edit1&quot;]-&gt;Text;
  5. (*Cont-&gt;getLabels())[&quot;Label2&quot;]-&gt;Caption = (*Cont-&gt;getEdits())[&quot;Edit2&quot;]-&gt;Text;
  6. (*Cont-&gt;getLabels())[&quot;Label3&quot;]-&gt;Caption = (*Cont-&gt;getEdits())[&quot;Edit3&quot;]-&gt;Text;
  7. (*Cont-&gt;getLabels())[&quot;Label4&quot;]-&gt;Caption = (*Cont-&gt;getEdits())[&quot;Edit4&quot;]-&gt;Text;
  8. }

Finally, in Test.cpp, I called the packer() function of my Container object, and then I made an event function on Button1 to call the TextEditsToLabels() function.

Test.cpp

  1. #include &lt;vcl.h&gt;
  2. #pragma hdrstop
  3. #include &quot;Test.h&quot;
  4. #pragma package(smart_init)
  5. #pragma resource &quot;*.dfm&quot;
  6. TUMap_Test *UMap_Test;
  7. fastcall TUMap_Test::TUMap_Test(TComponent* Owner): TForm(Owner){
  8. Cont.packComponents(this);
  9. }
  10. void __fastcall TUMap_Test::Button1Click(TObject *Sender){
  11. TextEditsToLabels(&amp;Cont);
  12. }

Issue handling TComponent objects (TEdits, TLabels, …) from a TForm object, saved in an unordered_map (member of my own class)

The problem is, when I click on Button1, nothing happens. So I put a breakpoint in my TextEditsToLabels() function to see what is happening, and made four variables to get the Text inside of those TEdits. Those new variables show me NULL values:

  1. #include &quot;Handler.h&quot;
  2. void TextEditsToLabels(Container * Cont){
  3. String Text1 = (*Cont-&gt;getEdits())[&quot;Edit1&quot;]-&gt;Text;
  4. String Text2 = (*Cont-&gt;getEdits())[&quot;Edit2&quot;]-&gt;Text;
  5. String Text3 = (*Cont-&gt;getEdits())[&quot;Edit3&quot;]-&gt;Text;
  6. String Text4 = (*Cont-&gt;getEdits())[&quot;Edit4&quot;]-&gt;Text;
  7. //BREAKPOINT
  8. //Change the Caption of Labels to Edits Text
  9. //(*Cont-&gt;getLabels())[&quot;Label1&quot;]-&gt;Caption = (*Cont-&gt;getEdits())[&quot;Edit1&quot;]-&gt;Text;
  10. //(*Cont-&gt;getLabels())[&quot;Label2&quot;]-&gt;Caption = (*Cont-&gt;getEdits())[&quot;Edit2&quot;]-&gt;Text;
  11. //(*Cont-&gt;getLabels())[&quot;Label3&quot;]-&gt;Caption = (*Cont-&gt;getEdits())[&quot;Edit3&quot;]-&gt;Text;
  12. //(*Cont-&gt;getLabels())[&quot;Label4&quot;]-&gt;Caption = (*Cont-&gt;getEdits())[&quot;Edit4&quot;]-&gt;Text;
  13. }

Issue handling TComponent objects (TEdits, TLabels, …) from a TForm object, saved in an unordered_map (member of my own class)

Note: Before clicking on Button1, I put some random values in my TEdit, expecting to see them in my four new variables:

I tried to change the container type from std::unordered_map to std::map, and then the problem was partially solved:

std::map&lt;String, TEdit*&gt; m_Edits;

Issue handling TComponent objects (TEdits, TLabels, …) from a TForm object, saved in an unordered_map (member of my own class)

I say partially solved because I need to use std::unordered_map to have better performance, as I will pack lots of TComponents later, so it will perform worst if I use std::map.

答案1

得分: 1

packLabels() 正在搜索 TEdit 对象而不是 TLabel 对象,并将它们转换为 TLabel,这将会失败,但您没有检查这种情况,因此您的 m_Labels 映射最终会填充为 NULL 指针。

因此,TextEditsToLabels() 展示了未定义行为,因为它试图访问 m_Labels 映射中不存在的 TLabel 对象,但您没有检查这种情况。如果指定的键不存在,映射的 operator[] 将返回一个 NULL 指针。切换到 std::map 并不能“部分解决”这个问题。

您需要修复 packLabels() 中的错误,然后您显示的代码的其余部分 应该 工作正常,例如:

  1. void Container::packLabels(){
  2. //String Labels[4] = {"Edit1", "Edit2", "Edit3", "Edit4"};
  3. String Labels[4] = {"Label1", "Label2", "Label3", "Label4"}; // <-- 这里
  4. for (int i = 0; i < 4; i++) {
  5. m_Labels.insert(std::make_pair(Labels[i], dynamic_cast<TLabel*>(m_Form->FindComponent(Labels[i]))));
  6. }
  7. }

您还没有检查 FindComponent() 在插入映射之前是否返回了一个 NULL 指针。您也应该修复这个问题,例如:

  1. void Container::packEdits(){
  2. String Edits[4] = {"Edit1", "Edit2", "Edit3", "Edit4"};
  3. for (int i = 0; i < 4; i++) {
  4. TComponent* comp = m_Form->FindComponent(Edits[i]);
  5. if (comp) {
  6. m_Edits.insert(std::make_pair(Edits[i], static_cast<TEdit*>(comp)));
  7. }
  8. }
  9. }
  10. void Container::packLabels(){
  11. String Labels[4] = {"Label1", "Label2", "Label3", "Label4"};
  12. for (int i = 0; i < 4; i++) {
  13. TComponent* comp = m_Form->FindComponent(Labels[i]);
  14. if (comp) {
  15. m_Labels.insert(std::make_pair(Labels[i], static_cast<TLabel*>(comp)));
  16. }
  17. }
  18. }

另外:您的代码在一些地方使用了指针,实际上应该使用引用。

我建议将代码更改为以下方式:

Test.h:

  1. // 这部分不是翻译,是代码,请忽略

Test.cpp:

  1. // 这部分不是翻译,是代码,请忽略

Container.h:

  1. // 这部分不是翻译,是代码,请忽略

Container.cpp:

  1. // 这部分不是翻译,是代码,请忽略

Handler.h:

  1. // 这部分不是翻译,是代码,请忽略

Handler.cpp:

  1. // 这部分不是翻译,是代码,请忽略

如果您有任何其他疑问,请随时提出。

英文:

packLabels() is searching for TEdit objects instead of TLabel objects, and is dynamic_cast'ing them to TLabel, which will fail but you are not checking for that condition, and so your m_Labels map ends up being filled with NULL pointers.

As such, TextEditsToLabels() exhibits undefined behavior because it is trying to access TLabel objects that do not exist in the m_Labels map, but you are not checking for that condition. The map's operator[] will return a NULL pointer if the specified key does not exist. Switching to std::map does not "partially solve" that problem.

You need to fix that packLabels() error, then the rest of the code you have shown should work fine, eg:

  1. void Container:: packLabels(){
  2. //String Labels[4] = {&quot;Edit1&quot;, &quot;Edit2&quot;, &quot;Edit3&quot;, &quot;Edit4&quot;};
  3. String Labels[4] = {&quot;Label1&quot;, &quot;Label2&quot;, &quot;Label3&quot;, &quot;Label4&quot;}; // &lt;-- HERE
  4. for (int i = 0; i &lt; 4; i++) {
  5. m_Labels.insert(std::make_pair(Labels[i], dynamic_cast&lt;TLabel*&gt;(m_Form-&gt;FindComponent(Labels[i]))));
  6. }
  7. }

You are also not checking whether FindComponent() is returning a NULL pointer before inserting into your maps. You should fix that as well, eg:

  1. void Container::packEdits(){
  2. String Edits[4] = {&quot;Edit1&quot;, &quot;Edit2&quot;, &quot;Edit3&quot;, &quot;Edit4&quot;};
  3. for (int i = 0; i &lt; 4; i++) {
  4. TComponent* comp = m_Form-&gt;FindComponent(Edits[i]);
  5. if (comp) {
  6. m_Edits.insert(std::make_pair(Edits[i], static_cast&lt;TEdit*&gt;(comp)));
  7. }
  8. }
  9. }
  10. void Container:: packLabels(){
  11. String Labels[4] = {&quot;Label1&quot;, &quot;Label2&quot;, &quot;Label3&quot;, &quot;Label4&quot;};
  12. for (int i = 0; i &lt; 4; i++) {
  13. TComponent* comp = m_Form-&gt;FindComponent(Labels[i]);
  14. if (comp) {
  15. m_Labels.insert(std::make_pair(Labels[i], static_cast&lt;TLabel*&gt;(comp)));
  16. }
  17. }
  18. }

On a side note: your code is using pointers in places that really should be using references instead.

I would suggest changing the code to be more like the following instead:

Test.h

  1. //---------------------------------------------------------------------------
  2. #ifndef TestH
  3. #define TestH
  4. //---------------------------------------------------------------------------
  5. #include &lt;System.Classes.hpp&gt;
  6. #include &lt;Vcl.Controls.hpp&gt;
  7. #include &lt;Vcl.StdCtrls.hpp&gt;
  8. #include &lt;Vcl.Forms.hpp&gt;
  9. //---------------------------------------------------------------------------
  10. #include &quot;Container.h&quot;
  11. //---------------------------------------------------------------------------
  12. class TUMap_Test : public TForm
  13. {
  14. __published: // IDE-managed Components
  15. TEdit *Edit1;
  16. TEdit *Edit2;
  17. TEdit *Edit3;
  18. TEdit *Edit4;
  19. TLabel *Label1;
  20. TLabel *Label2;
  21. TLabel *Label3;
  22. TLabel *Label4;
  23. TButton *Button1;
  24. void __fastcall Button1Click(TObject *Sender);
  25. private: // User declarations
  26. Container Cont;
  27. public: // User declarations
  28. __fastcall TUMap_Test(TComponent* Owner);
  29. };
  30. //---------------------------------------------------------------------------
  31. extern PACKAGE TUMap_Test *UMap_Test;
  32. //---------------------------------------------------------------------------
  33. #endif

Test.cpp

  1. #include &lt;vcl.h&gt;
  2. #pragma hdrstop
  3. #include &quot;Test.h&quot;
  4. #include &quot;Handler.h&quot;
  5. #pragma package(smart_init)
  6. #pragma resource &quot;*.dfm&quot;
  7. TUMap_Test *UMap_Test;
  8. fastcall TUMap_Test::TUMap_Test(TComponent* Owner): TForm(Owner), Cont(this) {
  9. Cont.packComponents();
  10. }
  11. void __fastcall TUMap_Test::Button1Click(TObject *Sender){
  12. TextEditsToLabels(Cont);
  13. }

Container.h

  1. #ifndef ContainerH
  2. #define ContainerH
  3. #include &lt;vcl.h&gt;
  4. #include &lt;unordered_map&gt;
  5. class Container{
  6. private:
  7. TForm* m_Form;
  8. std::unordered_map&lt;String, TEdit*&gt; m_Edits;
  9. std::unordered_map&lt;String, TLabel*&gt; m_Labels;
  10. void packEdits();
  11. void packLabels();
  12. public:
  13. Container(TForm* Form);
  14. void packComponents();
  15. std::unordered_map&lt;String, TEdit*&gt;&amp; getEdits();
  16. std::unordered_map&lt;String, TLabel*&gt;&amp; getLabels();
  17. /* alternatively:
  18. TEdit* getEdit(String Name);
  19. TLabel* getLabel(String Name);
  20. */
  21. };
  22. #endif

Container.cpp

  1. #include &quot;Container.h&quot;
  2. Container::Container(TForm* Form) {
  3. m_Form = Form;
  4. }
  5. void Container::packComponents() {
  6. packEdits();
  7. packLabels();
  8. }
  9. void Container::packEdits() {
  10. String Edits[4] = {&quot;Edit1&quot;, &quot;Edit2&quot;, &quot;Edit3&quot;, &quot;Edit4&quot;};
  11. for (int i = 0; i &lt; 4; ++i) {
  12. TComponent *comp = m_Form-&gt;FindComponent(Edits[i]);
  13. TEdit *edit = dynamic_cast&lt;TEdit*&gt;(comp);
  14. if (edit) {
  15. m_Edits[Edits[i]] = edit;
  16. }
  17. }
  18. }
  19. void Container::packLabels() {
  20. String Labels[4] = {&quot;Label1&quot;, &quot;Label2&quot;, &quot;Label3&quot;, &quot;Label4&quot;};
  21. for (int i = 0; i &lt; 4; ++i) {
  22. TComponent* comp = m_Form-&gt;FindComponent(Labels[i]);
  23. TLabel *label = dynamic_cast&lt;TLabel*&gt;(comp);
  24. if (label) {
  25. m_Labels[Labels[i]] = label;
  26. }
  27. }
  28. }
  29. std::unordered_map&lt;String, TEdit*&gt;&amp; Container::getEdits() {
  30. return m_Edits;
  31. }
  32. std::unordered_map&lt;String, TLabel*&gt;&amp; Container::getLabels() {
  33. return m_Labels;
  34. }
  35. /* alternatively:
  36. TEdit* Container::getEdit(String Name) {
  37. return m_Edits.at(Name);
  38. }
  39. TLabel* Container::getLabel(String Name) {
  40. return m_Labels.at(Name);
  41. }
  42. */

Handler.h

  1. #ifndef HandlerH
  2. #define HandlerH
  3. #include &quot;Container.h&quot;
  4. void TextEditsToLabels(Container&amp; Cont);
  5. #endif

Handler.cpp

  1. #include &quot;Handler.h&quot;
  2. static String getEditText(TEdit* edit) {
  3. if (edit) {
  4. return edit-&gt;Text;
  5. }
  6. return EmptyStr;
  7. }
  8. static void TextEditToLabel(TEdit* edit, TLabel* label) {
  9. if ((edit) &amp;&amp; (label)) {
  10. label-&gt;Caption = edit-&gt;Text;
  11. }
  12. }
  13. void TextEditsToLabels(Container&amp; Cont) {
  14. std::unordered_map&lt;String, TEdit*&gt;&amp; edits = Cont.getEdits();
  15. std::unordered_map&lt;String, TLabel*&gt;&amp; labels = Cont.getLabels();
  16. String Text1 = getEditText(edits[&quot;Edit1&quot;]);
  17. String Text2 = getEditText(edits[&quot;Edit2&quot;]);
  18. String Text3 = getEditText(edits[&quot;Edit3&quot;]);
  19. String Text4 = getEditText(edits[&quot;Edit4&quot;]);
  20. //BREAKPOINT
  21. //Change the Caption of Labels to Edits Text
  22. TextEditToLabel(edits[&quot;Edit1&quot;], labels[&quot;Label1&quot;]);
  23. TextEditToLabel(edits[&quot;Edit2&quot;], labels[&quot;Label2&quot;]);
  24. TextEditToLabel(edits[&quot;Edit3&quot;], labels[&quot;Label3&quot;]);
  25. TextEditToLabel(edits[&quot;Edit4&quot;], labels[&quot;Label4&quot;]);
  26. /* alternatively:
  27. String Text1 = Cont.getEdit(&quot;Edit1&quot;)-&gt;Text;
  28. String Text2 = Cont.getEdit(&quot;Edit2&quot;)-&gt;Text;
  29. String Text3 = Cont.getEdit(&quot;Edit3&quot;)-&gt;Text;
  30. String Text4 = Cont.getEdit(&quot;Edit4&quot;)-&gt;Text;
  31. //BREAKPOINT
  32. //Change the Caption of Labels to Edits Text
  33. Cont.getLabel(&quot;Label1&quot;)-&gt;Caption = Cont.getEdit(&quot;Edit1&quot;)-&gt;Text;
  34. Cont.getLabel(&quot;Label2&quot;)-&gt;Caption = Cont.getEdit(&quot;Edit2&quot;)-&gt;Text;
  35. Cont.getLabel(&quot;Label3&quot;)-&gt;Caption = Cont.getEdit(&quot;Edit3&quot;)-&gt;Text;
  36. Cont.getLabel(&quot;Label4&quot;)-&gt;Caption = Cont.getEdit(&quot;Edit4&quot;)-&gt;Text;
  37. */
  38. }

huangapple
  • 本文由 发表于 2023年5月25日 07:58:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定