名前でテキストボックスにアクセス

名前でテキストボックスにアクセスする

以前は、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