画像ファイル
画像ファイル インデクサ(C#、VB.NET)
C#コード
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace indexer2
{
public partial class Form1 : Form
{
//テキストボックスの配列を持つクラス
ArryTextBox aBox;
//テキストボックスの配列
TextBox[] tBox = new TextBox[5];
public Form1()
{
InitializeComponent();

for (int i = 0; i < 5; i++)
{
//新しいテキストボックスを作成して配列に格納
tBox[i] = new TextBox ();
//表示の為に必要
tBox[i].Parent = this;
//テキストボックスの位置を設定
tBox[i].Location = new Point(tBox[i].Width * i + 5, 10);
//個別のTextを設定する
tBox[i].Text = "textBox-" + i.ToString();
}
//配列の参照を持つクラスに配列を設定する
aBox = new ArryTextBox(tBox);
}

//フォームをクリックするとテキストボックスの文字が移動する
private void Form1_Click(object sender, EventArgs e)
{
//一番右のテキストを保存
string btext = aBox[4];
//右にシフト
for (int i = 3; i > -1; i--)
aBox[i + 1] = aBox[i];
//一番左のテキストボックスに一番右の文字を入れる
aBox[0] = btext;
}
}

//テキストボックスの配列を持つクラス
public class ArryTextBox
{
private TextBox[] tb;//内部の配列

//コンストラクタ、引数にテキストボックスの配列を持つ
public ArryTextBox(TextBox[] formsTextBox )
{
//内部の配列に参照を設定する
tb = formsTextBox;
}

//インデクサ
public String this[int index]
{
get
{
//テキストクスのテキストを返す
return tb[index].Text;
}
set
{
//テキストボックスのテキストにに値を設定する
tb[index].Text = value;
}
}
}
}
画像 上記C#コードのダウンロード
画像 上記VB2005のコードのダウンロード

画像ファイル