Access the Text Box by the Name with C++
Access the Text Box by the Name
Earlier, text boxes were accessed by index from the Worksheet.GetTextBoxes() collection, but now you can also access the text box by name from this collection. This is a convenient and quick way to access your text box if you already know its name.
The following sample code first creates a text box and assigns it some text and a name. Then, in the next lines, we access the same text box by its name and print its text.
C++ code to access the text box by name
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
using namespace Aspose::Cells::Drawing;
int main()
{
Aspose::Cells::Startup();
// Create an object of the Workbook class
Workbook workbook;
// Access first worksheet from the collection
Worksheet sheet = workbook.GetWorksheets().Get(0);
// Add the TextBox to the worksheet
int idx = sheet.GetTextBoxes().Add(10, 10, 10, 10);
// Access newly created TextBox using its index & name it
TextBox tb1 = sheet.GetTextBoxes().Get(idx);
tb1.SetName(u"MyTextBox");
// Set text for the TextBox
tb1.SetText(u"This is MyTextBox");
// Access the same TextBox via its name
TextBox tb2 = sheet.GetTextBoxes().Get(u"MyTextBox");
// Display the text of the TextBox accessed via name
std::cout << tb2.GetText().ToUtf8() << std::endl;
std::cout << "Press any key to continue..." << std::endl;
std::cin.get();
Aspose::Cells::Cleanup();
}
Console output generated by the sample code
Here is the console output of the above sample code.
This is MyTextBox