画像ファイル
画像ファイル コントロールの配列はジェネリクスに任せた
VB.NET C#全般
1羊の皮を着た狼 VB.NET
2Form1、Form2の相互参照
3Form1、Form2の相互参照 2
4VB.NET C# データ型の基本
5VB.NET C# 文字列
6VB.NET タイマー精度
7BackgroundWorkerの魅力1..
8BackgroundWorkerの魅力2..
9VB6のタイマー
10コントロールの配列をインデクサ..
11コントロールの配列はジェネリク..
12インデクサ(C#、VB.NET)
13インデクサでBit操作
14Unicode 入門
15デリゲート入門
16マルチスレッド入門
17イベント入門
18デリゲートとイベント
18インターフェースの基本

RichTextBox関係
1RichTextBoxの不思議
2テキスト色付け高速化計画
3VB.NET RichTextBox1
4VB.NET RichTextBox 2

RS-232C関係
1RS-232Cの基礎
2RS-232Cの何が変わった..
3SerialPortクラス
4Unicode(ユニコード)の壁
5マルチスレッドの壁
6RS-232C サンプルコード
7RS-232CのHEXモニタ
8RS-232C 送信モジュール
9RS-232Cのループテスト
10RS-232Cのピンチェンジ..

Socket通信
1C#、VB2005 でSocket通信
2サーバー 複数接続

プロセス間通信
1プロセス間通信(送信側)
2プロセス間通信(受信側)


質問、意見はこちらに
画像ファイル

恐るべしジェネリクス
配列は厄介である、特に動的に配列の大きさを変えなければならない場合はなおさらである。
ところがジェネリクスなる物が出来た。
必要に応じて長さを自動的に変更してくれるListクラスである。
しかもオブジェクトなら殆ど何でも配列に格納出来るスーパーListなのである。
もうTextBoxやButtonのオブジェクトの配列で悩む必要は全く無くなったのである。

ジェネリッククラスのListでコントロールの配列を使う
画像ファイル
上の画像の様にボタンの下にテキストボックスが有り、ボタンを押すと下のテキストボックスに、 押したボタンの名前が入る様なプログラムを、Listクラスを使って書いてみる。
コードは極めて簡単で殆ど注意点も無い。

C#コード
//ボタンのリストクラスを作成
List<Button> Button = new List<Button>();
//テキストのリストクラスを作成
List<TextBox> TextBox = new List<TextBox>();
VB2005コード
'ボタンのリストクラスを作成
Dim Button As List(Of Button) = New List(Of Button)()
'テキストのリストクラスを作成
Dim TextBox As List(Of TextBox) = New List(Of TextBox)()
これだけでButtonとTextBoxを保持するListが出来上がる、後は実際のコントロールを 足しこむだけである。

C#コード
Button.Add(New Button())
TextBox.Add(New TextBox())
VB2005コード
Button.Add(New Button())
TextBox.Add(New TextBox())
これでコントロールが足しこまれる。

C#コード
Button[i].Click += Button_Click;
VB2005コード
AddHandler Button(i).Click, AddressOf Button_Click
これで共通のイベント処理がされる。

C#コード
Button[i].Tag = i;    //タグに番号を追加
VB2005コード
Button(i).Tag = i    'タグに番号を追加
イベント先で送りもとのコントロールの名前がわかる様にTagに番号を埋め込む。
もちろんこれは名前から番号を取得しても良い。

C#コード
//ボタンのクリックイベントを設定
private void Button_Click(object sender, EventArgs e)
{
Button bt=(Button)sender;
TextBox[(int)bt.Tag].Text = bt.Text ; }
VB2005コード
'ボタンのクリックイベントを設定
Private Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim bt As Button = DirectCast(sender, Button)
TextBox(CInt(bt.Tag)).Text = bt.Text
End Sub
後はイベント先で送り元の番号をTagから取得すればOKである。
なんと簡単な!
もう配列はジェネリクスに任せた。

C#コード
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
//ボタンのリストクラスを作成
List<Button> Button = new List<Button>();
//テキストのリストクラスを作成
List<TextBox> TextBox = new List<TextBox>();
public Form1()
{
InitializeComponent();
InitializeComponent();
for (int i = 0; i < 5; i++)
{
//リストの10個のボタンを追加
Button.Add(new Button());
//Button[i].Name = "Button" + Convert.ToString(i + 1);
Button[i].Text = "Button" + Convert.ToString(i + 1);
Button[i].Tag = i;    //タグに番号を追加
Button[i].Parent = this; //これが無いと表示されない
//場所を設定
Button[i].Location = new Point(Button[0].Width * i + 5, 10);

//イベント先を設定
Button[i].Click += Button_Click;
//リストに10個のテキストを追加
TextBox.Add(new TextBox());
TextBox[i].Parent = this;
//場所を設定
TextBox[i].Location = new Point(Button[i].Left, Button[i].Top
                         + Button[i].Height);
//長さを設定
TextBox[i].Width = Button[i].Width;
}
}

//ボタンのクリックイベントを設定
private void Button_Click(object sender, EventArgs e)
{
Button bt = (Button)sender;
TextBox[(int)bt.Tag].Text = bt.Text;
}
}
}
VB2005コード
Option Strict On
Public Class Form1
'ボタンのリストクラスを作成
Dim Button As List(Of Button) = New List(Of Button)()
'テキストのリストクラスを作成
Dim TextBox As List(Of TextBox) = New List(Of TextBox)()

Private Sub Form1_Load(ByVal sender As System.Object,  _
ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 4
'リストの10個のボタンを追加
Button.Add(New Button())
Button(i).Name = "Button" + Convert.ToString(i + 1)
Button(i).Text = "Button" + Convert.ToString(i + 1)
Button(i).Tag = i 'タグに番号を追加
Button(i).Parent = Me 'これが無いと表示されない
'場所を設定
Button(i).Location = New Point(Button(0).Width * i + 5, 10)
'イベント先を設定
AddHandler Button(i).Click, AddressOf Button_Click
'リストに10個のテキストを追加
TextBox.Add(New TextBox())
TextBox(i).Parent = Me
'場所を設定
TextBox(i).Location = New Point(Button(i).Left, Button(i).Top + _
Button(i).Height)
'長さを設定
TextBox(i).Width = Button(i).Width
Next
End Sub
'ボタンのクリックイベントを設定
Private Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim bt As Button = DirectCast(sender, Button)
TextBox(CInt(bt.Tag)).Text = bt.Text
End Sub
End Class
画像ファイル