按名称访问文本框

按名称访问文本框

早些时候,文本框是通过索引从工作表.文本框集合,但现在您还可以从此集合中按名称访问文本框。如果您已经知道文本框的名称,这是访问文本框的一种方便快捷的方法。

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

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