按名称访问文本框

按名称访问文本框

以前,文本框是通过Worksheet.TextBoxes集合中的索引访问的,但现在您也可以通过名称从该集合中访问文本框。如果您已经知道其名称,这是一种方便快捷的访问文本框的方式。

以下示例代码首先创建了一个文本框并分配了一些文本和名称。然后在接下来的行中,我们通过其名称访问相同的文本框并打印其文本。

通过名称访问文本框的C#代码

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Create an object of the Workbook class
Workbook workbook = new Workbook();
// Access first worksheet from the collection
Worksheet sheet = workbook.Worksheets[0];
// Add the TextBox to the worksheet
int idx = sheet.TextBoxes.Add(10, 10, 10, 10);
// Access newly created TextBox using its index & name it
TextBox tb1 = sheet.TextBoxes[idx];
tb1.Name = "MyTextBox";
// Set text for the TextBox
tb1.Text = "This is MyTextBox";
// Access the same TextBox via its name
TextBox tb2 = sheet.TextBoxes["MyTextBox"];
// Display the text of the TextBox accessed via name
Console.WriteLine(tb2.Text);
Console.WriteLine("Press any key to continue...");
Console.ReadKey();

样本代码生成的控制台输出

这是上面示例代码的控制台输出。

This is MyTextBox