Unofficial Nextion/TJC User Forum

How to index objects on a page

Hello.
I have five numeric display boxes on a page, and I want to put values into them without calling them specifically. Can I index them? And if I do index them, do I use their name or the object id?

For example, suppose I have display boxes, n0, n1, n2, n3, n4, n5, and want to populate the five displays like this…

for(k=1;k<6;k++)
{
n[k].val=k
}

I would assume that the variable “k” would take the place of the object id. But this does not work. I get an invalid variable error. I have tried making them all global. And I have tried putting the page number in there as well (page0.n[k].val, and page[0].n[k].val). Same errors all the way around.

Any help would be appreciated.

AKG

Two indexed arrays exist in Nextion firmware for this purpose, the p[] and the b[] array. Each component can thus be addressed by
p[pageId].b[objectId].attribute=…

Thank you. So buttons and pages can be indexed, nothing else can. Well, I could use buttons for the numeric displays.

Thanks again

Let me ask a follow up question. How would I learn this information without asking this forum? Is there a manual available, aside from the instruction set?

You‘re confusing two things. The b[] array gives access to all components on that page. The name of the array is not related to the name of the components, like buttons f.ex. Remember, you can rename your buttons to anything you like. Instead of b0, b1, b2, … they could be called return, enter, ok, blubblub, … And you could still access them over this array just like you can for numbers, dual state buttons and all the other components. There‘s no exception.

The instruction set covers I‘d say 90% of the knowledge. Nextion has some additional blogposts but really most of the stuff you need to know is on the instruction set page. Yes, these arrays are mentioned and explained there, too.

Kind regards,
Max

That is very valuable. Thanks so much.

To avoid this confusion, I had written “each component”, regarding the b[] array. “Each component” was NOT intended to be “buttons only”. Sorry if that hasn’t been clear.

No worries. It’s my mistake. I’m not used to this level of programming. I just saw code that said p[i].b[k].txt=“xxxx”, and I made the assumption that “b” referred to buttons.


This forum is in no way affiliated with NEXTION®, ITEAD STUDIO®, TJC®, or anyone else really. All product names, logos, and brands are property of their respective owners. All company, product, and service names used in this website are for identification purposes only. Use of these names, logos, and brands does not imply endorsement from the respective rights holder(s).