Access to components in a loop

Hi guys,

how can i access to components in a loop.?

My problem is in this case ui_Panel1, ui_Panel2 etc.

    lv_label_set_text(ui_comp_get_child(ui_Panel1, UI_COMP_PANEL_LABEL), Buffer.item_name[0]);
    lv_label_set_text(ui_comp_get_child(ui_Panel2, UI_COMP_PANEL_LABEL), Buffer.item_name[1]);
    lv_label_set_text(ui_comp_get_child(ui_Panel2, UI_COMP_PANEL_LABEL), Buffer.item_name[2]);
    lv_label_set_text(ui_comp_get_child(ui_Panel3, UI_COMP_PANEL_LABEL), Buffer.item_name[3]);

best regards

I guess you want to assign texts from a string array to different panels in a for loop. You need to create an array of pointers to the lv_obj_t* panel-object pointers, and assign the string array’s elements to them in the loop. Something like this:

enum { PANEL_COUNT = 3 };
static lv_obj_t* *Panels [PANEL_COUNT] = { &ui_Panel1, &ui_Panel2, &ui_Panel3 };

for (unsigned char i=0; i<PANEL_COUNT; ++i) {
lv_label_set_text( ui_comp_get_child( *Panels[i], UI_COMP_PANEL_LABEL ), Buffer.item_name[i] );

Thanks for the gret Tip Hermit ! That works!

1 Like

You’re welcome, it’s good to see that this solution is working for your project.