準備
(なし)
デザイン
1. フォーム (Form1) にボタン (button1) を配置します。
2. フォーム (Form1) にリストボックス (listBox1) を配置します。
サンプルコード (C#)
// 名前空間の追加 // (なし) // 構造体の宣言 struct stComputer { public string sMaker; public string sPCName; } // コード private void button1_Click(object sender, EventArgs e) { stComputer[] sF = { new stComputer { sMaker = "NEC", sPCName = "ValueStar" }, new stComputer { sMaker = "SONY", sPCName = "VAIO" }, new stComputer { sMaker = "DELL", sPCName = "Precision" } }; foreach (stComputer item in sF) { listBox1.Items.Add(item.sMaker + " " + item.sPCName); } }
解説
構造体の配列を宣言すると同時に初期化しています。
結果
動作確認環境
Visual Studio 2015 Professional (C# 6.0)